JS-拖拽位移、放大缩小

对同一盒子拖拽位移、缩放,这其实是不符合js的逻辑的,位移和拖拽必然会互相影响,所以需要在布局上略加调整

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .resize-box {
      width: 300px;
      height: 300px;
      position: absolute;
      left: 50px;
      top: 50px;
      cursor: move;
      background-color: #ccc;
      border: 1px solid #000;
    }
    .resize {
      width: 20px;
      height: 20px;
      background-color: #000;
      position: absolute;
      right: 0;
      bottom: 0;
      z-index: 100;
      cursor: se-resize;
    }
  </style>
</head>
<body>
<div class="resize-box">
  <div class="resize"></div>
</div>
<script>
  const resizeBox = document.querySelector('.resize-box');
  const resize = document.querySelector('.resize');
  let isResizing = false;
  let startX, startY, startWidth, startHeight;
  resize.onmousedown = function(e) {
    preventFun(e);
    isResizing = true;
    startX = e.clientX;
    startY = e.clientY;
    startWidth = parseInt(getComputedStyle(resizeBox).width, 10);
    startHeight = parseInt(getComputedStyle(resizeBox).height, 10);

    document.onmousemove = function(e) {
      preventFun(e);
      const deltaX = e.clientX - startX;
      const deltaY = e.clientY - startY;
      resizeBox.style.width = (startWidth + deltaX) + 'px';
      resizeBox.style.height = (startHeight + deltaY) + 'px';
    };
    document.onmouseup = closeDragElement;
  };

  function dragElement(elmnt) {
    var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
    elmnt.onmousedown = dragMouseDown;
    function dragMouseDown(e) {
      preventFun(e);
      e.preventDefault();
      e = e || window.event;
      pos3 = e.clientX;
      pos4 = e.clientY;
      document.onmousemove = elementDrag;
      document.onmouseup = closeDragElement;
    }

    function elementDrag(e) {
      preventFun(e);
      e = e || window.event;
      pos1 = pos3 - e.clientX;
      pos2 = pos4 - e.clientY;
      pos3 = e.clientX;
      pos4 = e.clientY;
      elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
      elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
    }
  }

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  }

  dragElement(resizeBox);

  function preventFun(e) {
    // 阻止默认事件
    e.preventDefault();
    e.returnValue;
    // 阻止冒泡
    if (e && e.stopPropagatio) {
      e.stopPropagation();
    } else {
      window.event.cancelBubble = true;
    }
  }

</script>
</body>
</html>

位移还是通过自身来实现,缩放则是通过加一个盒子间接操作,这样互相之间不会冲突,当然还要加上去除冒泡 和 默认事件,这样就可以了

相关推荐
To_OC5 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
labixiong7 小时前
实现一个能跑的迷你版Promise(一)
前端·javascript·面试
weedsfly11 小时前
还在用 Axios?你可能需要重新理解 XHR 与 Fetch
前端·javascript·面试
CoderWeen11 小时前
从零实现一个 Vue3 流程图编辑器:节点拖拽、贝塞尔连线与框选
前端·javascript
To_OC1 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
kyriewen1 天前
我用 50 行代码重写了 React Router 核心,终于搞懂了前端路由原理
前端·javascript·react.js
Asize1 天前
HTML5 Canvas 基础:从按帧动画到 ECharts 数据可视化
前端·javascript·canvas
默_笙1 天前
🎄 后端给我一堆扁平数据,我 10 行代码把它变成了树
前端·javascript
前端Hardy1 天前
又一个 AI 神器火了!
前端·javascript·后端
PBitW1 天前
GPT训练我的第二天,我表示不过如此!!!😕😕😕
前端·javascript·面试