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

typescript+react實現(xiàn)移動端和PC端簡單拖拽效果

 更新時間:2021年09月23日 14:39:17   作者:博-軒  
這篇文章主要為大家詳細介紹了typescript+react實現(xiàn)移動端和PC端簡單拖拽效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了typescript+react實現(xiàn)移動端和PC端簡單拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下

一、移動端

1.tsx代碼

import { Component } from "react";
import './Tab.less'
interface Props {
 
}
interface user {
    id: string,
    text: string
}
interface content {
    id: string,
    text: string
}
interface State {
    ButtonIndex: number,
    ButtonArray: user[],
    ContentArray: content[]
}
class Tab extends Component<Props, State>{
    constructor(props: Props) {
        super(props)
        this.state = {
            ButtonIndex: 0,
            ButtonArray: [
                {
                    id: '01',
                    text: '按鈕一'
                },
                {
                    id: '02',
                    text: '按鈕二'
                },
                {
                    id: '03',
                    text: '按鈕三'
                }
            ],
            ContentArray: [
                {
                    id: 'c1',
                    text: '內(nèi)容一'
                },
                {
                    id: 'c2',
                    text: '內(nèi)容二'
                },
                {
                    id: 'c3',
                    text: '內(nèi)容三'
                }
            ],
        }
    }
    FnTab(index: number):void {
        this.setState({
            ButtonIndex: index
        })
    }
    render() {
        return (
            <div className='tab'>
                {
                    this.state.ButtonArray.map((item, index) => <button key={item.id} onClick={this.FnTab.bind(this, index)} className={this.state.ButtonIndex === index ? 'tab-button ac' : 'tab-button'}>{item.text}</button>)
                }
                {
                    this.state.ContentArray.map((item, index) => <div key={item.id} style={{display:this.state.ButtonIndex===index?'block':'none'}} className='tab-content'>{item.text}</div>)
                }
 
            </div>
        )
    }
}
export default Tab

2.css代碼

.drag {
    position: absolute;
    width: 100px;
    height: 100px;
    background-color: red;
}

二、PC端

1.tsx代碼

import { Component, createRef } from 'react'
import './index.less'
interface Props {
 
 
}
interface State {
 
 
}
class TextDrag extends Component {
  disX: number = 0
  disY: number = 0
  x: number = 0
  y: number = 0
  dragElement = createRef<HTMLDivElement>()
  constructor(props: Props) {
    super(props)
    this.state = {
 
 
    }
  }
  FnDown(ev: React.MouseEvent) {
    if (this.dragElement.current) {
      this.disX = ev.clientX - this.dragElement.current?.getBoundingClientRect().left
      this.disX = ev.clientY - this.dragElement.current?.getBoundingClientRect().top
    }
    document.onmousemove = this.FnMove.bind(this)
    document.onmouseup = this.FnUp
  }
  FnMove(ev: MouseEvent) {
    this.x = ev.clientX - this.disX
    this.y = ev.clientY - this.disY
    if (this.dragElement.current) {
      this.dragElement.current.style.left = this.x + 'px'
      this.dragElement.current.style.top = this.y + 'px'
    }
  }
  FnUp() {
    document.onmousemove = null
    document.onmouseup = null
  }
  render() {
    return (
      <div className="TextDrag" ref={this.dragElement} onMouseDown={this.FnDown.bind(this)}>  </div>
 
 
    )
  }
}

export default TextDrag

2.css代碼

.TextDrag{
    position: absolute;
    width: 100px;
    height: 100px;
    background-color: red;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

深泽县| 涿州市| 濮阳市| 托克托县| 长沙县| 萨嘎县| 和林格尔县| 岢岚县| 大洼县| 吐鲁番市| 西乌| 义乌市| 河间市| 洪洞县| 金阳县| 即墨市| 佛教| 永川市| 龙岩市| 旅游| 鲁甸县| 临清市| 娄底市| 察哈| 辉县市| 龙岩市| 刚察县| 璧山县| 崇明县| 麻栗坡县| 西乌珠穆沁旗| 连江县| 邛崃市| 阿荣旗| 临安市| 紫金县| 太原市| 龙口市| 丹江口市| 盐山县| 文水县|