前沿
最近发现公司的产品好几个模块用到了拖拽功能,之前拖拽组件是通过Html5 drag Api 实现的但体验并不是很好,顺便将原来的拖拽组建稍做修改,写一个自定义hook,方便大家使用拖拽功能。
正文
拖拽功能原理:
- 拖拽元素通过addEventListener监听器添加鼠标按下,鼠标移动,以及鼠标抬起事件。
- 再通过getBoundingClientRect() 得到拖拽元素四周相对于可拖拽区域边界的距离。
- 鼠标移动时计算x轴和y轴的移动偏移量。
- 通过element.style.transform 设置元素移动。
- 每次拖拽完成后,都将此次偏移量保存,下次再次拖拽时,可以保证位置的实时性。
代码开发:
useDrag.jsx
import { useEffect, useState } from "react";
export default function useDrag({ dragger, draggerBox = dragger, container = document.body, maring = [0, 0, 0, 0] }) {
const [translateX, setTranslateX] = useState(0);
const [translateY, setTranslateY] = useState(0);
useEffect(() => {
if (!dragger) return;
if (!draggerBox) return;
if (!container) return;
dragger = typeof dragger === 'string' ? document.querySelector(dragger) : dragger;
draggerBox = typeof draggerBox === 'string' ? document.querySelector(draggerBox) : draggerBox;
container = typeof container === 'string' ? document.querySelector(container) : container;
const { left: containerL, top: containerT, right: containerR, bottom: containerB } = container.getBoundingClientRect();
const onMouseDown = event => {
const initMouseX = event.clientX;
const initMouseY = event.clientY;
const { left: boxL, top: boxT, right: boxR, bottom: boxB } = draggerBox.getBoundingClientRect();
let deltaMouseX;
let deltaMouseY;
const onMouseMove = event => {
const moveMouseX = event.clientX;
const moveMouseY = event.clientY;
let deltaX = moveMouseX - initMouseX;
let deltaY = moveMouseY - initMouseY;
if (boxL + deltaX < containerL + maring[0]) {
deltaX = containerL + maring[0] - boxL;
}
if (boxR + deltaX > containerR - maring[1]) {
deltaX = containerR - maring[1] - boxR;
}
if (boxB + deltaY > containerB - maring[2]) {
deltaY = containerB - maring[2] - boxB;
}
if (boxT + deltaY < containerT + maring[3]) {
deltaY = containerT + maring[3] - boxT
}
deltaMouseX = deltaX + translateX;
deltaMouseY = deltaY + translateY;
draggerBox.style.transform = `translate(${deltaMouseX}px, ${deltaMouseY}px)`;
};
const onMouseUp = () => {
setTranslateX(deltaMouseX);
setTranslateY(deltaMouseY);
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseup', onMouseUp);
}
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', onMouseUp);
}
dragger.addEventListener('mousedown', onMouseDown);
return () => dragger.removeEventListener('mouseup', onMouseDown);
}, [dragger, draggerBox, container, maring])
}
使用方法:
import React from 'react';
import useDrag from '../hooks/useDrag'
import './index.less';
function Test() {
useDrag({ dragger: '.dragger', draggerBox: '.draggerBox', container: '.container', maring: [10, 10, 10, 10]})
return (
<div className='container'>
<div className='draggerBox'>
<div className='dragger'>
</div>
</div>
</div>
)
}
export default Test;
.container{
width: 800px;
height: 800px;
position: absolute;
top: 50%;
left: 50%;
margin: -400px 0 0 -400px;
border: 2px solid green;
}
.draggerBox{
width: 200px;
height: 300px;
border: 1px solid red;
}
.dragger{
width: 100%;
height: 50px;
background: blue;
}
效果展示
|