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
In the previous article, after using openssl to g...
Building new images from existing images is done ...
Canvas has always been an indispensable tag eleme...
1. Check whether event is enabled show variables ...
I believe some people have seen this picture of c...
Environment: MacOS_Cetalina_10.15.1, Mysql8.0.18,...
I downloaded and installed the latest version of ...
The default scroll bar style in Windows is ugly, ...
This article introduces the sample code of advanc...
1.Write in front: As a lightweight virtualization...
Project scenario: When running the Vue project, t...
Table of contents 1 View the current database con...
0 Differences between icons and images Icons are ...
After solving the form auto-fill problem discussed...
Preface meta is an auxiliary tag in the head area...