Vue 实现div或者组件的「拖拽」功能,(PC和移动端都可以用)

Vue 实现div或者组件的拖拽功能

1. 复制这个js到你的项目里

import Vue from 'vue';

/**
 * 1. 在调用的时候必须写v-
 * 2. 在使用的组件上或者div的位置必须是绝对的,在需要拖拽的最外层样式上加上 position: absolute;
 * 3. 在所需要的样式上使用 v-drag
 * 4. 激活之后的样式名为 v-drag-active 没有激活的样式名为 v-drag-inactive
 *
 * @author Zhou Xinchen
 * @type {DirectiveOptions}
 */
const drag = Vue.directive('drag', {
    // 指令绑定到元素上回立刻执行bind函数,只执行一次
    bind: function (el) {

    },
    //inserted表示一个元素,插入到DOM中会执行inserted函数,只触发一次
    inserted: function (el) {


        let wMax = document.documentElement.clientWidth - el.offsetWidth;
        let hMax = document.documentElement.clientHeight - el.offsetHeight;

        if ("ontouchstart" in window) { // 移动端
            el.ontouchstart = function (e) {
                let time1 = new Date().getTime();
                let x = e.touches[0].pageX - el.offsetLeft;
                let y = e.touches[0].pageY - el.offsetTop;
                // 抑制浏览器端默认拖拽行为,移动端是拖拽屏幕,pc端无
                // e.preventDefault(); 开启后点击 子集点击事件事件会无效
                document.ontouchmove = function (e) {
                    let time2 = new Date().getTime();
                    if (time2 - time1 > 300) {
                        el.classList.remove('v-drag-inactive')
                        el.classList.add('v-drag-active')
                    }
                    let left = e.touches[0].pageX - x;
                    let top = e.touches[0].pageY - y;

                    if (left < 0) left = 0;
                    else if (left > wMax) left = wMax;

                    if (top < 0) top = 0;
                    else if (top > hMax) top = hMax;

                    el.style.left = left + 'px';
                    el.style.top = top + 'px';
                }
                document.ontouchend = function () {
                    let time2 = new Date().getTime();
                    if (time2 - time1 > 300) {
                        el.classList.remove('v-drag-active')
                        el.classList.add('v-drag-inactive')
                    }

                    document.ontouchmove = document.ontouchend = null;
                }
            }
        } else { // pc端
            el.onmousedown = function (e) {
                let time1 = new Date().getTime();
                let x = e.pageX - el.offsetLeft;
                let y = e.pageY - el.offsetTop;
                document.onmousemove = function (e) {
                    let time2 = new Date().getTime();
                    if (time2 - time1 > 300) {
                        el.classList.remove('v-drag-inactive')
                        el.classList.add('v-drag-active')
                    }
                    el.style.left = e.pageX - x + 'px';
                    el.style.top = e.pageY - y + 'px';
                }
                document.onmouseup = function () {
                    let time2 = new Date().getTime();
                    if (time2 - time1 > 300) {
                        el.classList.remove('v-drag-active')
                        el.classList.add('v-drag-inactive')
                    }
                    document.onmousemove = document.onmouseup = null;
                }
            }
        }


    },
    updated: function (el) {
    }
})
export default drag;

2. 将这个js注册到全局main.js

import drag from '@/..你的路径.../drag';

3. 在需要使用的 div 或者组件上 添加一个 v-drag

4. 编写激活和没激活的样式 (less)

.contanier {
  margin-top: 100px;
  margin-left: 40px;
  width: 320px;
  height: 600px;

  .drag-item {
    position: absolute;
    width: 100px;
    height: 100px;
    background-color: deepskyblue;
    transform: scale(1);
  }

  .v-drag-active {
    transform: scale(1.2);
  }

  .v-drag-inactive {
    transform: scale(1);
  }
}

更新时间:2021-1-2

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值