移動(dòng)端效果之IndexList詳解
寫在前面
接著前面的移動(dòng)端效果講,這次講解的的是IndexList的實(shí)現(xiàn)原理。效果如下:

代碼請(qǐng)看這里:github
移動(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" style="display: none;" 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)前頁(yè)面的高度與內(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);
2. 總結(jié)
分析就這么多,多看源碼能夠?qū)W到優(yōu)秀的設(shè)計(jì)理念。比如如果最開始讓我來做的話,我可以就只會(huì)在右側(cè)的索引欄上綁定事件,而不會(huì)關(guān)聯(lián)左側(cè)的內(nèi)容,這樣滑動(dòng)的區(qū)域?qū)?huì)大大減小。
同時(shí)看源碼可以學(xué)到一些比較偏僻的知識(shí),促使自己去學(xué)習(xí)。比如文中的changedTouches以及elementFromPoint等API的學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS簡(jiǎn)單的輪播的圖片滾動(dòng)實(shí)例
JS簡(jiǎn)單的輪播的圖片滾動(dòng)實(shí)例,需要的朋友可以參考一下2013-06-06
javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
jquery實(shí)現(xiàn)右側(cè)欄菜單選擇操作
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)右側(cè)欄菜單選擇操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
JS驗(yàn)證日期的格式Y(jié)YYY-mm-dd 具體實(shí)現(xiàn)
JavaScript基于對(duì)象去除數(shù)組重復(fù)項(xiàng)的方法

