react+ts实现小球互推,小球交叉

index.tsx

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

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);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React TS是一个用于构建用户界面的JavaScript库,而VR看房可以通过使用WebVR技术来实现。在React TS实现VR看房需要以下步骤: 1. 了解WebVR技术:WebVR是一种使Web应用程序能够在虚拟现实设备上运行的技术,它提供了一种在虚拟现实设备上渲染内容的方式。 2. 导入WebVR库:在React TS项目中,需要导入与WebVR相关的库,例如A-Frame或React 360等,这些库提供了一些用于构建VR界面的组件和功能。 3. 创建VR场景:使用React TS的组件化开发方式,可以通过在项目中创建VR场景组件来构建VR看房的界面。可以使用库中提供的组件来构建3D场景、添加虚拟现实设备的交互等。 4. 加载房屋模型:在VR场景中加载房屋模型,可以使用库中提供的加载器将3D房屋模型导入到场景中,并设置适当的位置和缩放。 5. 添加交互功能:为了实现VR看房的功能,需要添加一些交互功能,例如移动、旋转和缩放房屋模型,点击房间以获取更多信息等。可以使用库中提供的交互组件或自定义事件处理程序来实现这些功能。 6. 兼容不同的设备:考虑到不同的虚拟现实设备,需要在React TS项目中进行一些适配工作,以确保VR看房界面在不同设备上的兼容性。 总的来说,使用React TS实现VR看房需要对WebVR技术有一定的了解,并结合具体的库和组件来构建VR场景、加载房屋模型,并添加交互功能。不同设备的兼容性也是一个需要考虑的因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值