react实现拖拽功能

import React, { Component } from 'react';
import './index.css'
class DragBox extends Component {
  constructor(props) {
    super(props);
    this.state = {
        left: 0,
        top: 0,
        isDragging: false,
        startX: 0,
        startY: 0
    };
  }

  handleMouseDown = (e) => {
    this.setState({
        isDragging: true,
        startX: e.clientX,
        startY: e.clientY
    });
  }

  handleMouseMove = (e) => {
    if (this.state.isDragging) {
        const offsetX = e.clientX - this.state.startX;
        const offsetY = e.clientY - this.state.startY;
        this.setState({
            left: this.state.left + offsetX,
            top: this.state.top + offsetY,
            startX: e.clientX,
            startY: e.clientY
        });
    }
  }

  handleMouseUp = (e) => {
    this.setState({
        isDragging: false
    });
  }

  render() {
    return (
        <div className="drag-box"
             style={{ left: this.state.left + 'px', top: this.state.top + 'px' ,position:'absolute'}}
             onMouseDown ={this.handleMouseDown} //鼠标按下事件
             onMouseMove={this.handleMouseMove} //鼠标移动事件
             onMouseUp={this.handleMouseUp} //鼠标抬起事件
             > 
            Drag me!
        </div>
    );
  }
}

export default DragBox;

onMouseDown //鼠标按下事件
onMouseMove //鼠标移动事件
onMouseUp //鼠标抬起事件

主要的实现思路是:鼠标移动到最后的左边距和上边距-鼠标按下的左边距和上边距然后给元素一个定位给它的left和top赋值就可实现

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React Antd提供了一个Draggable组件,可以实现拖拽组件的功能。 首先,需要安装antd组件库和react-dnd库: ```bash npm install antd react-dnd react-dnd-html5-backend ``` 然后,在组件中引入Draggable组件和DragDropContext组件: ```jsx import { Draggable } from 'react-drag-and-drop'; import { DragDropContext } from 'react-dnd'; import HTML5Backend from 'react-dnd-html5-backend'; import { Card } from 'antd'; const DragCard = ({ id, text, index, moveCard }) => { const style = { marginBottom: 16, }; return ( <Draggable key={id} draggableId={id} index={index} onDragEnd={(result) => moveCard(result.source.index, result.destination?.index)} > {(provided) => ( <Card {...provided.draggableProps} {...provided.dragHandleProps} ref={provided.innerRef} style={{ ...provided.draggableProps.style, ...style }} > {text} </Card> )} </Draggable> ); }; const DragList = ({ cards, moveCard }) => { return ( <div> {cards.map((card, index) => ( <DragCard key={card.id} index={index} {...card} moveCard={moveCard} /> ))} </div> ); }; const App = () => { const [cards, setCards] = useState([ { id: 'card-1', text: 'Card 1' }, { id: 'card-2', text: 'Card 2' }, { id: 'card-3', text: 'Card 3' }, ]); const moveCard = useCallback((sourceIndex, destinationIndex) => { if (destinationIndex === undefined) { return; } const newCards = [...cards]; const [removed] = newCards.splice(sourceIndex, 1); newCards.splice(destinationIndex, 0, removed); setCards(newCards); }, [cards]); return ( <DragDropContext backend={HTML5Backend}> <DragList cards={cards} moveCard={moveCard} /> </DragDropContext> ); }; ``` 在上述代码中,Draggable组件用于包裹需要拖拽的组件,而DragDropContext组件用于提供拖拽功能的上下文。通过onDragEnd回调函数,可以获取到拖拽的源位置和目标位置,然后更新数据源的位置。在拖拽的过程中,可以通过provided参数传递组件的属性,如样式等。 最后,将DragList组件渲染到页面中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱闯~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值