当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript的限制拖拽效果------JavaScript学习之路6

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

2020年07月17日  | 移动技术网IT编程  | 我要评论

限制拖拽效果

在这里插入图片描述

思想

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>

本文地址:https://blog.csdn.net/dragoned_123/article/details/107386660

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网