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>

);

}

效果展示:

相关推荐
街尾杂货店&11 分钟前
css word-spacing属性
前端·css
千叶寻-14 分钟前
正则表达式
前端·javascript·后端·架构·正则表达式·node.js
光影少年5 小时前
angular生态及学习路线
前端·学习·angular.js
无尽夏_7 小时前
HTML5(前端基础)
前端·html·html5
Jagger_8 小时前
敏捷开发流程-精简版
前端·后端
FIN66688 小时前
昂瑞微冲刺科创板:创新驱动,引领射频芯片国产化新征程
前端·安全·前端框架·信息与通信·芯片
GISer_Jing8 小时前
ByteDance——jy真题
前端·javascript·面试
睡美人的小仙女1278 小时前
浏览器为何屏蔽本地文件路径?
前端
真的想不出名儿8 小时前
Vue 中 props 传递数据的坑
前端·javascript·vue.js
FIN66688 小时前
昂瑞微:深耕射频“芯”赛道以硬核实力冲刺科创板大门
前端·人工智能·科技·前端框架·信息与通信·智能