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

可以拖拽缩放的div

时间:2023-12-08 21:06:11浏览次数:35  
标签:const 缩放 top 拖拽 5px div event resize left

一、HTML代码

<!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>
    <link rel="stylesheet" href="./index.css" />
  </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 src="./index.js"></script>
</html>

二、CSS代码

.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;
}

三、JS代码

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);
  }
}

标签:const,缩放,top,拖拽,5px,div,event,resize,left
From: https://blog.51cto.com/u_15997490/8741541

相关文章

  • trans.bat 将.m4a 文件拖拽到这个上面 自动转换成.mp3 老歌精选-歌曲z
    @chcp65001>nulechooff::获取文件名SETfilePath=%1::因为这里目录的路径是E:\老歌精选-歌曲z是11个字符,所以是从第12个字符到最后4个字符中间,就是文件名SETfileName=%filePath:~12,-4%@if"%1"==""(echo"未检测到文件拖拽操作。")else(echo发现......
  • 浅谈WPF之控件拖拽与拖动
    使用过office的visio软件画图的小伙伴都知道,画图软件分为两部分,左侧图形库,存放各种图标,右侧是一个画布,将左侧图形库的图标控件拖拽到右侧画布,就会生成一个新的控件,并且可以自由拖动。那如何在WPF程序中,实现类似的功能呢?今天就以一个简单的小例子,简述如何在WPF中实现控件的拖拽和拖......
  • vue-quill富文本编辑器实现图片缩放
    安装环境官网:https://vueup.github.io/vue-quill/guide/modules.htmlnpm安装:npminstall@vueup/vue-quill@latest--savepnpm安装:pnpmadd@vueup/vue-quill@latest安装模块quill-blot-formatternpm:npminstall--savequill-blot-formatterpnpm:npmaddquill-blot-form......
  • [ABC254Ex] Multiply or Divide by 2
    [ABC254Ex]MultiplyorDivideby2题意:给定大小为$n$的集合$A$和$B$,你可以对集合$A$中的元素$a_i$进行两种操作,分别为$a_i\leftarrow\lfloor\dfrac{a_i}{2}\rfloor$,和$a_i\leftarrowa_i\times2$。你需要操作集合$A$直至集合$A,B$完......
  • react antd table react-sortable-hoc DraggableBodyRow 拖拽及禁用指定行拖拽
    原文地址:基于antd树形表格table的拖拽排序效果实现-掘金(juejin.cn)思路片段:constDraggableBodyRow:React.FC<any>=({className,style,...restProps})=>{constindex=customInfoList.findIndex(({order:_index})=>_index===restProps['data-r......
  • Codeforces Round 913 (Div. 3)
    CodeforcesRound913(Div.3)比赛链接ROOK题目思路:我没有下过国际象棋,但这个题跟国际象棋真是没有一点关系,就是一个简单的输出代码:#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){//intn;strings;cin>>s;in......
  • Codeforces Round 913 (Div. 3)
    CodeforcesRound913(Div.3)div3两题新纪录..我再也不喝完酒打cf了A.Rook#include<bits/stdc++.h>//#defineintlonglong#defineendl'\n'usingnamespacestd;intboard[10][10];voidsolve(){strings;cin>>s;intx=s[0]-&#......
  • Educational Codeforces Round 158 (Rated for Div. 2)
    Preface补题,妈的现在EduE都做不来这搞毛啊A.LineTrip签到#include<cstdio>#include<iostream>#include<utility>#include<vector>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<queue&......
  • Codeforces Round 913 (Div. 3)(A~F)
    A.Rook题意:在一个国际象棋棋盘中,横坐标为a-h,纵坐标为1-8,字母在前,数字在后,输入一个棋子的位置,输出该棋子所在的行与列中非棋子本身位置的所有位置。分析:模拟。代码:#include<iostream>#include<algorithm>usingnamespacestd;typedeflonglongll;constintN=2e5......
  • Codeforces Round 912 (Div. 2)
    Preface这场题莫名很对我胃口,因为F是个究极套路题,还是我最拿手的2-SAT,想+写不到半小时就搞定了然后E的核心思想和上周末VP的一场省赛的题一样,因此看一眼就会了唯一抽象的是D2要用对超集的sosdp,由于之前没写过就不知道还能这么搞A.HalloumiBoxes当\(k\ge2\)时,我们总可以通......