码迷,mamicode.com
首页 > Web开发 > 详细

js 实现简单的拖拽

时间:2018-11-10 21:12:23      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:htm   clientx   list   demo1   ali   doc   back   ros   space   

 

 

步骤

 

使用 JavaScript 实现拖拽的步骤:

  • 让元素捕获事件(mousedown, mousemove & mouseup)
  • 单击并不释放,触发 mousedown,标记开始拖拽,并获取元素和鼠标的位置
  • 拖动鼠标,触发 mousemove,不断的获取鼠标的位置,并通过计算重新确定元素的位置
  • 释放师表,触发 mouseup,结束拖拽,确定元素位置并更新

 

代码1:

demo1.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #block {
            width: 200px;
            height: 200px;
            background-color: yellow;
            position: absolute;
        }
    </style>
</head>
<body>
<div id="block"></div>
<script>
    /**
     * 拖动元素
     * @param elementId 元素id
     */
    function drag(elementId) {
        var element = document.getElementById(elementId);
        var position = {
            offsetX: 0, //点击处偏移元素的X
            offsetY: 0, //偏移Y值
            state: 0 //是否正处于拖拽状态,1表示正在拖拽,0表示释放
        };
        //获得兼容的event对象
        function getEvent(event) {
            return event || window.event;
        }
        //元素被鼠标拖住
        element.addEventListener(‘mousedown‘, function (event) {
            //获得偏移的位置以及更改状态
            var e = getEvent(event);
            position.offsetX = e.offsetX;
            position.offsetY = e.offsetY;
            position.state = 1;
        }, false);
        //元素移动过程中
        document.addEventListener(‘mousemove‘, function (event) {
            var e = getEvent(event);
            if (position.state) {
                position.endX = e.clientX;
                position.endY = e.clientY;
                //设置绝对位置在文档中,鼠标当前位置-开始拖拽时的偏移位置
                element.style.position = ‘absolute‘;
                element.style.top = position.endY - position.offsetY + ‘px‘;
                element.style.left = position.endX - position.offsetX + ‘px‘;
            }
        }, false);
        //释放拖拽状态
        element.addEventListener(‘mouseup‘, function (event) {
            position.state = 0;
        }, false);
    }
    drag(‘block‘);
</script>
</body>
</html>

 

 

 

 

demo2.html 
<!DOCTYPE html>
<html lang="en">
<body>
<div id="box" class="box" style="width: 100px;height: 100px; position: absolute;background: red;cursor: move;"></div>
<script>
(function () {
  var dragging = false;
  var boxX, boxY, mouseX, mouseY, offsetX, offsetY;
  var box = document.getElementById(‘box‘);
  box.onmousedown = down;
  document.onmousemove = move;
  document.onmouseup = up;
  function down(e) {
    dragging = true;
    boxX = box.offsetLeft;
    boxY = box.offsetTop;
    mouseX = parseInt(getMouseXY(e).x);
    mouseY = parseInt(getMouseXY(e).y);
    offsetX = mouseX - boxX;
    offsetY = mouseY - boxY;
  }
  function move(e) {
    if (dragging) {
      var x = getMouseXY(e).x - offsetX;
      var y = getMouseXY(e).y - offsetY;
      var width = document.documentElement.clientWidth - box.offsetWidth;
      var height = document.documentElement.clientHeight - box.offsetHeight;
      x = Math.min(Math.max(0, x), width);
      y = Math.min(Math.max(0, y), height);
      box.style.left = x + ‘px‘;
      box.style.top = y + ‘px‘;
    }
  }
  function up(e) {
    dragging = false;
  }
  function getMouseXY(e) {
    var x = 0, y = 0;
    e = e || window.event;
    if (e.pageX) {
      x = e.pageX;
      y = e.pageY;
    } else {
      x = e.clientX + document.body.scrollLeft - document.body.clientLeft;
      y = e.clientY + document.body.scrollTop - document.body.clientTop;
    }
    return {
      x: x,
      y: y
    };
  }
})()
</script>
</body>
</html>

 

js 实现简单的拖拽

标签:htm   clientx   list   demo1   ali   doc   back   ros   space   

原文地址:https://www.cnblogs.com/taobr/p/9940421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!