Typescript+react to achieve simple drag and drop effects on mobile and PC

Typescript+react to achieve simple drag and drop effects on mobile and PC

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. Mobile

1.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. PC

1.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 gantt chart draggable and editable (highcharts can be used for vue and react)
  • react-beautiful-dnd implements component drag and drop function
  • Using react-beautiful-dnd to implement drag and drop between lists
  • More than 100 lines of code to implement react drag hooks
  • React.js component implements drag and drop sorting component function process analysis
  • React sample code to implement drag and drop function
  • React.js component implements drag-and-drop copy and sortable sample code
  • Let's talk again about a series of problems caused by React.js implementing native js drag effects
  • Thoughts on implementing native js drag effects based on React.js
  • React implements simple drag and drop function

<<:  Detailed explanation of MySQL clustered index and non-clustered index

>>:  Detailed steps to deploy SpringBoot projects using Docker in Idea

Recommend

How to configure SSL certificate in nginx to implement https service

In the previous article, after using openssl to g...

How to build a new image based on an existing image in Docker

Building new images from existing images is done ...

js implements clock component based on canvas

Canvas has always been an indispensable tag eleme...

A brief discussion on MySQL event planning tasks

1. Check whether event is enabled show variables ...

A brief discussion on the fun of :focus-within in CSS

I believe some people have seen this picture of c...

How to install MySQL 8.0 in Docker

Environment: MacOS_Cetalina_10.15.1, Mysql8.0.18,...

Summary of solutions for MySQL not supporting group by

I downloaded and installed the latest version of ...

CSS3 custom scroll bar style::webkit-scrollbar sample code detailed explanation

The default scroll bar style in Windows is ugly, ...

Example code of implementing starry sky animation with CSS3 advanced LESS

This article introduces the sample code of advanc...

Teach you how to deploy Vue project with Docker

1.Write in front: As a lightweight virtualization...

Analysis of idea compiler vue indentation error problem scenario

Project scenario: When running the Vue project, t...

How to use MySQL binlog to restore accidentally deleted databases

Table of contents 1 View the current database con...

Complete steps for vue dynamic binding icons

0 Differences between icons and images Icons are ...

Text mode in IE! Introduction to the role of DOCTYPE

After solving the form auto-fill problem discussed...

HTML tag meta summary, HTML5 head meta attribute summary

Preface meta is an auxiliary tag in the head area...