typescript+react實(shí)現(xiàn)移動(dòng)端和PC端簡單拖拽效果
本文實(shí)例為大家分享了typescript+react實(shí)現(xiàn)移動(dòng)端和PC端簡單拖拽效果的具體代碼,供大家參考,具體內(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;
}
以上就是本文的全部內(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)拖拽排序組件功能過程解析
- React 實(shí)現(xiàn)拖拽功能的示例代碼
- react.js組件實(shí)現(xiàn)拖拽復(fù)制和可排序的示例代碼
- 再次談?wù)揜eact.js實(shí)現(xiàn)原生js拖拽效果引起的一系列問題
- 基于React.js實(shí)現(xiàn)原生js拖拽效果引發(fā)的思考
- react實(shí)現(xiàn)簡單的拖拽功能
相關(guān)文章
js實(shí)現(xiàn)為a標(biāo)簽添加事件的方法(使用閉包循環(huán))
這篇文章主要介紹了js實(shí)現(xiàn)為a標(biāo)簽添加事件的方法,基于閉包循環(huán)實(shí)現(xiàn)事件添加的功能,涉及javascript閉包與事件操作相關(guān)技巧,需要的朋友可以參考下2016-08-08
Js實(shí)現(xiàn)滾動(dòng)變色的文字效果
滾動(dòng)變色的文字js特效,可看到文字在交替變色顯示,以吸引人的注意,效果真心不錯(cuò)哦,需要的朋友可以參考下2014-06-06
開發(fā) Internet Explorer 右鍵功能表(ContextMenu)
本篇介紹如何開發(fā) Internet Explorer 右鍵功能表(ContextMenu),以 0rz.tw 縮短網(wǎng)址列為范例2013-07-07
js代碼實(shí)現(xiàn)無縫滾動(dòng)(文字和圖片)
js無縫滾動(dòng),圖片無縫滾動(dòng),文字無縫滾動(dòng),基于js代碼如何實(shí)現(xiàn),本篇文章給大家詳解js代碼實(shí)現(xiàn)無縫滾動(dòng)(文字和圖片),需要的朋友可以參考下2015-08-08
JS中正則表達(dá)式只有3種匹配模式(沒有單行模式)詳解
下面小編就為大家?guī)硪黄狫S中正則表達(dá)式只有3種匹配模式(沒有單行模式)詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
KnockoutJS 3.X API 第四章之click綁定
click綁定主要作用是用于DOM元素被點(diǎn)擊時(shí)調(diào)用相關(guān)JS函數(shù)。這篇文章主要介紹了KnockoutJS 3.X API 第四章之click綁定,感興趣的朋友一起看看吧2016-10-10
JavaScript開發(fā)中需要搞懂的字符編碼總結(jié)
字符集就是字符的集合,字符編碼則代表字符集的實(shí)際編碼規(guī)則,是用于計(jì)算機(jī)解析字符的。本文為大家整理了JavaScript開發(fā)中需要搞懂的字符編碼,希望對(duì)大家有所幫助2023-02-02
uniapp自定義網(wǎng)絡(luò)檢測組件項(xiàng)目實(shí)戰(zhàn)總結(jié)分析
這篇文章主要為大家介紹了uniapp自定義網(wǎng)絡(luò)檢測組件項(xiàng)目實(shí)戰(zhàn)總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

