網站首頁 編程語言 正文
本文實例為大家分享了react實現(xiàn)簡單的拖拽功能的具體代碼,供大家參考,具體內容如下
src文件夾下新建文件夾demo ?然后在創(chuàng)建兩個文件js和css
demo.js文件代碼
// react實現(xiàn)拖拽 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;? ? ? 這樣就可以實現(xiàn)一個簡單的拖拽了
原文鏈接:https://blog.csdn.net/weixin_57163112/article/details/119856192
相關推薦
- 2023-05-09 Linux?解壓縮文件到指定目錄_linux shell
- 2022-04-07 Go語言中的Base64編碼原理介紹以及使用_Golang
- 2022-03-23 詳解C#異步多線程使用中的常見問題_C#教程
- 2022-08-30 ORACLE分區(qū)表轉換在線重定義DBMS_REDEFINITION_oracle
- 2023-03-11 Golang的Fork/Join實現(xiàn)代碼_Golang
- 2022-05-29 ASP.NET?Core使用HttpClient調用WebService_實用技巧
- 2022-06-12 startup.bat啟動Tomcat閃退問題原因及解決_Tomcat
- 2023-03-16 C語言進階之字符串查找?guī)旌瘮?shù)詳解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支