This time I will show you how to use Vue to implement the drag and drop function, and what are theprecautions for using Vue to implement the drag and drop function. The following is a practical case, let's take a look.
Rendering:位置
x:{{val.x}}
y:{{val.y}}//注意这里要通过指令绑定函数将当前元素的位置数据传出来
Vue.directive('drag',//自定义指令 {bind:function (el, binding) { let op = el; //当前元素 let self = this; //上下文 op.onmousedown = function (e) { //鼠标按下,计算当前元素距离可视区的距离 let disX = e.clientX - op.offsetLeft; let disY = e.clientY - op.offsetTop; document.onmousemove = function (e) { //通过事件委托,计算移动的距离 let l = e.clientX - disX; let t = e.clientY - disY; //移动当前元素 op.style.left = l + 'px'; op.style.top = t + 'px'; //将此时的位置传出去 binding.value({x:e.pageX,y:e.pageY}) }; document.onmouseup = function (e) { document.onmousemove = null; document.onmouseup = null; }; }; } } ); window.onload = function () { let vm = new Vue({ el: '#box', data: { val: '123', style: { width: '100px', height: '100px', background: 'aqua', position: 'absolute', right: '30px', top: 0 } }, methods:{ //接受传来的位置数据,并将数据绑定给data下的val greet(val){ vm.val = val; } } , }); }
The above is the detailed content of How to use Vue to implement drag and drop function. For more information, please follow other related articles on the PHP Chinese website!