欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • js中的replace方法使用介紹

    js中的replace方法使用介紹

    replace() 方法用于將字符串用一些字符替換另一些字符,如果用正則表達式替換時, regexp 具有全局標志 g,下面有個不錯的示例,感興趣的朋友可以參考下
    2013-10-10
  • 微信小程序?qū)崿F(xiàn)錄音Record功能

    微信小程序?qū)崿F(xiàn)錄音Record功能

    這篇文章主要為大家詳細介紹了微信小程序?qū)崿F(xiàn)錄音Record功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • TypeScript中的類

    TypeScript中的類

    這篇文章主要介紹了TypeScript中的類,類這個概念基本是所有面向?qū)ο缶幊陶Z言都具有一個概念,JavaScript中ES6?之前是沒有類這個概念,下面文章圍繞TypeScript類的相關(guān)資料展開內(nèi)容,需要的朋友可以參考一下
    2021-12-12
  • JavaScript 巧學(xué)巧用

    JavaScript 巧學(xué)巧用

    本文主要給大家分享一下在編寫JavaScript代碼的時候存在的一些方法和技巧,雖然有時候條條大路都通向羅馬,但是也許總會有那么一條最短的路徑可走。希望通過以下幾點JavaScript技巧讓大家的代碼“化繁為簡,化簡為精”
    2017-05-05
  • 純JS實現(xiàn)本地圖片預(yù)覽的方法

    純JS實現(xiàn)本地圖片預(yù)覽的方法

    這篇文章主要介紹了純JS實現(xiàn)本地圖片預(yù)覽的方法,基于javascript插件實現(xiàn)本地圖片預(yù)覽功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • JS實現(xiàn)圖文并茂的tab選項卡效果示例【附demo源碼下載】

    JS實現(xiàn)圖文并茂的tab選項卡效果示例【附demo源碼下載】

    這篇文章主要介紹了JS實現(xiàn)圖文并茂的tab選項卡效果,涉及javascript響應(yīng)鼠標事件動態(tài)修改頁面元素屬性的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2016-09-09
  • bootstrap表格分頁實例講解

    bootstrap表格分頁實例講解

    這篇文章主要為大家詳細介紹了bootstrap表格分頁實例講解,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • JavaScript如何一次性展示幾萬條數(shù)據(jù)

    JavaScript如何一次性展示幾萬條數(shù)據(jù)

    本文主要介紹了JavaScript一次性展示幾萬條數(shù)據(jù)的實現(xiàn)方法。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • JS簡單實現(xiàn)自定義右鍵菜單實例

    JS簡單實現(xiàn)自定義右鍵菜單實例

    本篇文章主要介紹了JS簡單實現(xiàn)自定義右鍵菜單實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • javascript 對象 與 prototype 原型用法實例分析

    javascript 對象 與 prototype 原型用法實例分析

    這篇文章主要介紹了javascript 對象 與 prototype 原型用法,結(jié)合實例形式分析了javascript 對象 與 prototype 原型實現(xiàn)對象創(chuàng)建、繼承、拷貝等相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11

最新評論