這篇文章主要介紹移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)公司是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開發(fā),軟件開發(fā),微信小程序開發(fā),10多年建站對(duì)成都銅雕雕塑等多個(gè)領(lǐng)域,擁有多年的網(wǎng)站維護(hù)經(jīng)驗(yàn)。寫在前面
接著前面的移動(dòng)端效果講,這次講解的的是IndexList的實(shí)現(xiàn)原理。效果如下:
代碼請(qǐng)看這里:github
移動(dòng)端效果之swiper
移動(dòng)端效果之picker
移動(dòng)端效果之cellSwiper
1. 核心解析
總體來說的原理就是當(dāng)點(diǎn)擊或者滑動(dòng)右邊的索引條時(shí),通過獲取點(diǎn)擊的索引值來使左邊的內(nèi)容滑動(dòng)到相應(yīng)的位置。其中怎樣滑動(dòng)到具體的位置,看下面分解:
1.1 基本html代碼
<div class="indexlist"> <ul class="indexlist-content" id="content"> <!-- 需要生成的內(nèi)容 --> </ul> <div class="indexlist-nav" id="nav"> <ul class="indexlist-navlist" id="navList"> <-- 需要生成的索引條 --> </ul> </div> <div class="indexlist-indicator" id="indicator"></div> </div>
1.2 DOM初始化
由于餓了么組件庫(kù)中的indexList是采用vue組件生成DOM,我這里大致使用javascript來模擬生成DOM。
// 內(nèi)容填充 function initialDOM() { // D.data 獲取內(nèi)容數(shù)據(jù) var data = D.data; var contentHtml = ''; var navHtml = ''; // 初始化內(nèi)容和NAV data.forEach(function(d) { var index = d.index; var items = d.items; navHtml += '<li class="indexlist-navitem">'+ index +'</li>'; contentHtml += '<li class="indexsection" data-index="'+ index +'"><p class="indexsection-index">'+ index +'</p><ul>'; items.forEach(function(item) { contentHtml += '<a class="cell"><div class="cell-wrapper"><div class="cell-title"><span class="cell-text">'+ item +'</span></div></div></a>'; }); contentHtml += '</ul></li>'; }); content.innerHTML = contentHtml; navList.innerHTML = navHtml; } // 樣式初始化 if (!currentHeight) { currentHeight = document.documentElement.clientHeight -content.getBoundingClientRect().top; } // 右邊索引欄的寬度 navWidth = nav.clientWidth; // 左邊內(nèi)容的初始化高度和右邊距 // 高度為當(dāng)前頁面的高度與內(nèi)容top的差值 content.style.marginRight = navWidth + 'px'; content.style.height = currentHeight + 'px';
1.3 綁定滑動(dòng)事件
在右邊的索引欄上加上滑動(dòng)事件,當(dāng)點(diǎn)擊或者滑動(dòng)的時(shí)候觸發(fā)。在源代碼中在touchstart事件的結(jié)尾處,在window上綁定了touchmove與touchend事件,是為了使得滑動(dòng)得區(qū)域更大,只有在開始的時(shí)候在索引欄上觸發(fā)了touchstart事件時(shí),之后再window上觸發(fā)滑動(dòng)和結(jié)束事件,這就意味著我們?cè)诨瑒?dòng)的過程中可以在左側(cè)的內(nèi)容區(qū)域滑動(dòng),同時(shí)也能達(dá)到index的效果。
function handleTouchstart(e) { // 如果不是從索引欄開始滑動(dòng),則直接return // 保證了左側(cè)內(nèi)容區(qū)域能夠正?;瑒?dòng) if (e.target.tagName !== 'LI') { return; } // 記錄開始的clientX值,這個(gè)clientX值將在之后的滑動(dòng)中持續(xù)用到,用于定位 navOffsetX = e.changedTouches[0].clientX; // 內(nèi)容滑動(dòng)到指定區(qū)域 scrollList(e.changedTouches[0].clientY); if (indicatorTime) { clearTimeout(indicatorTime); } moving = true; // 在window區(qū)域注冊(cè)滑動(dòng)和結(jié)束事件 window.addEventListener('touchmove', handleTouchMove, { passive: false }); window.addEventListener('touchend', handleTouchEnd); }
這里面用到了e.changedTouches,這個(gè)API可以去MDN查一下。
如果不是用到多點(diǎn)觸控,changedTouches和touches的區(qū)別并不是特別大,changedTouches在同一點(diǎn)點(diǎn)擊兩次,第二次將不會(huì)有touch值。具體可以看這篇文章
下面看一下如何滑動(dòng):
function scrollList(y) { // 通過當(dāng)前的y值以及之前記錄的clientX值來獲得索引欄中的對(duì)應(yīng)item var currentItem = document.elementFromPoint(navOffsetX, y); if (!currentItem || !currentItem.classList.contains('indexlist-navitem')) { return; } // 顯示指示器 currentIndicator = currentItem.innerText; indicator.innerText = currentIndicator; indicator.style.display = ''; // 找到左側(cè)內(nèi)容的對(duì)應(yīng)section var targets = [].slice.call(sections).filter(function(section) { var index = section.getAttribute('data-index'); return index === currentItem.innerText; }); var targetDOM; if (targets.length > 0) { targetDOM = targets[0]; // 通過對(duì)比要滑動(dòng)到的區(qū)域的top值與最開始的一個(gè)區(qū)域的top值 // 兩者的差值即為要滾動(dòng)的距離 content.scrollTop = targetDOM.getBoundingClientRect().top - firstSection.getBoundingClientRect().top; // 或者使用scrollIntoView來達(dá)到相同的目的 // 不過存在兼容性的問題 // targetDOM.scrollIntoView(); } }
關(guān)于elementFromPoint的API可以看這里
caniuse.com上關(guān)于getBoundingClientRect和scrollIntoView的兼容性
getBoundingClientRect
scrollIntoView
最后需要注銷window上的滑動(dòng)事件
window.removeEventListener('touchmove', handleTouchMove); window.removeEventListener('touchend', handleTouchEnd);
以上是“移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁題目:移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法-創(chuàng)新互聯(lián)
URL地址:http://muchs.cn/article10/eijgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站制作、網(wǎng)站改版、品牌網(wǎng)站制作、ChatGPT、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容