您好,登錄后才能下訂單哦!
本篇內容主要講解“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 <SliderItem item={item} count={this.props.len} key={'item' + idx} />; }); let dotNodes = []; let count = this.props.len / 2; for(let i = 0; i < count; i++){ //為第一個dot點加上selected dotNodes[i] = ( <span key={'dot' + i} className={'slider-dot' + (i === 0 ? ' slider-dot-selected' : '')}> </span> ); } return ( <div className="slider"> <ul className="lunbo" style={{width: (this.props.len) * 100 + '%'}}> {itemNodes} </ul> <div className="slider-dots-wrap"> {dotNodes} </div> </div>
到此,相信大家對“react怎么實現手機端首頁無縫輪播功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。