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>

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

相关推荐
qq_5896660519 分钟前
TypeScript 完整入门教程
前端·javascript·typescript
星栈独行2 小时前
翻完 Pi 源码:它和 Codex、Claude Code 有何不同
开发语言·javascript·人工智能·程序人生
落落Plus3 小时前
浏览器缓存机制详解
javascript·缓存·浏览器缓存
浅水壁虎5 小时前
vue基础(第二章 )
前端·javascript·vue.js
界面开发小八哥5 小时前
界面控件DevExtreme v26.1新版亮点——支持Angular 22
前端·javascript·angular.js·devexpress·ui开发·devextreme
mONESY6 小时前
React useState 核心原理与最佳实践:从异步更新到性能优化全解
javascript
腻害兔6 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:CRM 客户关系模块深度解析——从线索到回款,一套完整的 B2B 销售闭环是怎么搭的?
java·前端·javascript·vue.js·产品经理·ai编程
gis开发之家7 小时前
《Vue3 从入门到大神37篇》Vue3 源码详解(七):watch 与 watchEffect 源码对比——副作用是如何被追踪的?
javascript·前端框架·vue3·vue3源码
bloglin999998 小时前
langchain 和 langgraph 和 react
javascript·react.js·langchain
触底反弹8 小时前
🚀 删了数据刷新又回来?3 组件 × 4 回调 × 3 坑讲透 React 父子通信
前端·javascript·react.js