js 拖拽

作者: Do_Du | 来源:发表于2020-03-31 15:23 被阅读0次

e.clientX:指鼠标距离浏览器左边的距离
e.clientY:指鼠标距离浏览器上边的距离

div.offsetTop: div元素到距离浏览器可视化窗口上边的距离
div.offsetLeft: div元素到距离浏览器可视化窗口左边的距离

wps2.jpeg wps1.jpeg

如果要设置物体拖拽,那么必须使用三个事件,并且这三个事件的使用顺序不能颠倒。

1、onmousedown:鼠标按下事件

2、onmousemove:鼠标移动事件

3、onmouseup:鼠标抬起事件

注:

1、一定要绝对定位

2、onmousedown绑定拖拽的元素,onmousemove和onmouseup是针对document的绑定,因为移动的是整个div

3、计算

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  #box {
    width: 400px;
    height: 150px;
    background: purple;
    position: absolute;
    cursor:move;
  }
</style>
<body>
  <div id="box"></div>
  <script>
    window.onload = function () {
      var box = document.getElementById('box');

      box.onmousedown = function (e) {
        e = event || window.event;
        var diffX = e.clientX - box.offsetLeft;
        var diffY = e.clientY - box.offsetTop;
        
         //修复低版本ie bug
        if(typeof box.setCapture !== 'undefined'){
          box.setCapture(); 
        }

        document.onmousemove = function (e) {
          e = event || window.event;
          var moveX = event.clientX - diffX;
          var moveY = event.clientY - diffY;

          // 边界判断
          if (moveX < 0) {
            moveX = 0;
          }else if (moveX > window.innerWidth - box.offsetWidth){
            moveX = window.innerWidth - box.offsetWidth;
          }
          if (moveY < 0) {
            moveY = 0;
          }else if (moveY > window.innerHeight - box.offsetHeight) {
            moveY = window.innerHeight - box.offsetHeight;
          }
          // 关键性赋值绝对定位top left距离
          box.style.left = moveX + 'px';
          box.style.top = moveY + 'px';
        }

        // 鼠标抬起,清楚绑定再文档上的mousemove和mouseup事件,否则鼠标抬起后还可以继续拖拽方法  
        document.onmouseup = function (e) {
          e = event || window.event;
          this.onmousemove = null;
          this.onmouseup = null;

           //修复低版本ie bug
          if(typeof box.releaseCapture!='undefined'){  
            box.releaseCapture();  
          }  
        }
      }
    } 
  </script>
</body>
</html>

// 转自:https://blog.csdn.net/weixin_41910848/article/details/82218243

相关文章

  • js拖拽html元素工具类

    复制就能用的拖拽js方法 原生js实现拖拽元素方法 使用(注意拖拽目标元素需绝对定位 position: 'abs...

  • 拖拽

    一、JS拖拽JS里拖拽三事件, onmousedown onmousemove onmouseup 是实现交互性效...

  • 原生js实现拖拽(碰撞检测)

    js实现拖拽

  • js实现拖拽

    ①鼠标按下+鼠标移动 → 拖拽②鼠标松开 → 无拖拽③鼠标偏移 → 拖拽距离 js实现 ① onmousedown...

  • h5拖拽相关事件

    拖拽 Sortable.js插件拖拽的时候主要由这几个事件完成,

  • vue 试卷拖动题号改变顺序

    一、template模版` 二、js部分 拖拽` `

  • jquery html5 Sortable.js 拖拽排序源码分

    最近公司项目经常用到一个拖拽 Sortable.js插件,所以有空的时候看了 Sortable.js 源码拖拽的时...

  • js拖拽

    (1)原生js实现checkbox全选

  • js拖拽

    开发了那么久,对于js实现拖拽多少都写过,用于实际项目却没有。 先看一下之前写的: 如果鼠标慢慢移动,拖拽是没有任...

  • js 拖拽

    1:鼠标在元素上的坐标位置 offsetX offsetY 2:鼠标在浏览器(可视窗口)上的坐标位置 client...

网友评论

      本文标题:js 拖拽

      本文链接:https://www.haomeiwen.com/subject/stwwuhtx.html