写在前面接着前面的移动端效果讲,这次讲解的的是indexlist的实现原理。效果如下:
代码请看这里:github
移动端效果之swiper
移动端效果之picker
移动端效果之cellswiper
1. 核心解析总体来说的原理就是当点击或者滑动右边的索引条时,通过获取点击的索引值来使左边的内容滑动到相应的位置。其中怎样滑动到具体的位置,看下面分解:
1.1 基本html代码
<p class="indexlist">
<ul class="indexlist-content" id="content">
<!-- 需要生成的内容 -->
</ul>
<p class="indexlist-nav" id="nav">
<ul class="indexlist-navlist" id="navlist">
<-- 需要生成的索引条 --> <
/ul>
</p>
<p class="indexlist-indicator" style="display: none;" id="indicator">
</p>
</p>
1.2 dom初始化由于饿了么组件库中的indexlist是采用vue组件生成dom,我这里大致使用javascript来模拟生成dom。
// 内容填充function initialdom() {
// d.data 获取内容数据
var data = d.data;
var contenthtml = '';
var navhtml = '';
// 初始化内容和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"><p class="cell-wrapper"><p class="cell-title"><span class="cell-text">'+ item +'</span></p></p></a>';
});
contenthtml += '</ul></li>';
});
content.innerhtml = contenthtml;
navlist.innerhtml = navhtml;}// 样式初始化if (!currentheight) {
currentheight = document.documentelement.clientheight -content.getboundingclientrect().top;}// 右边索引栏的宽度navwidth = nav.clientwidth;// 左边内容的初始化高度和右边距// 高度为当前页面的高度与内容top的差值content.style.marginright = navwidth + 'px';content.style.height = currentheight + 'px';
1.3 绑定滑动事件在右边的索引栏上加上滑动事件,当点击或者滑动的时候触发。在源代码中在touchstart事件的结尾处,在window上绑定了touchmove与touchend事件,是为了使得滑动得区域更大,只有在开始的时候在索引栏上触发了touchstart事件时,之后再window上触发滑动和结束事件,这就意味着我们在滑动的过程中可以在左侧的内容区域滑动,同时也能达到index的效果。
function handletouchstart(e) {
// 如果不是从索引栏开始滑动,则直接return
// 保证了左侧内容区域能够正常滑动
if (e.target.tagname !== 'li') {
return;
}
// 记录开始的clientx值,这个clientx值将在之后的滑动中持续用到,用于定位
navoffsetx = e.changedtouches[0].clientx;
// 内容滑动到指定区域
scrolllist(e.changedtouches[0].clienty);
if (indicatortime) {
cleartimeout(indicatortime);
}
moving = true;
// 在window区域注册滑动和结束事件
window.addeventlistener('touchmove', handletouchmove, { passive: false });
window.addeventlistener('touchend', handletouchend);}
这里面用到了e.changedtouches,这个api可以去mdn查一下。
如果不是用到多点触控,changedtouches和touches的区别并不是特别大,changedtouches在同一点点击两次,第二次将不会有touch值。具体可以看这篇文章
下面看一下如何滑动:
function scrolllist(y) {
// 通过当前的y值以及之前记录的clientx值来获得索引栏中的对应item
var currentitem = document.elementfrompoint(navoffsetx, y);
if (!currentitem || !currentitem.classlist.contains('indexlist-navitem')) {
return;
}
// 显示指示器
currentindicator = currentitem.innertext;
indicator.innertext = currentindicator;
indicator.style.display = '';
// 找到左侧内容的对应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];
// 通过对比要滑动到的区域的top值与最开始的一个区域的top值
// 两者的差值即为要滚动的距离
content.scrolltop = targetdom.getboundingclientrect().top - firstsection.getboundingclientrect().top;
// 或者使用scrollintoview来达到相同的目的
// 不过存在兼容性的问题
// targetdom.scrollintoview();
}}
关于elementfrompoint的api可以看这里
caniuse.com上关于getboundingclientrect和scrollintoview的兼容性
getboundingclientrect
scrollintoview
最后需要注销window上的滑动事件
window.removeeventlistener('touchmove', handletouchmove);window.removeeventlistener('touchend', handletouchend);
2. 总结分析就这么多,多看源码能够学到优秀的设计理念。比如如果最开始让我来做的话,我可以就只会在右侧的索引栏上绑定事件,而不会关联左侧的内容,这样滑动的区域将会大大减小。
同时看源码可以学到一些比较偏僻的知识,促使自己去学习。比如文中的changedtouches以及elementfrompoint等api的学习。
以上就是移动端indexlist效果介绍的详细内容。