移動端效果之IndexList詳解
寫在前面
接著前面的移動端效果講,這次講解的的是IndexList的實現(xiàn)原理。效果如下:
代碼請看這里:github
移動端效果之picker
移動端效果之cellSwiper
1. 核心解析
總體來說的原理就是當點擊或者滑動右邊的索引條時,通過獲取點擊的索引值來使左邊的內(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" style="display: none;" 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)容的初始化高度和右邊距 // 高度為當前頁面的高度與內(nèi)容top的差值 content.style.marginRight = navWidth + 'px'; content.style.height = currentHeight + 'px';
1.3 綁定滑動事件
在右邊的索引欄上加上滑動事件,當點擊或者滑動的時候觸發(fā)。在源代碼中在touchstart事件的結(jié)尾處,在window上綁定了touchmove與touchend事件,是為了使得滑動得區(qū)域更大,只有在開始的時候在索引欄上觸發(fā)了touchstart事件時,之后再window上觸發(fā)滑動和結(jié)束事件,這就意味著我們在滑動的過程中可以在左側(cè)的內(nèi)容區(qū)域滑動,同時也能達到index的效果。
function handleTouchstart(e) { // 如果不是從索引欄開始滑動,則直接return // 保證了左側(cè)內(nèi)容區(qū)域能夠正?;瑒? if (e.target.tagName !== 'LI') { return; } // 記錄開始的clientX值,這個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,這個API可以去MDN查一下。
如果不是用到多點觸控,changedTouches和touches的區(qū)別并不是特別大,changedTouches在同一點點擊兩次,第二次將不會有touch值。具體可以看這篇文章
下面看一下如何滑動:
function scrollList(y) { // 通過當前的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值與最開始的一個區(qū)域的top值 // 兩者的差值即為要滾動的距離 content.scrollTop = targetDOM.getBoundingClientRect().top - firstSection.getBoundingClientRect().top; // 或者使用scrollIntoView來達到相同的目的 // 不過存在兼容性的問題 // targetDOM.scrollIntoView(); } }
關(guān)于elementFromPoint的API可以看這里
caniuse.com上關(guān)于getBoundingClientRect和scrollIntoView的兼容性
getBoundingClientRect
scrollIntoView
最后需要注銷window上的滑動事件
window.removeEventListener('touchmove', handleTouchMove); window.removeEventListener('touchend', handleTouchEnd);
2. 總結(jié)
分析就這么多,多看源碼能夠?qū)W到優(yōu)秀的設(shè)計理念。比如如果最開始讓我來做的話,我可以就只會在右側(cè)的索引欄上綁定事件,而不會關(guān)聯(lián)左側(cè)的內(nèi)容,這樣滑動的區(qū)域?qū)蟠鬁p小。
同時看源碼可以學(xué)到一些比較偏僻的知識,促使自己去學(xué)習(xí)。比如文中的changedTouches以及elementFromPoint等API的學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用JavaScript判斷一個元素是否在可視范圍內(nèi)的幾種方法
在Web開發(fā)中,有時我們需要知道一個元素是否在用戶的可視范圍內(nèi),以便執(zhí)行相應(yīng)的操作,比如延遲加載圖片、實現(xiàn)懶加載、或是觸發(fā)動畫效果, 本文將詳細介紹使用 JavaScript 如何判斷一個元素是否在可視范圍內(nèi)的幾種方法,需要的朋友可以參考下2024-02-02bootstrap導(dǎo)航、選項卡實現(xiàn)代碼
這篇文章主要為大家詳細介紹了bootstrap導(dǎo)航、選項卡的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12javascript里絕對用的上的字符分割函數(shù)總結(jié)
本節(jié)主要介紹了javascript里比較實用的字符分割函數(shù)的使用,需要的朋友可以參考下2014-07-07JavaScript中數(shù)組的排序、亂序和搜索實現(xiàn)代碼
JavaScript中實現(xiàn)數(shù)組的排序、亂序和搜索,其實所有這些功能,用一個sort()就可以完成了2011-11-11