vue 拖拽元素到任意位置

使用vue-drag-it-dude组件

npm install vue-drag-it-dude --save

参考地址:https://github.com/xzqyun/vue-drag-it-dude

import DragItDude from 'vue-drag-it-dude';export default {name: '***',components: {DragItDude},
}<template><div id="app" class="parentElement"><drag-it-dude@activated="handleActivated"@dragging="handleDragging"@dropped="handleDropped"><div class="innerElement">{{ text }}</div></drag-it-dude></div>
</template><script>
import DragItDude from "vue-drag-it-dude";export default {name: "App",components: {DragItDude},data: () => ({text: "Just move me!",}),methods: {handleActivated() {this.text = "I am ready for great things!";},handleDragging() {this.text = "Weeee!";},handleDropped() {this.text = "That's place is awesome!";setTimeout(() => {this.text = "Just move me!";}, 3000);}}
};
</script><style>.parentElement {position: relative;}
</style>

由于该组件并没有返回元素的所在位置,所以我对该组件进行了稍微的改动

在components创建drag文件夹,放入DragItDude.vue,以下是DragItDude.vue的代码:

<template><div class="drag-it-dude" @touchstart.stop="hang" @touchend.stop="drop" @mousedown.stop="hang" @mouseup.stop="drop"@touchmove.stop="iosMove" v-bind:style="{top:parentTop  + 'px',left:parentLeft + 'px'}"><slot></slot></div>
</template><script>export default {name: 'drag',props: {width: {type: Number,default: 0,},height: {type: Number,default: 0,},parentWidth: {type: Number,default: 0,},parentHeight: {type: Number,default: 0,},parentLeft: {type: Number,default: 0,},parentTop: {type: Number,default: 0,},},watch: {width(newWidth, oldWidth) {if (newWidth < oldWidth) return;if (this.left === 0) return;this.parent.width = this.parentWidth || this.elem.parentNode.offsetWidth;this.parent.height = this.parentHeight || this.elem.parentNode.offsetHeight;if (newWidth > this.parent.width - this.left) {const newLeft = this.parent.width - newWidth;this.left = newLeft < 0 ? 0 : newLeft;this.elem.style.left = `${this.left}px`;}},height(newHeight, oldHeight) {if (newHeight < oldHeight) return;if (this.top === 0) return;this.parent.width = this.parentWidth || this.elem.parentNode.offsetWidth;this.parent.height = this.parentHeight || this.elem.parentNode.offsetHeight;if (newHeight > this.parent.height - this.top) {const newTop = this.parent.height - this.height;this.top = newTop;this.elem.style.top = `${this.top}px`;}},},data: () => ({shiftY: null,shiftX: null,left: 0,top: 0,elem: null,isIos: false,parent: {width: 0,height: 0,},}),methods: {iosMove(e) {if (this.isIos) this.elementMove(e);},elementMove(e) {this.$emit('dragging', {left: this.left,top: this.top});e.preventDefault();if (!e.pageX) {document.body.style.overflow = 'hidden';}const x = e.pageX || e.changedTouches[0].pageX;const y = e.pageY || e.changedTouches[0].pageY;let newLeft = x - this.shiftX;let newTop = y - this.shiftY;const newRight = x - this.shiftX + this.elem.offsetWidth;const newBottom = y - this.shiftY + this.elem.offsetHeight;if (newLeft < 0) {newLeft = 0;} else if (newRight > this.parent.width) {newLeft = this.parent.width - this.elem.offsetWidth;} else {newLeft = x - this.shiftX;}if (newTop < 0) {newTop = 0;} else if (newBottom > this.parent.height) {newTop = this.parent.height - this.elem.offsetHeight;} else {newTop = y - this.shiftY;}this.elem.style.left = `${newLeft}px`;this.left = newLeft;this.elem.style.top = `${newTop}px`;this.top = newTop;},hang(e) {this.$emit('activated', {left: this.left,top: this.top});this.parent.width = this.parentWidth || this.elem.parentNode.offsetWidth;this.parent.height = this.parentHeight || this.elem.parentNode.offsetHeight;this.shiftX = e.pageX ?e.pageX - this.elem.offsetLeft :e.changedTouches[0].pageX - this.elem.offsetLeft;this.shiftY = e.pageY ?e.pageY - this.elem.offsetTop :e.changedTouches[0].pageY - this.elem.offsetTop;if (e.pageX) {if (this.isIos) {this.elem.addEventListener('touchmove', this.elementMove);} else {this.elem.addEventListener('mousemove', this.elementMove);this.elem.addEventListener('mouseleave', this.drop);}} else {this.elem.addEventListener('touchmove', this.elementMove);}},drop() {this.$emit('dropped', {left: this.left,top: this.top});document.body.style.overflow = null;this.elem.removeEventListener('mousemove', this.elementMove, false);this.elem.removeEventListener('touchmove', this.elementMove, false);this.elem.onmouseup = null;this.elem.ontouchend = null;},},mounted() {this.isIos = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;this.elem = this.$el;},};
</script><style>.drag-it-dude {position: absolute;top: 0;left: 0;z-index: 1;}
</style>

引用:

import Drag from '../../../components/drag/DragItDude.vue';
export default {components: {Drag},

使用

<drag @dropped="handleDropped($event)" :parentTop="100" :parentLeft="100"><el-input></el-input>
</drag>handleDropped(obj) {console.log(obj);//返回所在位置
},

vue 拖拽元素到任意位置相关推荐

  1. 【记】Vue - 拖拽元素组件实现

    需求描述: 1.可实现PC/移动端元素拖拽移动 2.支持2种模式:1.元素跟随光标点放置2.元素在光标点平齐位置靠侧边吸附 市面上估计有很多这种组件和功能了,但我没找到合适的,用了VueUse的use ...

  2. JQuery 拖拽元素,并移动其他元素位置

    JQuery 拖拽元素,并移动其他元素位置 <!DOCTYPE html> <html> <head><meta charset="UTF-8&qu ...

  3. html元素拖动互换位置原理,【详】JS实现拖拽元素互换位置

    写在前面的废话 大家好,我是练习js时长接近两年半的个人练习生--李大雷 算了,直接 鸡,你太美~ 应用场景 很多时候,我们需要让用户来自定义自己想要的菜单顺序,或者一些按钮的排序,那么这个时候,怎么 ...

  4. vue拖动添加模块展示_一个全新的Vue拖拽特性实现:“移动”部分

    关于拖拽 CabloyJS提供了完备的拖拽特性,可以实现移动和调整尺寸两大类功能,这里对移动的开发进行阐述关于调整尺寸的开发,请参见:拖拽:调整尺寸 演示 开发步骤 下面以模块test-party为例 ...

  5. 简单使用vue拖拽组件vue3-dnd

    项目中需要使用到拖拽,这里使用vue3-dnd来满足需求 这里项目使用的vue3(使用js而非ts) 插件官网地址:Vue3 DnD 安装 npm install vue3-dnd react-dnd ...

  6. Vue实现DOM元素拖放互换位置

    一.拖放和释放 HTML 拖放接口使得 web 应用能够在网页中拖放文件.这里将介绍了 web 应用如何接受从底层平台的文件管理器拖动DOM的操作. 拖放的主要步骤是为 drop 事件定义一个释放区( ...

  7. Vue拖拽组件开发实例

    为什么选择Vue? 主要原因:对于前端开发来说,兼容性是我们必须要考虑的问题之一.我们的项目不需要兼容低版本浏览器.项目本身也是一个数据驱动型的.加之,Vue本身具有以下主要特性: 使用虚拟DOM: ...

  8. VUE 拖拽组件 vue.draggable

    中文文档 https://www.itxst.com/vue-draggable/tutorial.html 安装 npm i -S vuedraggable 属性 属性名称 说明 group :gr ...

  9. vue拖拽组件生成页面代码,vue可视化拖拽组件模板

    怎么使用vuedraggable实现简单的拖拽,只拖拽一个内容(div) . 仔细观察发现,今日头条导航部分编辑效果,有以下几个效果1:点击编辑开启可以编辑的效果(也就是是否开启拖拽)2:拖拽前选中效 ...

最新文章

  1. python文件的编译
  2. simpledateformat 毫秒_阿里巴巴 | 为啥代码中禁用static修饰SimpleDateFormat?
  3. 图数据库并非要取代区块链,而是让区块链如虎添翼
  4. 深入理解计算机系统 第三章笔记,《深入理解计算机系统》笔记.PDF
  5. android jackson 解析json字符串,android:json解析的两个工具:Gson和Jackson的使用小样例...
  6. mysql用户权限重置_mysql中重置密码与新用户权限分配的方法介绍
  7. 在Node.js中发起HTTP请求的5种方法
  8. tp3.2 URL模式
  9. php 5的手册,本手册中所涉及的 PHP 版本 - PHP 5 中文文档
  10. 随机信号分析 matlab仿真实验,随机信号分析实验报告的(基于MATLAB语言).docx
  11. xmapp启动之路径错误
  12. 68pin SCSI接头针脚排序
  13. NLP基础--single-pass 聚类算法
  14. 英语翻译的重点词汇词组
  15. 英国内政部(Home Office)间谍机构(spy powers)假装它是Ofcom咨询中的一名私人公民1525446049260...
  16. 【网络编程实践】2.4.2 muduo库安装与 procmon 编译
  17. PHP 正则表达式preg_match
  18. Jmeter压力测试报告案例
  19. 王俊杰:5G为零售带来“五新”发展机遇
  20. C语言报错:error: static declaration of xxx follows non-static declaration

热门文章

  1. java mysql查询语句_Mysql查询语句执行过程
  2. python语言中最基本的概念_Python 学习之路-基本的概念(三)
  3. 深井软岩巷道群支护技术与应用_黑龙江优质右旋锚杆厂家量大从优_双福煤矿支护架...
  4. Windows下打开Url 代码封装 C++
  5. memcpy 作用(C++)
  6. Makefile不再编译已经编译过的未更新文件
  7. C语言printf控制光标位置和清空屏幕
  8. Flink所使用到maven依赖
  9. highstack的一个bug
  10. 打印机如何共享多台电脑_多台电脑打印机共享的方法