千家信息网

react怎么实现手机端首页无缝轮播功能

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容主要讲解"react怎么实现手机端首页无缝轮播功能",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"react怎么实现手机端首页无缝轮播功能"吧!to
千家信息网最后更新 2026年11月01日react怎么实现手机端首页无缝轮播功能

本篇内容主要讲解"react怎么实现手机端首页无缝轮播功能",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"react怎么实现手机端首页无缝轮播功能"吧!

touchstart = this.touchStart.bind(this);    touchmove = this.touchMove.bind(this);    touchend = this.touchEnd.bind(this);    //设置transform    cssTransform(ele, attr, val){        if(!ele.transform){            ele.transform = {};        };        //当传入值时对属性进行设置。        if(arguments.length > 2){            ele.transform[attr] = val;            var sval = '';            for(var s in ele.transform){                if(s === 'translateX'){                    sval += s + '(' + ele.transform[s] + 'px)';                }                //如果实在不理解,可以console.log(sval)可以看到到最后一张时会有一个先跳到第二张再快速到第三张的过程                ele.style.WebkitTransform = ele.style.transform = sval;            }        }        else{            val = ele.transform[attr];            if(typeof val === 'undefined'){                if(attr === 'translateX'){                    val = 0;                }            };            return val;        }    }    //自动轮播    auto(){        clearInterval(this.timer);        this.timer = setInterval(() => {            //当到达最后一张时            if(this.now === this.props.len - 1){                this.now = this.props.len / 2 - 1;                //这两句代码很重要,是实现的关键,none是为了不出现平移而是直接跳变                this.LunBoEle.style.transition = 'none';                this.cssTransform(this.LunBoEle, 'translateX', - this.now * this.sliderWidth);            }    //定时是为了tab函数中执行的csstransform函数与上面到达最后一张时的csstransform有先后,不然仍会导致右移;            setTimeout(() => {                 this.now++;                this.tab();            }, 30);        }, this.props.delay * 1000);    }    tab(){        this.LunBoEle.style.transition = '.5s';        this.cssTransform(this.LunBoEle, 'translateX', -this.now * this.sliderWidth);        let SelectIndex = this.now % (this.props.len / 2);        $('.slider-dots-wrap span').eq(SelectIndex).addClass('slider-dot-selected').siblings().removeClass('slider-dot-selected');    }    componentDidMount() {        this.LunBoEle = document.querySelector('ul.lunbo');        this.SliderEle = document.querySelector('.slider');        this.sliderWidth = $('.slider').width();        this.cssTransform(this.LunBoEle, 'translateX', 0);        this.auto.bind(this)();        this.SliderEle.addEventListener('touchstart', this.touchstart, false);        this.SliderEle.addEventListener('touchmove', this.touchmove, false);        this.SliderEle.addEventListener('touchend', this.touchend, false);    }//触发    touchStart(e: TouchEvent){        e.stopPropagation();        if(!this.stopped){            clearInterval(this.timer);            this.LunBoEle.style.transition = 'none';            let moveX = this.cssTransform.bind(this)(this.LunBoEle, 'translateX');                this.now = Math.round(-moveX / this.sliderWidth);            if(this.now === 0){                this.now = this.props.len / 2;            }else if(this.now === this.props.len - 1){                this.now = this.props.len / 2 - 1;            }            this.cssTransform(this.LunBoEle, 'translateX', -this.now * this.sliderWidth);            this.startPoint = e.changedTouches[0].pageX;            this.startEle = this.cssTransform.bind(this)(this.LunBoEle, 'translateX');        }    };//移动    touchMove(e: TouchEvent){        e.preventDefault();        e.stopPropagation();        if(!this.stopped){            let endPoint = e.changedTouches[0].pageX;            let disX = endPoint - this.startPoint;            this.cssTransform.bind(this)(this.LunBoEle, 'translateX', disX + this.startEle);        }    }//平移结束    touchEnd(e: TouchEvent){        e.stopPropagation();        if(!this.stopped){            let moveX = this.cssTransform.bind(this)(this.LunBoEle, 'translateX');            //这边我是对移动做了判断            if(Math.abs(moveX) > Math.abs(this.now * this.sliderWidth)){                this.now = Math.ceil(-moveX / this.sliderWidth);            }else{                this.now = Math.floor(-moveX / this.sliderWidth);            }            this.tab.bind(this)();            this.auto.bind(this)();        }    }    componentWillUnmount(){        //注意这边的清楚很重要,因为用户在使用时如果后台修改,用户刷新,会导致下面的dot出现问题        clearInterval(this.timer);        //卸载的同时需要将所有事件清除掉        this.SliderEle.removeEventListener('touchstart', this.touchstart, false);        this.SliderEle.removeEventListener('touchmove', this.touchmove, false);        this.SliderEle.removeEventListener('touchend', this.touchend, false);    }    //防止如果只有一张轮播图时进行轮播    componentDidUpdate(){        if((this.props.len / 2) === 1){            clearInterval(this.timer);            this.stopped = true;        }        else{            this.stopped = false;        }    }    render() {        let itemNodes = this.props.items.map((item, idx) => {            return ;        });        let dotNodes = [];        let count = this.props.len / 2;        for(let i = 0; i < count; i++){            //为第一个dot点加上selected            dotNodes[i] = (                                            );        }        return (            
    {itemNodes}
{dotNodes}

到此,相信大家对"react怎么实现手机端首页无缝轮播功能"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

功能 手机 无缝 首页 重要 内容 函数 用户 学习 平移 移动 实用 更深 清楚 事件 代码 先后 关键 兴趣 动轮 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 疫情对互联网科技公司影响 青岛互联网科技公司 致骋互联网科技 华中科技大学互联网 掌上梦互联网科技 上海亿帐通互联网科技 佰商互联网科技有限公司 互联网科技素材视频竖屏 互联网和高科技公司 互联网科技热点资讯 南山科技园互联网公司有哪些 宁夏众和广鑫互联网科技有限公司 湖南一路畅行互联网科技 互联网科技扶贫时间 科技互联网海报 山东子淼互联网科技有限公司 山东星卡互联网科技有限公司官网 广东工业互联网大赛深圳学泰科技 郑州欣桑互联网科技有限公司 美国五大互联网科技公司市值多少 上海睿民互联网科技有限公司 互联网科技的英文 隆华科技工业互联网 港股科技和互联网 互联网时代的科技公司 本届互联网大会上的黑科技 同城互联网络科技 科技互联网经济论坛 马化腾互联网科技教学 安徽九吞吴互联网科技有限公司
0