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

(含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现

原生写法

// 封装组件
import React, { useState, useRef } from 'react';const DraggableModal = ({ children }) => {const [position, setPosition] = useState({ x: 0, y: 0 });const modalRef = useRef(null);const handleMouseDown = (e) => {const modal = modalRef.current;const startX = e.clientX - modal.offsetLeft;const startY = e.clientY - modal.offsetTop;const handleMouseMove = (e) => {setPosition({x: e.clientX - startX,y: e.clientY - startY});};const handleMouseUp = () => {document.removeEventListener('mousemove', handleMouseMove);document.removeEventListener('mouseup', handleMouseUp);};document.addEventListener('mousemove', handleMouseMove);document.addEventListener('mouseup', handleMouseUp);};return (<divref={modalRef}style={{ position: 'absolute', left: position.x, top: position.y }}><div style={{ cursor: 'move' }} onMouseDown={handleMouseDown}>Drag Me</div>{children}</div>);
};export default DraggableModal;// 使用
<DraggableModal><ModalContent />
</DraggableModal>

使用库并处理拖动和点击事件重叠问题

// 使用库实现功能
你可以使用react-draggable库来实现这个功能。首先安装react-draggable库:```
npm install react-draggable
```然后在你的组件中引入react-draggable库,并使用它来包裹你想要添加可拖动功能的按钮:```jsx
import React from 'react';
import Draggable from 'react-draggable';const DraggableButton = () => {let isDragging = false;const handleDrag = () => {isDragging = true;};const handleStop = () => {console.log('handleStop');// 触发 onStop 后会触发 onClick,加个 setTimeout 是为了让修改不“马上”发生,导致 handleClick 拿到的不是预期的结果setTimeout(() => (isDragging = false), 0);};const handleClick = () => {console.log('handleClick');if (isDragging) {return;}console.log('啊,我被 click 了');};return (<Draggable onDrag={handleDrag} onStop={handleStop}><button onClick={handleClick} style={{ position: 'fixed', bottom: 0, right: 0 }}>可拖动按钮</button></Draggable>);
}export default DraggableButton;

解决库和Antd Tooltip配置使用无法拖动

可以尝试使用`react-draggable`的`handle`属性来指定拖动的句柄,避免事件冲突。
示例代码如下:<Draggable handle=".drag-handle"><div><Button className="drag-handle">Hover me<Tooltip title="Tooltip text"><QuestionCircleOutlined /></Tooltip></Button></div>
</Draggable>

移动端或PC端如何判断react-draggable包裹的组件时点击还是移动(含移动位置判断),用reack hook代码实现

// 判断是否点击还是移动import React, { useState } from 'react';
import Draggable from 'react-draggable';const DraggableComponent = () => {const [isDragging, setIsDragging] = useState(false);const handleTouchStart = (e) => {setIsDragging(false);};const handleTouchMove = (e) => {setIsDragging(true);};const handleTouchStop = () => {if (!isDragging) {// 没在拖动}settimeout(() => {setIsDragging(false)}, 0)}return (<DraggableonStart={handleTouchStart}onDrag={handleTouchMove}onStop={handleTouchStop}><div style={{ width: '100px', height: '100px', backgroundColor: 'lightblue' }}>Drag me</div></Draggable>);
};export default DraggableComponent;// 下方是判断移动位置import React, { useState } from 'react';
import Draggable from 'react-draggable';const DraggableComponent = () => {const [isDragging, setIsDragging] = useState(false);const [startX, setStartX] = useState(null);const [startY, setStartY] = useState(null);const handleTouchStart = (e) => {setStartX(e.touches[0].clientX);setStartY(e.touches[0].clientY);setIsDragging(false);};const handleTouchMove = (e) => {const moveX = e.touches[0].clientX;const moveY = e.touches[0].clientY;if (Math.abs(moveX - startX) > 5 || Math.abs(moveY - startY) > 5) {setIsDragging(true);}};const handleTouchStop = () => {setIsDragging(false)}return (<DraggableonStart={handleTouchStart}onDrag={handleTouchMove}onStop={handleTouchStop}><div style={{ width: '100px', height: '100px', backgroundColor: 'lightblue' }}>Drag me</div></Draggable>);
};export default DraggableComponent;

http://www.lryc.cn/news/327649.html

相关文章:

  • 选择最佳图像处理工具OpenCV、JAI、ImageJ、Thumbnailator和Graphics2D
  • 微信小程序版本更新检测
  • 【每日力扣】343. 整数拆分与63. 不同路径 II
  • 洛谷 Cut Ribbon
  • #AS,idea,maven,gradle
  • FPGA结构与片上资源
  • 【分布式】——分布式事务
  • 第6章:“让我们思考这个”的提示
  • 安卓Activity上滑关闭效果实现
  • 使用conda管理python环境
  • MR混合现实情景实训教学系统在军事演练课堂中的教学应用
  • vant checkbox 复选框 样式改写
  • 物联网实战--入门篇之(一)物联网概述
  • 将yolov5s部署到安卓上实战经验总结
  • 算法日记————对顶堆(4道题)
  • 【I.MX6ULL移植】Ubuntu-base根文件系统移植
  • unity3d for web
  • 大宋咨询(深圳问卷调研)关于消费者研究的流程
  • STM32看似无法唤醒的一种异常现象分析
  • iOS - Runtime-isa详解(位域、union(共用体)、位运算)
  • 使用VSCode搭建Vue 3开发环境
  • 深度学习中的模型蒸馏技术:实现流程、作用及实践案例
  • Java服务运行在Linux----维护常用命令
  • 夜晚水闸3D可视化:科技魔法点亮水利新纪元
  • 从零开始的软件开发实战:互联网医院APP搭建详解
  • 【深度学习】YOLO检测器的发展历程
  • C语言--编译和链接
  • 实现使用C#代码完成wifi的切换和连接功能
  • Mac添加和关闭开机应用
  • QT QInputDialog弹出消息框用法