Vue让前端飞

vue 拖拽元素指令

2021-10-14  本文已影响0人  蓝海00
效果图
 <!-- 注意要给div元素设置position 和 z-index -->
<div v-drap>盒子</div>
// 拖拽元素
Vue.directive('drag', function (el: any) {
    el.onmousedown = function (ev: any) {
        const disX = ev.clientX - el.offsetLeft;
        const disY = ev.clientY - el.offsetTop;
        document.onmousemove = function (ev) {
            const l = ev.clientX - disX;
            const t = ev.clientY - disY;
            el.style.left = l + 'px';
            el.style.top = t + 'px';
        };
        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
        };
    };
});
上一篇下一篇

猜你喜欢

热点阅读