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í)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解gantt甘特圖可拖拽、編輯(vue、react都可用?highcharts)
- react-beautiful-dnd 實現(xiàn)組件拖拽功能
- 使用react-beautiful-dnd實現(xiàn)列表間拖拽踩坑
- 一百多行代碼實現(xiàn)react拖拽hooks
- React.js組件實現(xiàn)拖拽排序組件功能過程解析
- React 實現(xiàn)拖拽功能的示例代碼
- react.js組件實現(xiàn)拖拽復(fù)制和可排序的示例代碼
- 再次談?wù)揜eact.js實現(xiàn)原生js拖拽效果引起的一系列問題
- 基于React.js實現(xiàn)原生js拖拽效果引發(fā)的思考
- react實現(xiàn)簡單的拖拽功能
相關(guān)文章
JS實現(xiàn)圖文并茂的tab選項卡效果示例【附demo源碼下載】
這篇文章主要介紹了JS實現(xiàn)圖文并茂的tab選項卡效果,涉及javascript響應(yīng)鼠標事件動態(tài)修改頁面元素屬性的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09JavaScript如何一次性展示幾萬條數(shù)據(jù)
本文主要介紹了JavaScript一次性展示幾萬條數(shù)據(jù)的實現(xiàn)方法。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03javascript 對象 與 prototype 原型用法實例分析
這篇文章主要介紹了javascript 對象 與 prototype 原型用法,結(jié)合實例形式分析了javascript 對象 與 prototype 原型實現(xiàn)對象創(chuàng)建、繼承、拷貝等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11