React实现拖拽改变宽度

插件

使用了第三方插件 re-resizable用React组件可以很便捷的实现可拖拽改变组件宽度高度。
安装方法
yarn add re-resizable

npm install re-resizable

属性
参数说明类型
defaultSize初始默认宽高string / number
minWidth宽度下限string / number
minHeight高度下限string / numberr
maxWidth宽度上限string / number
maxHeight高度上限string / number
bounds指定边界大小window / parent / HTMLElement
示例
import React, {Component} from 'react';
import { Resizable } from "re-resizable";

export default class Demo extends Component {

    render() {
        return (
            <Resizable
                style={{background: "#8cdbd5"}}
                defaultSize={{width:320, height:200}}
              
            >
               111111111
            </Resizable>
        );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值