typescript+react實(shí)現(xiàn)移動(dòng)端和PC端簡(jiǎn)單拖拽效果
更新時(shí)間:2021年09月23日 14:39:17 作者:博-軒
這篇文章主要為大家詳細(xì)介紹了typescript+react實(shí)現(xiàn)移動(dòng)端和PC端簡(jiǎn)單拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了typescript+react實(shí)現(xiàn)移動(dòng)端和PC端簡(jiǎn)單拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
一、移動(dòng)端
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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解gantt甘特圖可拖拽、編輯(vue、react都可用?highcharts)
- react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能
- 使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑
- 一百多行代碼實(shí)現(xiàn)react拖拽hooks
- React.js組件實(shí)現(xiàn)拖拽排序組件功能過(guò)程解析
- React 實(shí)現(xiàn)拖拽功能的示例代碼
- react.js組件實(shí)現(xiàn)拖拽復(fù)制和可排序的示例代碼
- 再次談?wù)揜eact.js實(shí)現(xiàn)原生js拖拽效果引起的一系列問(wèn)題
- 基于React.js實(shí)現(xiàn)原生js拖拽效果引發(fā)的思考
- react實(shí)現(xiàn)簡(jiǎn)單的拖拽功能
相關(guān)文章
JS實(shí)現(xiàn)圖文并茂的tab選項(xiàng)卡效果示例【附demo源碼下載】
這篇文章主要介紹了JS實(shí)現(xiàn)圖文并茂的tab選項(xiàng)卡效果,涉及javascript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)修改頁(yè)面元素屬性的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09
JavaScript如何一次性展示幾萬(wàn)條數(shù)據(jù)
本文主要介紹了JavaScript一次性展示幾萬(wàn)條數(shù)據(jù)的實(shí)現(xiàn)方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03
JS簡(jiǎn)單實(shí)現(xiàn)自定義右鍵菜單實(shí)例
本篇文章主要介紹了JS簡(jiǎn)單實(shí)現(xiàn)自定義右鍵菜單實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
javascript 對(duì)象 與 prototype 原型用法實(shí)例分析
這篇文章主要介紹了javascript 對(duì)象 與 prototype 原型用法,結(jié)合實(shí)例形式分析了javascript 對(duì)象 與 prototype 原型實(shí)現(xiàn)對(duì)象創(chuàng)建、繼承、拷貝等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11

