JavaScript的限制拖拽效果------JavaScript学习之路6

限制拖拽效果

在这里插入图片描述

思想

offsetLeft 获得元素在左边的窗口的距离

offsetTop 获得元素距离上边窗口的距离

document.documentElement.clientWidth || document.body.clientWidth 获取窗口宽度

document.documentElement.clientHeight || document.body.clientHeight 获取窗口高度

  • 按下鼠标时

    var l = e.clientX - node.offsetLeft;//记录按下鼠标时,鼠标距离元素的左边位置
    var t = e.clientY - node.offsetTop;//记录按下鼠标时,鼠标距离元素的上边位置
    
  • 移动鼠标时

    • 获取元素相对浏览器窗口的位置

      var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;//获取浏览器窗口的宽度
      var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;//获取浏览器窗口的高度
      var x = e.clientX - l;//获取元素相对浏览器左边窗口的距离
      var y = e.clientY - t;//获取元素相对浏览器上边窗口的距离
      
    • 元素移动的窗口限制

      if(x < 0){//限制左边
          x = 0;
      }
      if(x >= windowWidth - d.offsetWidth){//限制右边
          x = windowWidth - d.offsetWidth;
      }
      if(y < 0){//限制上边
          y = 0;
      }
      if(y>= windowHeight - d.offsetHeight){//限制下边
          y = windowHeight - d.offsetHeight;
      }
      
    • 设置元素相对浏览器窗口的位置

      node.style.left = x + "px";//设置鼠标移动后对应窗口的位置
      node.style.top = y + "px";//设置鼠标移动后对应窗口的位置
      
  • 抬起鼠标时

    document.onmouseup = function (ev) {
        document.onmousemove = null;
    }
    
node.onmousedown = function (ev) {
    var e = ev || window.event;
    var l = e.clientX - node.offsetLeft;//记录按下鼠标时,鼠标距离元素的左边位置
    var t = e.clientY - node.offsetTop;//记录按下鼠标时,鼠标距离元素的上边位置
    document.onmousemove = function (e2) {
        var e = e2 || window.event;
        var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;//获取浏览器窗口的宽度
        var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;//获取浏览器窗口的高度
        var x = e.clientX - l;//获取元素相对浏览器左边窗口的距离
        var y = e.clientY - t;//获取元素相对浏览器上边窗口的距离
        //窗口限制
        if(x < 0){//限制左边
            x = 0;
        }
        if(x >= windowWidth - d.offsetWidth){//限制右边
            x = windowWidth - d.offsetWidth;
        }
        if(y < 0){//限制上边
            y = 0;
        }
        if(y>= windowHeight - d.offsetHeight){//限制下边
            y = windowHeight - d.offsetHeight;
        }
        node.style.left = x + "px";//设置鼠标移动后对应窗口的位置
        node.style.top = y + "px";//设置鼠标移动后对应窗口的位置
    }
}
document.onmouseup = function (ev) {
    document.onmousemove = null;
}

完整源码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>rn</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            text-decoration: none;
            font-size: 26px;
            text-align: center;
        }
        .div{
            background-color: #d58512;
            width: 50px;
            height: 50px;
            position: absolute;
        }
    </style>
</head>
<body>
<div class="div" id="d1"></div>
<script>
    var d = document.getElementById("d1");
    limitDrag(d);
    function limitDrag(node) {
        node.onmousedown = function (ev) {
            var e = ev || window.event;
            var l = e.clientX - node.offsetLeft;//记录按下鼠标时,鼠标距离元素的左边位置
            var t = e.clientY - node.offsetTop;//记录按下鼠标时,鼠标距离元素的上边位置
            document.onmousemove = function (e2) {
                var e = e2 || window.event;
                var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;//获取浏览器窗口的宽度
                var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;//获取浏览器窗口的高度
                var x = e.clientX - l;//获取元素相对浏览器左边窗口的距离
                var y = e.clientY - t;//获取元素相对浏览器上边窗口的距离
                //窗口限制
                if(x < 0){//限制左边
                    x = 0;
                }
                if(x >= windowWidth - d.offsetWidth){//限制右边
                    x = windowWidth - d.offsetWidth;
                }
                if(y < 0){//限制上边
                    y = 0;
                }
                if(y>= windowHeight - d.offsetHeight){//限制下边
                    y = windowHeight - d.offsetHeight;
                }
                node.style.left = x + "px";//设置鼠标移动后对应窗口的位置
                node.style.top = y + "px";//设置鼠标移动后对应窗口的位置
            }
        }
        document.onmouseup = function (ev) {
            document.onmousemove = null;
        }
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值