typescript和react如何实现移动端和PC端简单拖拽效果
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,这篇文章将为大家详细讲解有关typescript和react如何实现移动端和PC端简单拖拽效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下一、移动端1
千家信息网最后更新 2025年11月14日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 = 0 disY: number = 0 x: number = 0 y: number = 0 dragElement = createRef() 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 ( ) }}export default TextDrag 2.css代码
.TextDrag{ position: absolute; width: 100px; height: 100px; background-color: red;}关于"typescript和react如何实现移动端和PC端简单拖拽效果"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
内容
代码
移动
按钮
篇文章
效果
更多
不错
实用
文章
知识
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库购买
软件开发最好的教材
大学网络安全宣传开幕式
nist数据库在哪里
linux做文件服务器
茶叶进销存数据库
互联网金融 网络安全公司
下列关于数据库的安全性
数据库创建存储过程格式
商务软件开发证书
实时流式数据库有哪些
91助手 数据库
网络安全如何检测
数据库设计心得
医院信息网络安全实施方案
河南电子软件开发公司
高速网络技术安全员
数据库怎么和百度地图链接
软件开发工程师还是前端
柯南数据库
上海购买网络技术转让产品
天龙八部帐号数据库
联想塔式服务器价格
阿里巴巴网络技术咨询电话
网络安全培养工作计划
深圳迈优互联网科技公司
dede数据库密码修改
数据库软件安装后磁盘空间
关系型的数据库是什么
领域软件无法连接数据库