您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“react怎么實現(xiàn)手機(jī)端首頁無縫輪播功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“react怎么實現(xiàn)手機(jī)端首頁無縫輪播功能”吧!
touchstart = this.touchStart.bind(this); touchmove = this.touchMove.bind(this); touchend = this.touchEnd.bind(this); //設(shè)置transform cssTransform(ele, attr, val){ if(!ele.transform){ ele.transform = {}; }; //當(dāng)傳入值時對屬性進(jìn)行設(shè)置。 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(() => { //當(dāng)?shù)竭_(dá)最后一張時 if(this.now === this.props.len - 1){ this.now = this.props.len / 2 - 1; //這兩句代碼很重要,是實現(xiàn)的關(guān)鍵,none是為了不出現(xiàn)平移而是直接跳變 this.LunBoEle.style.transition = 'none'; this.cssTransform(this.LunBoEle, 'translateX', - this.now * this.sliderWidth); } //定時是為了tab函數(shù)中執(zhí)行的csstransform函數(shù)與上面到達(dá)最后一張時的csstransform有先后,不然仍會導(dǎo)致右移; 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); } //觸發(fā) 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); } } //平移結(jié)束 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(){ //注意這邊的清楚很重要,因為用戶在使用時如果后臺修改,用戶刷新,會導(dǎo)致下面的dot出現(xiàn)問題 clearInterval(this.timer); //卸載的同時需要將所有事件清除掉 this.SliderEle.removeEventListener('touchstart', this.touchstart, false); this.SliderEle.removeEventListener('touchmove', this.touchmove, false); this.SliderEle.removeEventListener('touchend', this.touchend, false); } //防止如果只有一張輪播圖時進(jìn)行輪播 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怎么實現(xiàn)手機(jī)端首頁無縫輪播功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。