typescript+react实现移动端和PC端简单拖拽效果
【typescript+react实现移动端和PC端简单拖拽效果】本文实例为大家分享了typescript+react实现移动端和PC端简单拖拽效果的具体代码,供大家参考,具体内容如下
一、移动端
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{constructor(props: Props) {super(props)this.state = {ButtonIndex: 0,ButtonArray: [{id: '01',text: '按钮一'},{id: '02',text: '按钮二'},{id: '03',text: '按钮三'}],ContentArray: [{id: 'c1',text: '内容一'},{id: 'c2',text: '内容二'},{id: 'c3',text: '内容三'}],}}FnTab(index: number):void {this.setState({ButtonIndex: index})}render() {return ({this.state.ButtonArray.map((item, index) => )}{this.state.ContentArray.map((item, index) => {item.text})} )}}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 = 0disY: number = 0x: number = 0y: number = 0dragElement = createRef()constructor(props: Props) {super(props)this.state = { }}FnDown(ev: React.MouseEvent) {if (this.dragElement.current) {this.disX = ev.clientX - this.dragElement.current?.getBoundingClientRect().leftthis.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.disXthis.y = ev.clientY - this.disYif (this.dragElement.current) {this.dragElement.current.style.left = this.x + 'px'this.dragElement.current.style.top = this.y + 'px'}}FnUp() {document.onmousemove = nulldocument.onmouseup = null}render() {return ( )}}export default TextDrag
2.css代码
.TextDrag{position: absolute; width: 100px; height: 100px; background-color: red; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 移动端h5调试方法
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM