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

//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;
}

 

Logo

前往低代码交流专区

更多推荐