Day 180/200 react-draggable 实现组件拖拽功能

20 篇文章 0 订阅

1、需求

想实现页面弹窗的拖拽功能,原生手写时,基本逻辑能实现,但是拖拽双击操作时,会有bug,待解决,就想着看看有没有合适的组件,发现还真有~

2、介绍

周下载量300w+

image.png

2.1支持有各种拖拽情况的Demo实现

image.png

2.2支持React16.3+

image.png

2.3 基本代码
<Draggable>
  <div>I can now be moved around!</div>
</Draggable>

3、Demo样例

image.png

// Copyright 2021 zhaoarden
import React from 'react';
import Draggable from 'react-draggable'; // The default
import { Modal,Button } from 'antd';
import "antd/dist/antd.css";
class Dragable extends React.Component {
    constructor(props) {
        super(props);
        this.state={
            visible: false
        }
    }
    componentDidMount(){
    };
    render() {
        const {visible}=this.state
        const clickName=()=>{
            this.setState({visible:true});
            console.log('clickName');
        }
        const handleOk = () => {
            this.setState({visible:false});
        };
        const handleCancel = () => {
            this.setState({visible:false});
        };
        return <div>
            <h1>Learn, {this.props.name}</h1>
            <Button type="primary"  onClick={clickName}>Primary Button</Button>
            <Modal title="Basic Modal" visible={visible} onOk={handleOk} onCancel={handleCancel}>
                <Draggable>
                    <div>I can now be moved around!</div>
                </Draggable>
            </Modal>
        </div>;
    }
}
export default Dragable;

可运行的Demo的GitHub仓库
https://github.com/iguoxing/react-ts

参考资料

https://www.npmjs.com/package/react-draggable

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值