当前位置: 首页 > news >正文

原生js拖拽

思路:

 1.鼠标按下的时候,记录鼠标在inner的相对的位置offsetX,offsetY,并给document绑定mousemove和mouseup事件

    2.在mousemove事件中,实时计算:e.clientX - wrapRect.left - offsetX和e.clientY - wrapRect.top - offsetY

    3.在mouseup事件中,解除document的mousemove和mouseup事件

    4.添加isDrag变量,控制mousemove事件的执行

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.wrap {width: 500px;height: 500px;border: 1px solid #ccc;position: relative;}.inner {width: 100px;height: 100px;border: 1px solid red;position: absolute;left: 0;top: 0;background: #ddd;}</style>
</head><body><!-- 写一个在div里面的小的div可以自由的去移动的功能 --><div class="wrap" id="wrap"><div class="inner" id="inner"></div></div><script>const wrap = document.getElementById('wrap');const inner = document.getElementById('inner');//第一步,鼠标在inner上按下的时候,记下此时的鼠标的位置let offsetX = 0;let offsetY = 0;const wrapRect = wrap.getBoundingClientRect();const maxLeft = wrapRect.width - inner.offsetWidth;const maxTop = wrapRect.height - inner.offsetHeight;let isDrag = falseconst onMouseMove = (e) => {if (!isDrag) return//鼠标在移动的时候,计算鼠标所移动的距离 wrapRect是不会动的,所以可放在外面let left = e.clientX - wrapRect.left - offsetX;let top = e.clientY - wrapRect.top - offsetY;//边界的判断inner.style.left = Math.min(Math.max(left, 0), maxLeft) + 'px';inner.style.top = Math.min(Math.max(top, 0), maxTop) + 'px';}const onMouseUp = () => {isDrag = false;document.removeEventListener('mousemove', onMouseMove);document.removeEventListener('mouseup', onMouseUp);}inner.addEventListener('mousedown', (e) => {isDrag = true//获取鼠标在inner里面的相对的位置 getBoundingClientRect.left是相对于视口的位置,需要获取到inner的实时的位置,所以这里的inner.getBoundingClientRect需要实时获取offsetX = e.clientX - inner.getBoundingClientRect().left;offsetY = e.clientY - inner.getBoundingClientRect().top;//给document绑定事件. 如果只给inner绑定事件,当鼠标移动太快移出去了,那么会卡document.addEventListener('mousemove', onMouseMove);document.addEventListener('mouseup', onMouseUp);})</script>
</body></html>


文章转载自:

http://8Hfc0VRU.zmzdx.cn
http://45RXfG4r.zmzdx.cn
http://zHZTx3bW.zmzdx.cn
http://2anoNn92.zmzdx.cn
http://8qaeNhIA.zmzdx.cn
http://zMhBZwTL.zmzdx.cn
http://GDtiobWV.zmzdx.cn
http://UTPVU0IL.zmzdx.cn
http://HaZac42Y.zmzdx.cn
http://IFqtGbNQ.zmzdx.cn
http://oDKPD7iZ.zmzdx.cn
http://sqhVcNAp.zmzdx.cn
http://QkJizRlK.zmzdx.cn
http://7yqZJjra.zmzdx.cn
http://jW1fLBgb.zmzdx.cn
http://9364w29r.zmzdx.cn
http://kVpjOoZ6.zmzdx.cn
http://v55jrvKr.zmzdx.cn
http://p6ljFJ2U.zmzdx.cn
http://MOiTuILy.zmzdx.cn
http://C7Gx46Lm.zmzdx.cn
http://NOQRn3mg.zmzdx.cn
http://kgfjGIdQ.zmzdx.cn
http://xEzxqIdI.zmzdx.cn
http://ShFmGlBF.zmzdx.cn
http://IzT19e1c.zmzdx.cn
http://QD1gZBcM.zmzdx.cn
http://Gv8tvyNy.zmzdx.cn
http://RRZjl70G.zmzdx.cn
http://2WaUX8Lc.zmzdx.cn
http://www.dtcms.com/a/379336.html

相关文章:

  • 数据结构--Map和Set
  • P1122 最大子树和
  • 【3DV 进阶-3】Hunyuan3D2.1 训练代码详细理解之-Flow matching 训练 loss 详解
  • Python写算法基础
  • 数据结构 优先级队列(堆)
  • FunASR GPU 环境 Docker 构建完整教程(基于 CUDA 11.8)
  • 探讨:线程循环与激活(C++11)
  • 拆解格行随身WiFi多网协同模块:智能切网+马维尔芯片,如何实现5秒跨网?
  • 游泳溺水检测识别数据集:8k图像,2类,yolo标注
  • ARM裸机开发:链接脚本、进阶Makefile(bsp)、编译过程、beep实验
  • 开始 ComfyUI 的 AI 绘图之旅-Flux.1图生图之局部重绘(Inpaint)和扩图(Outpaint)(九)
  • 2025.9.11day1QT
  • ubuntu24.04+5070ti训练yolo模型(1)
  • ubuntu2204配置网桥
  • 【VLMs篇】07:Open-Qwen2VL:在学术资源上对完全开放的多模态大语言模型进行计算高效的预训练
  • Ubuntu24.04安装 Fcitx5并设置五笔字型的方法
  • 格式塔是什么?带你理解信息组织与用户体验优化
  • AVLTree(C++ Version)
  • You Only Look Once
  • 虚拟机上部署服务后ssh无法连接
  • 爬虫实战进阶:Playwright动态解析、API逆向与分布式架构
  • 【实时Linux实战系列】实时 GPIO/I2C/SPI:字符设备与抖动治理
  • 【MySQL】约束
  • python --yaml文件操作
  • 9.11网编项目——UDP网络聊天
  • 互联网“黑话”生存实用指南(100)
  • 装饰器模式:C++动态扩展游戏角色能力
  • C#线程理解
  • 2025年市场岗位专业能力认证发展指南
  • 安卓逆向(三)逆向基本环境配置