【vue】给 dom 添加拖拽功能

发布时间 2023-04-02 14:54:21作者: 三勺

实现效果

image

实现步骤

  1. 新建一个 drag.js 注册拖拽指令
import Vue from 'vue';
//使用Vue.directive()定义一个全局指令
//1.参数一:指令的名称,定义时指令前面不需要写v-
//2.参数二:是一个对象,该对象中有相关的操作函数
//3.在调用的时候必须写v-
const drag = Vue.directive('drag', {
    //1.指令绑定到元素上回立刻执行bind函数,只执行一次
    //2.每个函数中第一个参数永远是el,表示绑定指令的元素,el参数是原生js对象
    //3.通过el.focus()是无法获取焦点的,因为只有插入DOM后才生效bind: function (el) { },
    //inserted表示一个元素,插入到DOM中会执行inserted函数,只触发一次
    inserted: function (el) {
        el.onmousedown = function (e) {
            var disx = e.pageX - el.offsetLeft;
            var disy = e.pageY - el.offsetTop;
            document.onmousemove = function (e) {
                el.style.left = e.pageX - disx + 'px';
                el.style.top = e.pageY - disy + 'px';
                console.log(el.style.left)
                console.log(el.style.top)
            }
            document.onmouseup = function () {
                document.onmousemove = document.onmouseup = null;
            }
        }
    },//当VNode更新的时候会执行updated,可以触发多次updated: function (el) { }
})
export default drag;
  1. main.js 进行引入
import drag from './utils/drag.js';
  1. 调用,需要拖拽的div记得加定位
<template>
  <div class="moveWithMouse">
    <h3>拖拽 div </h3>

    <div class="dragContent" v-drag>拖动div</div>
  </div>
</template>

<style scoped>
.moveWithMouse {
   display: flex;
   flex-direction: column;
   align-items: center;
   justify-content: center;
}

.dragContent{
   position: absolute;
   top: 100px;
   width: 300px;
   height: 300px;
   background: pink;
   line-height: 300px;
   text-align: center;
   margin-top: 30px;
}
</style>