react-draggable实现拖拽

安装react-draggable插件

npm install react-draggable

设置无限制的拖动范围

创建jsx文件

import React from "react";

// 引入样式

import './index.css'

// 引入拖拽插件

import Draggable from 'react-draggable';

export default function Tuo() {

  return (

    <div>

      <div className="app">

        <Draggable>

          <div className="box1">我可以随意移动box1</div>

        </Draggable>

        <Draggable>

          <div className="box2">我可以随意移动box2</div>

        </Draggable>

      </div>

    </div>

  );

}

引入css样式

.app {

  background-color: red;

  width: 800px;

  height: 500px;

  margin: auto;

  position: relative;

}

.box1 {

  background-color: rgb(127, 192, 127);

  width: 300px;

  height: 300px;

}

.box2 {

  background-color: rgb(142, 200, 107);

  width: 300px;

  height: 300px;

}

.box3 {

  background-color: rgb(112, 148, 194);

  width: 300px;

  height: 300px;

}

.box4 {

    background-color: rgb(174, 103, 160);

    width: 300px;

    height: 300px;

}

设置拖拽的范围

在父级内移动 

import React from "react";

// 引入样式

import "./index.css";

// 引入拖拽插件

import Draggable from "react-draggable";

export default function Tuo() {

  return (

    <div>

      <div className="app">

        {/* 限制范围*/}

        <Draggable bounds={ { right: 200, left: -400, top: 0, bottom: 200 }}>

          <div className="box3">我移动受限制box3</div>

        </Draggable>

      </div>

    </div>

  );

}

另一种在父级内移动 

import React from "react";

// 引入样式

import "./index.css";

// 引入拖拽插件

import Draggable from "react-draggable";

export default function Tuo() {

  return (

    <div>

      <div className="app">

        {/* 限制范围另一个方式*/}

        <Draggable bounds={".app"}>

          <div className="box4">我移动ye受限制box4</div>

        </Draggable>

      </div>

    </div>

  );

}

效果展示: 

 

相关推荐

  1. React实践

    2023-12-22 19:56:01       72 阅读
  2. React实践

    2023-12-22 19:56:01       66 阅读
  3. React实践

    2023-12-22 19:56:01       52 阅读
  4. React 元素教程 react-dnd 实现

    2023-12-22 19:56:01       62 阅读
  5. react实现的插件

    2023-12-22 19:56:01       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-22 19:56:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-22 19:56:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-22 19:56:01       82 阅读
  4. Python语言-面向对象

    2023-12-22 19:56:01       91 阅读

热门阅读

  1. k8s pod常用资源清单

    2023-12-22 19:56:01       49 阅读
  2. spark中 write.csv时, 添加第一行的标题title

    2023-12-22 19:56:01       64 阅读
  3. 力扣面试经典题之数组/字符串

    2023-12-22 19:56:01       66 阅读
  4. libp2p服务发现之 Multicast DNS(mDNS)

    2023-12-22 19:56:01       49 阅读
  5. 微信小程序实现一个todolist这样的小demo

    2023-12-22 19:56:01       66 阅读
  6. C语言中关于函数和数组的理解

    2023-12-22 19:56:01       64 阅读
  7. 除了增删改查,后端一定要学会这些!

    2023-12-22 19:56:01       70 阅读
  8. 学习鸿蒙开发需要报培训班吗?

    2023-12-22 19:56:01       82 阅读