網站首頁 編程語言 正文
本文實例為大家分享了react實現簡單的拖拽功能的具體代碼,供大家參考,具體內容如下
src文件夾下新建文件夾demo ?然后在創建兩個文件js和css
demo.js文件代碼
// react實現拖拽 import React from 'react' // 引入css樣式 import './demo.css' class Drag extends React.Component { ? ? constructor(props) { ? ? ? ? super(props); ? ? ? ? this.state = { ? ? ? ? ? ? translateX: 0, ? ? ? ? ? ? translateY: 0, ? ? ? ? }; ? ? } ? ? small_down = (e) => { ? ? ? ? var obig = this.refs.move.parentNode; ? ? ? ? var osmall = this.refs.move; ? ? ? ? var e = e || window.event; ? ? ? ? /*用于保存小的div拖拽前的坐標*/ ? ? ? ? osmall.startX = e.clientX - osmall.offsetLeft; ? ? ? ? osmall.startY = e.clientY - osmall.offsetTop; ? ? ? ? /*鼠標的移動事件*/ ? ? ? ? document.onmousemove = function (e) { ? ? ? ? ? ? var e = e || window.event; ? ? ? ? ? ? osmall.style.left = e.clientX - osmall.startX + "px"; ? ? ? ? ? ? osmall.style.top = e.clientY - osmall.startY + "px"; ? ? ? ? ? ? /*對于大的DIV四個邊界的判斷*/ ? ? ? ? ? ? let x = obig.offsetWidth - osmall.offsetWidth ? ? ? ? ? ? let y = obig.offsetHeight - osmall.offsetHeight ? ? ? ? ? ? if (e.clientX - osmall.startX <= 0) { ? ? ? ? ? ? ? ? osmall.style.left = 0 + "px"; ? ? ? ? ? ? } ? ? ? ? ? ? if (e.clientY - osmall.startY <= 0) { ? ? ? ? ? ? ? ? osmall.style.top = 0 + "px"; ? ? ? ? ? ? } ? ? ? ? ? ? if (e.clientX - osmall.startX >= x) { ? ? ? ? ? ? ? ? osmall.style.left = x + "px"; ? ? ? ? ? ? } ? ? ? ? ? ? if (e.clientY - osmall.startY >= y) { ? ? ? ? ? ? ? ? osmall.style.top = y + "px"; ? ? ? ? ? ? } ? ? ? ? }; ? ? ? ? /*鼠標的抬起事件,終止拖動*/ ? ? ? ? document.onmouseup = function () { ? ? ? ? ? ? document.onmousemove = null; ? ? ? ? ? ? document.onmouseup = null; ? ? ? ? }; ? ? } ? ? componentDidMount() { ? ? } ? ? render() { ? ? ? ? return ( ? ? ? ? ? ?? ? ? ? ? ? ? ?this.small_down(e)} style={{ position: "absolute", left: `${this.state.translateX}px`, top: `${this.state.translateY}px` }} /> ? ? ? ? ? ?? ? ? ? ) ? ? } }; export default Dragdemo.css代碼
.box{ ? width: 100vw; ? height: 100vh; ? position: relative; } ? .box-item{ ? position: absolute; ? width: 100px; ? height: 100px; ? background: pink; }App.js里面
import './App.css'; // 引入demo這個文件 import Drag from './demo/demo' import React from 'react'; ? class App extends React.Component { ? constructor(props) { ? ? super(props) ? } ? render() { ? ? return ( ? ? ?? ? ? ?? ? ? ) ? } } export default App;? ? ? 這樣就可以實現一個簡單的拖拽了
原文鏈接:https://blog.csdn.net/weixin_57163112/article/details/119856192
相關推薦
- 2022-09-02 Redis解決Session共享問題的方法詳解_Redis
- 2021-12-02 C語言GetStdHandle函數使用方法_C 語言
- 2024-01-11 spring 事務控制 設置手動回滾 TransactionAspectSupport.curren
- 2022-10-23 Linux服務器VPS的Windows?DD包詳細的制作教程_Linux
- 2022-07-23 聊聊配置?Nginx?訪問與錯誤日志的問題_nginx
- 2022-11-25 詳解Python中的數據精度問題_python
- 2022-08-02 深入了解Golang的map增量擴容_Golang
- 2023-06-13 Python?base64和hashlib模塊及用法詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支