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***Q78411 分钟前
前端在移动端中的离线功能
前端
星环处相逢14 分钟前
Nginx 优化与防盗链及扩展配置指南
服务器·前端·nginx
tsumikistep23 分钟前
【前后端】Vue 脚手架与前端工程结构入门指南
前端·javascript·vue.js
在繁华处28 分钟前
JAVA实战:文件管理系统1.0
java·开发语言·前端
GISer_Jing37 分钟前
SSE Conf大会分享支付宝xUI引擎:AI时代的多模态交互革命
前端·人工智能·交互
Aerelin43 分钟前
爬虫playwright中的资源监听
前端·爬虫·js·playwright
WordPress学习笔记1 小时前
专业建外贸网站公司推荐
大数据·前端·人工智能
fruge1 小时前
前端简历优化:如何突出项目亮点与技术深度(附示例)
前端
华仔啊1 小时前
Vue3 + Element Plus 动态菜单实现:一套代码完美适配多角色权限系统
前端·vue.js
n***84071 小时前
Springboot-配置文件中敏感信息的加密:三种加密保护方法比较
android·前端·后端