react+ts双球交叉与碰撞

这篇博客展示了如何用React编写一个触摸拖动的滑块组件,该组件包含两个可拖动的小球,用于显示数值范围。通过监听touch事件,计算小球的位置并更新状态,从而实现滑动交互。同时,代码中包含了样式定义,使得组件具有良好的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

index.tsx

import { Component } from "react";
import './index.less';

interface Props {

}
interface State {
  Num: number,
  MaxNum: number,
}


class Drag extends Component<Props, State>{
  constructor(props: Props) {
    super(props);
    this.state = {
      Num: 0,
      MaxNum: 100
    }
  }
  parent?: HTMLDivElement;
  child!: HTMLCollection;
  startX: number = 0;
  ballW: number = 0;
  oW: number = 0;
  x: number = 0;
  dis: number = 0;
  Num: number = 0;
  MaxNum: number = 0;
  scale: number = 0;
  FnStart(ev: React.TouchEvent) {
    this.startX = ev.changedTouches[0].pageX - (ev.target as HTMLDivElement).offsetLeft;
    this.parent = ((ev.target as HTMLDivElement).parentNode) as HTMLDivElement;
    this.child = this.parent.children;
    this.ballW = (ev.target as HTMLDivElement).offsetWidth;
    this.oW = this.parent.offsetWidth - this.ballW;
    document.ontouchmove = this.FnMove.bind(this);
    document.ontouchend = this.FnEnd.bind(this);
  }
  FnMove(ev: TouchEvent) {
    this.x = ev.changedTouches[0].pageX - this.startX;
    if (this.x < 0) this.x = 0;
    if (this.x > this.oW) this.x = this.oW;
    for (var i = 0; i < this.child.length; i++) {
      if (this.child[i].classList.contains('small-ball')) {
        let Len = this.child.length - 2;
        if (i === Len) {
          this.dis = Math.abs((this.child[i] as HTMLDivElement).offsetLeft - (this.child[i + 1] as HTMLDivElement).offsetLeft);
          (this.child[1] as HTMLDivElement).style.width = this.dis + 'px';
          if ((this.child[i] as HTMLDivElement).offsetLeft - (this.child[i + 1] as HTMLDivElement).offsetLeft < 0) {
            (this.child[1] as HTMLDivElement).style.left = (this.child[i] as HTMLDivElement).offsetLeft + 'px';
          } else {
            (this.child[1] as HTMLDivElement).style.left = (this.child[i + 1] as HTMLDivElement).offsetLeft + 'px';
          }
        }
      }
    }
    // 两小球互推,注释掉就是交叉
    // if((ev.target as HTMLDivElement).className.indexOf('ac')===-1){
    //   if(this.x+this.ballW>(this.child[3] as HTMLDivElement).offsetLeft){
    //     if(this.x>this.oW-this.ballW)this.x=this.oW-this.ballW;
    //     (this.child[2] as HTMLDivElement).style.left=this.x+'px';
    //     (this.child[3] as HTMLDivElement).style.left=this.x+this.ballW+'px';
    //   }
    // }
    // if((ev.target as HTMLDivElement).className.indexOf('ac')!==-1){
    //   if(this.x-this.ballW<(this.child[2] as HTMLDivElement).offsetLeft){
    //     if(this.x<this.ballW)this.x=this.ballW;
    //     (this.child[2] as HTMLDivElement).style.left=this.x-this.ballW+'px';
    //     (this.child[3] as HTMLDivElement).style.left=this.x+'px';
    //   }
    // }
    
    this.scale = (this.oW) / 100; //一份的长度
    let left = (this.child[2] as HTMLDivElement).offsetLeft;
    this.Num = Math.floor((left / this.scale));
    if (this.Num < 0) this.Num = 0;
    if (this.Num > 100) this.Num = 100;
    let right = (this.child[3] as HTMLDivElement).offsetLeft;
    this.MaxNum = Math.floor(right / this.scale);
    if (this.MaxNum < 0) this.MaxNum = 0;
    if (this.MaxNum > 100) this.MaxNum = 100;
    this.setState({
      Num:this.Num,
      MaxNum:this.MaxNum,
  });
    (ev.target as HTMLDivElement).style.left = this.x + 'px';
  }
  FnEnd() {
    document.ontouchmove = null;
    document.ontouchend = null;
  }

  render() {
    return (
      <div className="ball-wrap">
        <div className="line" ref="right"></div>
        <div className="line active" ref="line"></div>
        <div className="small-ball" onTouchStart={this.FnStart.bind(this)}><div className="num">{this.state.Num}</div></div>
        <div className="small-ball ac" onTouchStart={this.FnStart.bind(this)}><div className="num">{this.state.MaxNum}</div></div>
      </div>
    )
  }
}

export default Drag;

index.less

.ball-wrap {
    width: 500px;
    height: 60px;
    margin: 200px auto;
    position: relative;

    .line {
        width: 500px;
        height: 3px;
        background: #e3e3e3;
        position: absolute;
        top: 0;
        bottom: 0;
        margin: auto;
    }

    .line.active {
        width: 500px;
        height: 3px;
        background: skyblue;
        position: absolute;
        top: 0;
        bottom: 0;
        margin: auto;
    }

    .small-ball {
        width: 50px;
        height: 50px;
        background: #fff;
        position: absolute;
        top: 0;
        bottom: 0;
        margin: auto;
        left: 0;
        border-radius: 50%;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
        .num{
            position: relative;
            top: 50px;
        }
    }

    .small-ball.ac {
        width: 50px;
        height: 50px;
        background: #fff;
        position: absolute;
        top: 0;
        bottom: 0;
        margin: auto;
        left: calc(500px - 50px);
        border-radius: 50%;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
    }
}

### 创建 React TypeScript 项目 对于希望创建并配置一个基于 React 和 TypeScript 的项目的开发者来说,有多种方法可以选择。一种流行的方法是利用 `create-react-app` 工具来快速搭建开发环境。 通过执行特定命令可以直接初始化带有 TypeScript 支持的新项目: ```bash npx create-react-app react_ts_demo --template typescript ``` 这条命令会自动设置好所需的全部基础结构,包括安装必要的依赖项以及配置 Webpack 来处理 TypeScript 文件[^2]。 另外,在某些情况下可能还需要手动安装一些额外的包以确保完整的功能支持: ```bash npm install --save react react-dom npm install --save-dev typescript @types/node @types/react @types/react-dom @types/jest ``` 这些命令用于引入 React 及其 DOM 绑定库作为生产依赖,并添加 TypeScript 编译器及相关类型定义文件到开发环境中[^3]。 除了 `create-react-app` 外,另一种现代的选择是 Vite 构建工具。它提供了更快冷启动速度和热更新机制。要使用 Vite 创建相同类型的项目,则应运行下列指令: ```bash npm create vite@latest my-react-app -- --template react-ts ``` 这同样能够建立一个预设好的 React + TypeScript 开发框架,但采用了更先进的构建技术栈[^4]。 #### 配置完成后的工作流程 一旦上述任一过程完成之后,进入新创建的应用目录并通过简单的脚本即可启动本地服务器来进行进一步开发工作。 ```bash cd react_ts_demo || cd my-react-app npm start ``` 此命令将会编译源码并在浏览器中打开应用程序界面供测试调试之用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值