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

Simple web page code used in NetEase blog

How to use the code in NetEase Blog: First log in...

A detailed guide to custom directives in Vue

Table of contents 1. What is a custom instruction...

Sharing some details about MySQL indexes

A few days ago, a colleague asked me a question a...

Summary of special processing statements of MySQL SQL statements (must read)

1. Update the entire table. If the value of a col...

Detailed explanation of the execution order of JavaScript Alert function

Table of contents question analyze solve Replace ...

Methods for optimizing Oracle database with large memory pages in Linux

Preface PC Server has developed to this day and h...

About 3 common packages of rem adaptation

Preface I wrote an article about rem adaptation b...

Detailed explanation of Nginx access restriction configuration

What is Nginx access restriction configuration Ng...

A complete example of implementing a timed crawler with Nodejs

Table of contents Cause of the incident Use Node ...

Two ways to install Python3 on Linux servers

First method Alibaba Cloud and Baidu Cloud server...

CSS Paint API: A CSS-like Drawing Board

1. Use Canvas images as CSS background images The...

A brief analysis of how to set the initial value of Linux root

Ubuntu does not allow root login by default, so t...

Some functions of using tcpdump to capture packets in the Linux command line

tcpdump is a flexible and powerful packet capture...

MYSQL custom function to determine whether it is a positive integer example code

You can write a function: Mainly use regular expr...