最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react實(shí)現(xiàn)簡單的拖拽功能

 更新時(shí)間:2022年03月08日 10:53:15   作者:至_臻  
這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)簡單的拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)的移動事件*/
? ? ? ? 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四個(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)的抬起事件,終止拖動*/
? ? ? ? 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這個(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • react項(xiàng)目如何使用iconfont的方法步驟

    react項(xiàng)目如何使用iconfont的方法步驟

    這篇文章主要介紹了react項(xiàng)目如何使用iconfont的方法步驟,這里介紹下如何在項(xiàng)目中配置。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-03-03
  • React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo

    React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo

    這篇文章主要為大家介紹了React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 無廢話快速上手React路由開發(fā)

    無廢話快速上手React路由開發(fā)

    本文以簡潔為目標(biāo),幫助快速上手react-router-dom默認(rèn)你接觸過路由相關(guān)的開發(fā),通過實(shí)例代碼講解的很詳細(xì),對React路由相關(guān)知識感興趣的朋友一起看看吧
    2021-05-05
  • webpack 2的react開發(fā)配置實(shí)例代碼

    webpack 2的react開發(fā)配置實(shí)例代碼

    本篇文章主要介紹了webpack 2的react開發(fā)配置實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • react-native 實(shí)現(xiàn)漸變色背景過程

    react-native 實(shí)現(xiàn)漸變色背景過程

    這篇文章主要介紹了react-native 實(shí)現(xiàn)漸變色背景過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React 中的 ForwardRef的使用示例詳解

    React 中的 ForwardRef的使用示例詳解

    forwardRef 相當(dāng)于是為 ref 傳遞的一種方式,普通的函數(shù)式組件就是 Render,而 fowardRef 多加了 Ref 參數(shù),這篇文章主要介紹了React 中的 ForwardRef的使用示例詳解,需要的朋友可以參考下
    2024-06-06
  • react-router4按需加載(踩坑填坑)

    react-router4按需加載(踩坑填坑)

    這篇文章主要介紹了react-router4按需加載(踩坑填坑),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • react MPA 多頁配置詳解

    react MPA 多頁配置詳解

    這篇文章主要介紹了react MPA 多頁配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • React構(gòu)建組件的幾種方式及區(qū)別

    React構(gòu)建組件的幾種方式及區(qū)別

    這篇文章主要介紹了React構(gòu)建組件的幾種方式及區(qū)別,組件就是把圖形、非圖形的各種邏輯均抽象為一個(gè)統(tǒng)一的概念來實(shí)現(xiàn)開發(fā)的模式文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • ChatGLM?集成LangChain工具詳解

    ChatGLM?集成LangChain工具詳解

    這篇文章主要為大家介紹了Svelte和React框架使用比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04

最新評論

南华县| 灵石县| 东丰县| 会泽县| 新建县| 刚察县| 鄂州市| 淳安县| 枣阳市| 军事| 越西县| 临清市| 青阳县| 中阳县| 株洲县| 锡林浩特市| 临桂县| 永新县| 汉寿县| 高碑店市| 屯门区| 息烽县| 农安县| 徐汇区| 常德市| 新绛县| 任丘市| 伊金霍洛旗| 吉首市| 阳西县| 郧西县| 姜堰市| 伊金霍洛旗| 松阳县| 岱山县| 阿鲁科尔沁旗| 汕尾市| 南安市| 大同县| 山阳县| 青海省|