溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2021-06-29 09:18:06 來源:億速云 閱讀:186 作者:小新 欄目:web開發(fā)

這篇文章主要介紹移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

寫在前面

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

移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法

代碼請看這里: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初始化

由于餓了么組件庫中的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é)束事件,這就意味著我們在滑動(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ū)域注冊滑動(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

移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法

scrollIntoView

移動(dòng)端效果之IndexList的實(shí)現(xiàn)方法

最后需要注銷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)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI