react+ts实现双小球滑动+交叉

tsx代码:

import React, { Component, createRef } from 'react'
import './index.less'
interface iProps { }
interface iState { }
export default class Eee extends Component<iProps, iState> {
    constructor(props: iProps) {
        super(props)
        this.state = {}
    }
    // 初始位置
    disX: number = 0
    // 要移动的位置
    X: number = 0
    // 线宽
    LineWidth: number = 0
    // 获取父元素box
    Parent = createRef<HTMLDivElement>()
    // 获取线
    Line?: HTMLDivElement
    // 获取左球
    Lball?: HTMLDivElement
    // 获取右球
    Rball?: HTMLDivElement
    // 获取操作元素
    Element?: HTMLDivElement
    // 转html
    FnParent() {
        return this.Parent.current as HTMLDivElement
    }
    // 事件
    FnStart(ev: React.TouchEvent) {
        // 获取操作元素
        this.Element = ev.target as HTMLDivElement
        // 记录初始位置
        this.disX = ev.changedTouches[0].pageX - this.Element.offsetLeft
        // 移动事件
        document.ontouchmove = this.FnMove.bind(this)
    }
    // 移动事件
    FnMove(ev: TouchEvent) {
        // 获取操作元素
        this.Element = ev.target as HTMLDivElement
        // 要移动的位置
        this.X = ev.changedTouches[0].pageX - this.disX
        // 获取左球,右球,线
        this.Lball = this.FnParent().children[0] as HTMLDivElement
        this.Rball = this.FnParent().children[1] as HTMLDivElement
        this.Line = this.FnParent().children[2] as HTMLDivElement

        // 判断小球出边
        if (this.X <= 0) this.X = 0
        if (this.X >= this.FnParent().clientWidth - this.Element.clientWidth) this.X = this.FnParent().clientWidth - this.Element.clientWidth

        // 获取线宽
        this.LineWidth = Math.abs(this.Rball.offsetLeft - this.Lball.offsetLeft)

        // 交叉判断
        if (this.Lball.offsetLeft > this.Rball.offsetLeft) {
            this.Line.style.left = this.Rball.offsetLeft + this.Lball.clientWidth / 2 + 'px'
        } else {
            this.Line.style.left = this.Lball.offsetLeft + this.Lball.clientWidth / 2 + 'px'

        }
        // 设置线宽
        this.Line.style.width = this.LineWidth + 'px'
        // 设置小球位置
        if (this.Element.classList.contains('ball')) this.Element.style.left = this.X + 'px'
    }
    render() {
        return (
            <div className='eee' ref={this.Parent}>
                <div className="ball" onTouchStart={this.FnStart.bind(this)}></div>
                <div className="ball ac" onTouchStart={this.FnStart.bind(this)}></div>
                <div className="line"></div>
            </div>
        )
    }
}

less代码:

.eee {
    width: 400px;
    height: 20px;
    background-color: #ccc;
    position: relative;
    margin: 0 auto;

    .ball {
        width: 40px;
        height: 40px;
        background-color: red;
        border-radius: 50%;
        position: absolute;
        left: 0;
        bottom: 0;
        top: 0;
        margin: auto;
        z-index: 2;
    }

    .ac {
        left: 360px;
    }

    .line {
        width: 400px;
        height: 20px;
        position: absolute;
        background-color: #ff0;
        left: 0;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值