這篇文章主要介紹移動端效果之IndexList的實(shí)現(xiàn)方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)十余年經(jīng)驗(yàn)成就非凡,專業(yè)從事成都做網(wǎng)站、成都網(wǎng)站建設(shè),成都網(wǎng)頁設(shè)計(jì),成都網(wǎng)頁制作,軟文發(fā)布平臺,廣告投放等。十余年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:18982081108,我們期待您的來電!
寫在前面
接著前面的移動端效果講,這次講解的的是IndexList的實(shí)現(xiàn)原理。效果如下:
代碼請看這里:github
移動端效果之swiper
移動端效果之picker
移動端效果之cellSwiper
1. 核心解析
總體來說的原理就是當(dāng)點(diǎn)擊或者滑動右邊的索引條時(shí),通過獲取點(diǎn)擊的索引值來使左邊的內(nèi)容滑動到相應(yī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初始化
由于餓了么組件庫中的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)點(diǎn)擊或者滑動的時(shí)候觸發(fā)。在源代碼中在touchstart事件的結(jié)尾處,在window上綁定了touchmove與touchend事件,是為了使得滑動得區(qū)域更大,只有在開始的時(shí)候在索引欄上觸發(fā)了touchstart事件時(shí),之后再window上觸發(fā)滑動和結(jié)束事件,這就意味著我們在滑動的過程中可以在左側(cè)的內(nèi)容區(qū)域滑動,同時(shí)也能達(dá)到index的效果。
function handleTouchstart(e) { // 如果不是從索引欄開始滑動,則直接return // 保證了左側(cè)內(nèi)容區(qū)域能夠正常滑動 if (e.target.tagName !== 'LI') { return; } // 記錄開始的clientX值,這個(gè)clientX值將在之后的滑動中持續(xù)用到,用于定位 navOffsetX = e.changedTouches[0].clientX; // 內(nèi)容滑動到指定區(qū)域 scrollList(e.changedTouches[0].clientY); if (indicatorTime) { clearTimeout(indicatorTime); } moving = true; // 在window區(qū)域注冊滑動和結(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)擊兩次,第二次將不會有touch值。具體可以看這篇文章
下面看一下如何滑動:
function scrollList(y) { // 通過當(dāng)前的y值以及之前記錄的clientX值來獲得索引欄中的對應(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)容的對應(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]; // 通過對比要滑動到的區(qū)域的top值與最開始的一個(gè)區(qū)域的top值 // 兩者的差值即為要滾動的距離 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上的滑動事件
window.removeEventListener('touchmove', handleTouchMove); window.removeEventListener('touchend', handleTouchEnd);
以上是“移動端效果之IndexList的實(shí)現(xiàn)方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁題目:移動端效果之IndexList的實(shí)現(xiàn)方法
網(wǎng)站地址:http://chinadenli.net/article4/gshgie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、靜態(tài)網(wǎng)站、面包屑導(dǎo)航、小程序開發(fā)、、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)