react實(shí)現(xiàn)簡單的拖拽功能
本文實(shí)例為大家分享了react實(shí)現(xiàn)簡單的拖拽功能的具體代碼,供大家參考,具體內(nèi)容如下
src文件夾下新建文件夾demo 然后在創(chuàng)建兩個(gè)文件js和css

demo.js文件代碼
// react實(shí)現(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拖拽前的坐標(biāo)*/
? ? ? ? osmall.startX = e.clientX - osmall.offsetLeft;
? ? ? ? osmall.startY = e.clientY - osmall.offsetTop;
? ? ? ? /*鼠標(biāo)的移動(dòng)事件*/
? ? ? ? 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";
? ? ? ? ? ? /*對(duì)于大的DIV四個(gè)邊界的判斷*/
? ? ? ? ? ? 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";
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? /*鼠標(biāo)的抬起事件,終止拖動(dòng)*/
? ? ? ? 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 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這個(gè)文件
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;這樣就可以實(shí)現(xiàn)一個(gè)簡單的拖拽了


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- React 實(shí)現(xiàn)拖拽功能的示例代碼
- 再次談?wù)揜eact.js實(shí)現(xiàn)原生js拖拽效果引起的一系列問題
- react.js組件實(shí)現(xiàn)拖拽復(fù)制和可排序的示例代碼
- 使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑
- React.js組件實(shí)現(xiàn)拖拽排序組件功能過程解析
- 詳解gantt甘特圖可拖拽、編輯(vue、react都可用?highcharts)
- 基于React.js實(shí)現(xiàn)原生js拖拽效果引發(fā)的思考
- react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能
- 一百多行代碼實(shí)現(xiàn)react拖拽hooks
- react實(shí)現(xiàn)拖拽模態(tài)框
相關(guān)文章
react項(xiàng)目如何使用iconfont的方法步驟
這篇文章主要介紹了react項(xiàng)目如何使用iconfont的方法步驟,這里介紹下如何在項(xiàng)目中配置。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo
這篇文章主要為大家介紹了React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
webpack 2的react開發(fā)配置實(shí)例代碼
本篇文章主要介紹了webpack 2的react開發(fā)配置實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
react-native 實(shí)現(xiàn)漸變色背景過程
這篇文章主要介紹了react-native 實(shí)現(xiàn)漸變色背景過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

