React+Ts 无缝轮播

import React, { Component, createRef } from 'react'
import "./index.less";
export default class mm extends Component {
    tit = [3, 1, 2, 3, 1];
    state = { Index: 1, left: -100 }
    passBox = createRef<HTMLDivElement>()
    getpass = () => this.passBox.current as HTMLDivElement
    disx: number = 0
    x: number = 0
    start(ev: React.TouchEvent) {
        this.disx = ev.changedTouches[0].pageX;
        document.ontouchend = this.end.bind(this);
        document.ontransitionend = this.trend.bind(this);
    }
    move(ev: React.TouchEvent) {
        this.x = ev.changedTouches[0].pageX - this.disx
        this.setState({ left: this.state.Index * -100 + (this.x / 10) });
    }
    end() {
        var { Index, left } = this.state
        if (this.x > 150 && Index !== 0) Index--;
        if (this.x < -150 && Index <= 4) Index++;
        this.getpass().style.transition = ".3s all";
        this.setState({ Index: Index, left: Index * -100 });
    }
    trend() {
        var { Index, left } = this.state
        this.getpass().style.transition = "";
        if (this.getpass().offsetLeft === 0) {
            left = (this.tit.length - 2) * -100;
            Index = 3;
        } else if (Index === 4) {
            left = -100;
            Index = 1;
        }
        this.setState({ Index: Index, left: left });
    }
    render() {
        var { left } = this.state
        return (
            <div className='passow-Box'>
                <div className='pass-body' style={{ left: left + "vw" }} ref={this.passBox} onTouchStart={this.start.bind(this)} onTouchMove={this.move.bind(this)}>
                    {this.tit.map((item, index) => <div key={index}>
                        {item}
                    </div>)}
                </div>
            </div>
        )
    }
}
  • Index.less
.passow-Box {
    width: 100vw;
    height: 40vh;
    background-color: red;
    position: relative;
    overflow: hidden;

    .pass-body {
        width: 500%;
        height: 40vh;
        position: absolute;

        div {
            float: left;
            width: 100vw;
            height: 40vh;
            text-align: center;
            line-height: 40vh;
            font-size: 20px;

            &:nth-child(1) {
                background-color: skyblue;
            }

            &:nth-child(2) {
                background-color: orange;
            }

            &:nth-child(3) {
                background-color: green;
            }

            &:nth-child(4) {
                background-color: skyblue;
            }

            &:nth-child(5) {
                background-color: orange;
            }
        }
    }
}

以上就是无缝轮播了 ,如有高见可在评论区发言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值