小球 交叉

tsx代码

import React,{Component,createRef} from 'react'
import './pull.less'
class Pull extends Component{
    Indexone=createRef<HTMLDivElement>()
    Parent: any = React.createRef();
    BallLeft: any = React.createRef();
    BallRight: any = React.createRef();
    Line: any = React.createRef();
    LineActive: any = React.createRef();
    Overall: number = 0;
    BallLeftStart: number = 0;
    BallRightStart: number = 0;
    x: number = 0;
    dis: number = 0;
    componentDidMount() {
        this.Overall = this.Parent.offsetWidth;
        document.ontouchstart = this.FnStart.bind(this);
    }
    FnStart(ev: TouchEvent) {
        if ((ev.target as HTMLDivElement).className.indexOf('ball') !== -1) {
            if ((ev.target as HTMLDivElement).className !== "ball ac") {
                this.x = ev.targetTouches[0].pageX - this.BallLeft.offsetLeft;
            } else {
                this.x = ev.changedTouches[0].pageX - this.BallRight.offsetLeft;

            }
            document.ontouchmove = this.FnMove.bind(this)
        }
    }
    FnMove(ev: TouchEvent) {
        if ((ev.target as HTMLDivElement).className.indexOf('ball') !== -1) {
            this.dis = ev.targetTouches[0].pageX - this.x;
            if (this.dis < 0) {
                this.dis = 0;
            }
            if (this.dis > this.Overall) {
                this.dis = this.Overall;
            }
            if ((ev.target as HTMLDivElement).className !== 'ball ac') {
                // 左
                this.Line.style.width = this.dis + 'px';
                this.BallLeft.style.left = this.dis + 'px';
            } else {
                // 右
                this.BallRight.style.left = this.dis + 'px';
            }
            this.LineActive.style.width = Math.abs(this.BallRight.offsetLeft - this.BallLeft.offsetLeft) + 'px';
            this.LineActive.style.left =
                this.BallRight.offsetLeft - this.BallLeft.offsetLeft >= 0 ? this.BallLeft.offsetLeft + 'px' : this.BallRight.offsetLeft + 'px';
        }
    }
    FnEnd(ev: TouchEvent) {

    }
    render(){
        return(<div className='double-ball'>
        <div className='slide-bar' ref={(div: HTMLDivElement) => (this.Parent = div)}>
            <div className='line' ref={(div: HTMLDivElement) => (this.Line = div)}></div>
            <div className='line ac' ref={(div: HTMLDivElement) => (this.LineActive = div)}></div>
            <div className='ball' ref={(div: HTMLDivElement) => (this.BallLeft = div)}></div>
            <div className='ball ac' ref={(div: HTMLDivElement) => (this.BallRight = div)}></div>
        </div>
    </div>)
    }
}
export default Pull

less代码

.double-ball {
    .slide-bar {
      width: 700px;
      height: 20px;
      background: skyblue;
      margin: 150px auto;
      position: relative;
      .line{
        width: 100%;
        position: absolute;
        height: 10px;
        background: #ccc;
        margin-top: 4px;
        &.ac{
          background: #fff;
        }
      }
      .ball {
        position: absolute;
        width: 50px;
        height: 50px;
        background: #f6f6f6;
        border: 1px solid #000;
        border-radius: 50%;
        transform: translateX(-25px);
        z-index: 5;
        margin-top: -14px;
        &.ac{
          left: 50px;
        }
      }
    }
  }
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值