Vue.directive('drag', function (el,bindings) { el.onmousedown = function(e){ let x = e.pageX - el.offsetLeft; let y = e.pageY - el.offsetTop; document.onmousemove = function (e){ el.style.left = e.pageX - x + 'px'; el.style.top = e.pageY - y + 'px'; } document.onmouseup = function(){ document.onmousemove = document.onmouseup = null; } } })
将需要移动的div加上:
v-drag
例:
<div v-drag>需拖拽的盒子</div>