Vue实现自定义拖拽元素

13 篇文章 1 订阅
3 篇文章 0 订阅

在其他网友的基础上改的,实现在格边缘上实现拖拽功能

//html
<div class="video_box" @mousemove="onMoveBoxBefore">

//js
onMoveBoxBefore(e) {
    let box = e.target;
    let _xl = e.offsetX, _xr = box.clientWidth - e.offsetX;   //x轴
    let _yt = e.offsetY, _yb = box.clientHeight - e.offsetY;  //y轴
    //鼠标在div边缘0到3之内才可移动
    if (((_xl > 0 && _xl < 3) || (_xr > 0 && _xr < 3)) || ((_yt > 0 && _yt < 3) || (_yb > 0 && _yb < 3))) {
        box.style.cursor = "all-scroll";
        box.classList.add("notselect");
        box.onmousedown = (e) => {
            //算出鼠标相对元素的位置
            let disX = e.clientX - box.offsetLeft;
            let disY = e.clientY - box.offsetTop;
            document.onmousemove = (e) => {
                let left = e.clientX - disX;
                let top = e.clientY - disY;
                box.style.left = left + 'px';
                box.style.top = top + 'px';
            };
            document.onmouseup = (e) => {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }
    } else {
        box.style.cursor = "default";
        box.classList.remove("notselect");
        box.onmousedown = null;
    }
}

//css
.video_box {
    position: absolute;
    width: 100px;
    height: 100px;
    top: 0;
    right: 0;
    background-color: red;
    z-index: 333;
}

.notselect {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值