首页 > 其他分享 >一个可以拖拽缩放的div?

一个可以拖拽缩放的div?

时间:2023-11-16 17:24:51浏览次数:40  
标签:const 缩放 top 拖拽 5px div event resize left

<!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>
    .box {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 200px;
      height: 200px;
      background-color: #f0f0f0;
      cursor: move;
    }

    .resize-handle {
      position: absolute;
      width: 10px;
      height: 10px;
      background-color: #000;
    }

    .top-left {
      top: -5px;
      left: -5px;
      cursor: nw-resize;
    }

    .top {
      top: -5px;
      left: calc(50% - 5px);
      cursor: ns-resize;
    }

    .top-right {
      top: -5px;
      right: -5px;
      cursor: ne-resize;
    }

    .right {
      top: calc(50% - 5px);
      right: -5px;
      cursor: ew-resize;
    }

    .bottom-right {
      bottom: -5px;
      right: -5px;
      cursor: se-resize;
    }

    .bottom {
      bottom: -5px;
      left: calc(50% - 5px);
      cursor: ns-resize;
    }

    .bottom-left {
      bottom: -5px;
      left: -5px;
      cursor: sw-resize;
    }

    .left {
      top: calc(50% - 5px);
      left: -5px;
      cursor: ew-resize;
    }
  </style>
</head>

<body>
  <div class="box" id="drag">
    <div class="resize-handle top-left"></div>
    <div class="resize-handle top"></div>
    <div class="resize-handle top-right"></div>
    <div class="resize-handle right"></div>
    <div class="resize-handle bottom-right"></div>
    <div class="resize-handle bottom"></div>
    <div class="resize-handle bottom-left"></div>
    <div class="resize-handle left"></div>
  </div>
</body>
<script>
  const dragElement=document.getElementById("drag");
  // 拖拽功能
  dragElement.addEventListener("mousedown",startDrag);
  function startDrag (event) {
    event.preventDefault();
    const currentHandle=event.target;
    const isResizeHandle=currentHandle.className.includes("resize-handle");
    if(isResizeHandle) return;
    const startX=event.clientX;
    const startY=event.clientY;
    const startLeft=dragElement.offsetLeft;
    const startTop=dragElement.offsetTop;
    document.addEventListener("mousemove",drag);
    document.addEventListener("mouseup",stopDrag);
    function drag (event) {
      const dx=event.clientX-startX;
      const dy=event.clientY-startY;
      const newLeft=startLeft+dx;
      const newTop=startTop+dy;
      dragElement.style.left=newLeft+"px";
      dragElement.style.top=newTop+"px";
    }
    function stopDrag () {
      document.removeEventListener("mousemove",drag);
      document.removeEventListener("mouseup",stopDrag);
    }
  }
  // 缩放功能
  const resizeHandles=document.getElementsByClassName("resize-handle");
  Array.from(resizeHandles).forEach((handle) => {
    handle.addEventListener("mousedown",startResize);
  });

  function startResize (event) {
    event.preventDefault();
    const currentHandle=event.target;
    const direction=currentHandle.className.split(" ")[1];
    const startX=event.clientX;
    const startY=event.clientY;
    const startWidth=dragElement.offsetWidth;
    const startHeight=dragElement.offsetHeight;
    const startLeft=dragElement.offsetLeft;
    const startTop=dragElement.offsetTop;
    document.addEventListener("mousemove",resize);
    document.addEventListener("mouseup",stopResize);
    function resize (event) {
      const dx=event.clientX-startX;
      const dy=event.clientY-startY;
      let width=startWidth,
        height=startHeight,
        left=startLeft,
        top=startTop;
      if(direction.includes("left")) {
        width=startWidth-dx+"px";
        left=startLeft+dx/2+"px";
      }
      if(direction.includes("right")) {
        width=startWidth+dx+"px";
        left=startLeft+dx/2+"px";
      }
      if(direction.includes("top")) {
        height=startHeight-dy+"px";
        top=startTop+dy/2+"px";
      }
      if(direction.includes("bottom")) {
        height=startHeight+dy+"px";
        top=startTop+dy/2+"px";
      }
      if(parseInt(width)<=0||parseInt(height)<=0) return;
      dragElement.style.width=width;
      dragElement.style.height=height;
      dragElement.style.left=left;
      dragElement.style.top=top;
    }
    function stopResize () {
      document.removeEventListener("mousemove",resize);
      document.removeEventListener("mouseup",stopResize);
    }
  }
</script>

</html>

 

 


作者:JYeontu
链接:https://juejin.cn/post/7282951856514793513
来源:稀土掘金

 

标签:const,缩放,top,拖拽,5px,div,event,resize,left
From: https://www.cnblogs.com/chenzxl/p/17836796.html

相关文章

  • uniapp微信小程序图片裁剪插件,支持自定义尺寸、定点等比例缩放、拖动、图片翻转、剪切
    uniapp微信小程序图片裁剪插件,支持自定义尺寸、定点等比例缩放、拖动、图片翻转、剪切圆形/圆角图片、定制样式,功能多性能高体验好注释全。1.效果预览:      5.引入插件项目代码:Homilier/qf-image-cropper·GitCode使用HBuilderX导入项目:图片裁剪插件-DCloud......
  • C++ signal(SIGFPE,handler) ignore division by 0 exception
    #include<stdexcept>#include<chrono>#include<csetjmp>#include<ctime>#include<fstream>#include<iostream>#include<iomanip>#include<signal.h>#include<sstream>#include<thread>#incl......
  • 【LGR-166-Div.4】洛谷入门赛17
    【LGR-166-Div.4】洛谷入门赛#17比赛地址这次是div4的难度,整体不算是很难,很适合小白玩家[10月入门赛-A]食堂题目描述为了给师生提供良好的用餐体验,洛谷小学的食堂坚持现炒、现做每一道菜肴。洛谷小学一共有\(a\)名老师和\(b\)名学生。食堂的营养师为每位师生的用餐进......
  • ElementUI的Dialog弹窗实现拖拽移动功能
    在项目中使用el-dialog中发现不能够拖拽移动,因此网上找了相关资料,使用自定义指令实现拖拽功能。1、创建自定义指令:新建文件directive/el-drag-dialog/index.jsimportdragfrom"./drag";constinstall=function(Vue){Vue.directive("el-drag-dialog",drag);};if(wind......
  • CF906 div2
    CF906div2A.Doremy'sPaint3题意给出一个序列,可以随意打乱顺序,问最后能否使得所有相邻两个元素的和相等。数据范围多组数据,\(2<=n<=100,1<=a_i<=10^5\)样例输入52893112411455233334100000100000100000100000样例输出YesYesNoNo......
  • VMware(Windows系统)不能复制粘贴拖拽
    1、先去把网上常见的方法看一遍,都不行。你再来试下面这个。2、wen.bat,通过共享文件放到虚拟机上,进行安装: 去百度网盘捞。链接:https://pan.baidu.com/s/1yokse5BUQ-by9azPGU_sJw?pwd=a123提取码:a123 3、然后就成功了。  共享文件: 本机电脑的IP,通过cmd去查ipco......
  • sortablejs拖拽功能
    官网:http://www.sortablejs.com/中文文档:https://www.itxst.com/sortablejs/neuinffi.htmlgithub地址如下:https://github.com/SortableJS/react-sortablejsimportReact,{FC,useState}from'react';import{ReactSortable}from'react-sortablejs';......
  • Codeforces Round 907 (Div. 2)
    \(A.SortingwithTwos\)https://codeforces.com/contest/1891/submission/232689614\(B.DejaVu\)https://codeforces.com/contest/1891/submission/232690141\(C.SmiloandMonsters\)https://codeforces.com/contest/1891/submission/232691988\(D.Suspic......
  • react使用react-draggable制作可拖拽弹框
    安装yarnaddreact-draggable使用importDraggablefrom'react-draggable';exportdefaultfunctionTableModal(){const[bounds,setBounds]=useState({left:0,top:0,bottom:0,right:0,});constdraggleRef=useRef(null)......
  • Codeforces Round 809 (Div. 2) D1. Chopping Carrots (Easy Version) 题解
    题意CodeforcesRound809(Div.2)D1.ChoppingCarrots(EasyVersion)给两个整数\(n,k\),一个数组\(a\),要求构造一个同样长度的数组\(p\),使得\(\max\limits_{1\lei\len}\left(\left\lfloor\frac{a_i}{p_i}\right\rfloor\right)-\min\limits_{1\lei\l......