手写可拖曳DIV

87 阅读1分钟

要点:

  1. 注意监听范围,不能只监听div
  2. 不要使用drag事件,很难用
  3. 使用transform会比top/left性能更好,因为可以避免reflow和repaint
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #xxx {
        width: 50px;
        height: 50px;
        border: 1px solid red;
        position: absolute;
      }
    </style>
  </head>
  <body>
    <div id="xxx"></div>
    <script>
      const xxx = document.querySelector('#xxx')
      let dragging = false // 拖曳状态确认
      let position = null // 鼠标当前位置记录
      xxx.addEventListener('mousedown', (e) => {
        dragging = true //记录当前状态为拖曳
        position = [e.clientX, e.clientY] // 记录当前鼠标位置
      })

      document.addEventListener('mousemove', function (e) {
        if (dragging === false) {
          return
        } // 如果不处于拖曳状态则退出
        const x = e.clientX
        const y = e.clientY
        const deltaX = x - position[0] // 计算移动中的鼠标和原始坐标的差值
        const deltaY = y - position[1]
        const left = parseInt(xxx.style.left || 0) //获得div本身的left和top
        const top = parseInt(xxx.style.top || 0)
        xxx.style.left = left + deltaX + 'px' //赋值给新的left和top
        xxx.style.top = top + deltaY + 'px'
        position = [x, y] //记录新的鼠标位置
      })
      document.addEventListener('mouseup', function (e) {
        dragging = false // 鼠标放开时候,取消拖曳状态
      })
    </script>
  </body>
</html>