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>

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

相关推荐
用户0044729538252 小时前
练歌镜:让眼镜听懂“循环这一句”
javascript
yuyuxun14 小时前
协同过滤的校园二手物品智能推荐系统的设计与实现 毕业设计-附源码40082
javascript·vue.js·spring boot·python·数据分析·django·课程设计
总要学点什么7 小时前
第105篇:三维特效-流动图片墙体效果——让箭头沿墙长滚,它俩竟是同一份代码
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
Agent世界9 小时前
用 CDP 做发布自动化:四道机器闸门与产物验收
前端·javascript
总要学点什么9 小时前
第104篇:三维特效-流动墙体效果——让贴图沿墙流动,它却只肯走一条对角线
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
程序员大阳9 小时前
副队长ES6+教程(3)--解构赋值
前端·javascript·es6·赋值·解构
神膘护体小月半10 小时前
css - 元素背景图由 hover 触发,初次加载时闪动的解决办法
前端·javascript·css
程序员大阳10 小时前
副队长ES6+教程(2)--let 与 const
前端·javascript·es6·const·let·var
曦樂~11 小时前
【Web Development】03JavaScript 基础和事件
开发语言·前端·javascript
码云社区12 小时前
智慧物业系统选型指南:房屋档案、账单管理与报修协作
前端·javascript·vue.js·智慧物业