This article shares the specific code of typescript+react to achieve simple drag and drop effects on mobile and PC for your reference. The specific content is as follows 1. Mobile1.tsx code 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: 'Button 1' }, { id: '02', text: 'Button 2' }, { id: '03', text: 'Button three' } ], ContentArray: [ { id: 'c1', text: 'Content 1' }, { id: 'c2', text: 'Content 2' }, { id: 'c3', text: 'Content 3' } ], } } 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 code .drag { position: absolute; width: 100px; height: 100px; background-color: red; } 2. PC1.tsx code 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 code .TextDrag{ position: absolute; width: 100px; height: 100px; background-color: red; } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of MySQL clustered index and non-clustered index
>>: Detailed steps to deploy SpringBoot projects using Docker in Idea
How to use the code in NetEase Blog: First log in...
Table of contents 1. What is a custom instruction...
A few days ago, a colleague asked me a question a...
1. Update the entire table. If the value of a col...
Table of contents question analyze solve Replace ...
Preface PC Server has developed to this day and h...
Preface I wrote an article about rem adaptation b...
A few days ago, I saw an example written by @Kyle...
What is Nginx access restriction configuration Ng...
Table of contents Cause of the incident Use Node ...
First method Alibaba Cloud and Baidu Cloud server...
1. Use Canvas images as CSS background images The...
Ubuntu does not allow root login by default, so t...
tcpdump is a flexible and powerful packet capture...
You can write a function: Mainly use regular expr...