React+ts仿小球模块左右滑动交叉碰撞

该博客主要介绍了一个使用React编写的组件,该组件实现了两个小球通过触摸拖动的功能,小球的位置变化映射为两个数值,并实时更新显示。通过监听touchstart、touchmove和touchend事件,计算小球的移动距离并更新状态,同时调整相邻小球的位置,实现了类似滑块的效果。代码中还包含了一些边界条件的处理,确保小球不会超出容器范围。
摘要由CSDN通过智能技术生成

index.tsx

import { Component } from "react";
import './xiaoqou.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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React+TypeScript脚手架是一种用于创建React项目的工具。通过使用create-react-app脚手架,可以快速搭建一个React项目,并且使用TypeScript作为项目的开发语言。\[1\] 使用步骤如下: 1. 首先,需要安装create-react-app工具。可以通过以下命令进行安装: ``` npm i create-react-app -g ``` 或者 ``` yarn add create-react-app -g ``` 2. 然后,使用create-react-app命令来创建一个React+TypeScript项目。命令的格式如下: ``` npx create-react-app <项目名> --template typescript ``` 注意,项目名不要使用中文,否则会报错。\[2\] 3. 创建完成后,可以在VSCode中打开项目。可以看到项目中已经生成了React项目的雏形,组件的后缀为.tsx,这说明成功创建了React+TypeScript项目。\[1\] 4. 在项目中,可以根据需要进行配置。通常,可以在App组件中进行配置,例如引入所需的组件、样式和路由等。\[3\] 希望以上信息对你有帮助!如果还有其他问题,请随时提问。 #### 引用[.reference_title] - *1* *3* [react+ts搭建](https://blog.csdn.net/m0_56540662/article/details/124267829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [React+ts搭建脚手架](https://blog.csdn.net/weixin_51612593/article/details/120249135)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值