react實現(xiàn)簡單的拖拽功能
本文實例為大家分享了react實現(xiàn)簡單的拖拽功能的具體代碼,供大家參考,具體內(nèi)容如下
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 ( ? ? ? ? ? ? <div className='box'> ? ? ? ? ? ? ? ? <div className='box-item' ref="move" onMouseDown={e => this.small_down(e)} style={{ position: "absolute", left: `${this.state.translateX}px`, top: `${this.state.translateY}px` }} /> ? ? ? ? ? ? </div> ? ? ? ? ) ? ? } }; export default Drag
demo.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 ( ? ? ? <div> ? ? ? ? <Drag></Drag> ? ? ? </div> ? ? ? ) ? } } export default App;
這樣就可以實現(xiàn)一個簡單的拖拽了
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- React 實現(xiàn)拖拽功能的示例代碼
- 再次談論React.js實現(xiàn)原生js拖拽效果引起的一系列問題
- react.js組件實現(xiàn)拖拽復制和可排序的示例代碼
- 使用react-beautiful-dnd實現(xiàn)列表間拖拽踩坑
- React.js組件實現(xiàn)拖拽排序組件功能過程解析
- 詳解gantt甘特圖可拖拽、編輯(vue、react都可用?highcharts)
- 基于React.js實現(xiàn)原生js拖拽效果引發(fā)的思考
- react-beautiful-dnd 實現(xiàn)組件拖拽功能
- 一百多行代碼實現(xiàn)react拖拽hooks
- react實現(xiàn)拖拽模態(tài)框
相關文章
react-redux多個組件數(shù)據(jù)共享的方法
這篇文章主要介紹了react-redux多個組件數(shù)據(jù)共享的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08React 如何使用時間戳計算得到開始和結(jié)束時間戳
這篇文章主要介紹了React 如何拿時間戳計算得到開始和結(jié)束時間戳,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09使用react-activation實現(xiàn)keepAlive支持返回傳參
本文主要介紹了使用react-activation實現(xiàn)keepAlive支持返回傳參,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05React如何實現(xiàn)Vue的watch監(jiān)聽屬性
在 Vue 中可以簡單地使用 watch 來監(jiān)聽數(shù)據(jù)的變化,還能獲取到改變前的舊值,而在 React 中是沒有 watch 的,今天通過本文給大家講解React實現(xiàn)Vue的watch監(jiān)聽屬性的方法,需要的朋友可以參考下2023-06-06React+EggJs實現(xiàn)斷點續(xù)傳的示例代碼
這篇文章主要介紹了React+EggJs實現(xiàn)斷點續(xù)傳的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07react-router JS 控制路由跳轉(zhuǎn)實例
這篇文章主要介紹了react-router JS 控制路由跳轉(zhuǎn)實例,react實現(xiàn)路由可以直接使用react-router。有興趣的可以了解一下2017-06-06