以前项目中的列表是采用的IScroll
,可是在使用IScroll
有一个问题就是:当内容不足全屏的时候,是木有办法往下拉的,这样就达不到刷新的目的了。【这是本人工做中遇到的,具体例子具体分析,这里只做一个参考】javascript
大体的例子是这样的:css
<style> * { margin: 0; padding: 0; } html,body,.container { width: 100%; height: 100%; } .container>ul>li { padding: 15px 20px; text-align: center; border-bottom: 1px solid #ccc; } </style> <div id="container" class="container"> <ul class="scroller"> <li>item1</li> <li>item2</li> <li>item3</li> <li>item4</li> <li>item5</li> </ul> </div> <script src="https://cdn.bootcss.com/iScroll/5.2.0/iscroll.min.js"></script> <script> var myScroll = null; function onLoad() { myScroll = new IScroll('container'); } window.addEventListener('DOMContentLoaded', onLoad, false); </script>
那么,既然超过一屏是能够刷新的,那咱们就来逛逛代码吧。在github上搜索iscroll,打开第一个,找到src
下面的core.js
。html
首先既然要下拉,确定会触发touchstart
、touchmove
以及touchend
事件。搜索touchmove
,很好,在_initEvents
中的注册了这个事件。java
_initEvents: function (remove) { // ... // 这里省略若干代码 if ( utils.hasTouch && !this.options.disableTouch ) { eventType(this.wrapper, 'touchstart', this); eventType(target, 'touchmove', this); eventType(target, 'touchcancel', this); eventType(target, 'touchend', this); } // ... },
好吧,看到这里的时候,我表示懵了一下逼,这不就是个绑定事件么?this
又是一个什么鬼,而后我去查了一下文档,发现了这么一个东西。文档地址css3
target.addEventListener(type, listener[, options]); target.addEventListener(type, listener[, useCapture]); target.addEventListener(type, listener[, useCapture, wantsUntrusted ]); // // Gecko/Mozilla only listener 当所监听的事件类型触发时,会接收到一个事件通知(实现了 Event 接口的对象)对象。listener 必须是一个实现了 EventListener 接口的对象,或者是一个函数
木有看错,listener
是一个对象或者是一个函数。前提是这个对象实现了EventListener
接口。咱们接着往下看,发现了这么一个例子。git
var Something = function(element) { // |this| is a newly created object this.name = 'Something Good'; this.handleEvent = function(event) { console.log(this.name); // 'Something Good', as this is bound to newly created object switch(event.type) { case 'click': // some code here... break; case 'dblclick': // some code here... break; } }; // Note that the listeners in this case are |this|, not this.handleEvent element.addEventListener('click', this, false); element.addEventListener('dblclick', this, false); // You can properly remove the listeners element.removeEventListener('click', this, false); element.removeEventListener('dblclick', this, false); } var s = new Something(document.body);
而后在去IScroll
的源码去找,发现了一样的实现方式。在default
文件夹中有一个handleEvent.js
。github
好了,这个梗先告一段落。仍是继续看源码。在handleEvent.js
中,有这么一段东西。app
handleEvent: function (e) { switch ( e.type ) { case 'touchstart': case 'pointerdown': case 'MSPointerDown': case 'mousedown': this._start(e); break; case 'touchmove': case 'pointermove': case 'MSPointerMove': case 'mousemove': this._move(e); break; case 'touchend': case 'pointerup': case 'MSPointerUp': case 'mouseup': case 'touchcancel': case 'pointercancel': case 'MSPointerCancel': case 'mousecancel': this._end(e); break; // ... } } };
发如今start/move/end
分别调用了内部方法_start/_move/_end
方法。去看看这三个方法,看其中可能会引发不会滑动的点。函数
在_start
方法中,看到这样的几行代码,会不会是直接返回了呢?分析分析:测试
if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) { return; } // ... var point = e.touches ? e.touches[0] : e, pos; this.initiated = utils.eventType[e.type]; this.moved = false;
initiated
属性在最开始确定是没有的,而enabled
默认是true
,因此在最开始执行这个方法的时候是不会返回的,而是会给initiated
这个属性设置当前的eventType
值,这个值会在_move
方法中用到。重点来看看_move
方法。
if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { return; }
首先来进行类型判断,由于在_start
方法中已经定义了这个值,因此这里也不会返回。接着往下看:
if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) { return; }
【其实是两次click事件的模拟】若是两次滑动的时间大于了300ms,而且只要一个方向上的位移少于10像素,那么也是会返回的。那么会不会呢,打个断点测试一下就知道了。这里就不贴图了,实际中的测试结果是,每一次移动确定是在300ms之内的,这里之因此判断300ms,主要是click
事件执行会有一个300ms的延迟。而每一次移动,因为手指的触点比较大,仍是会大于10像素的,即便两次不大于10像素,也是不影响的。因此这点不会返回。那么继续接着看:
// If you are scrolling in one direction lock the other if ( !this.directionLocked && !this.options.freeScroll ) { if ( absDistX > absDistY + this.options.directionLockThreshold ) { this.directionLocked = 'h'; // lock horizontally } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) { this.directionLocked = 'v'; // lock vertically } else { this.directionLocked = 'n'; // no lock } } if ( this.directionLocked == 'h' ) { if ( this.options.eventPassthrough == 'vertical' ) { e.preventDefault(); } else if ( this.options.eventPassthrough == 'horizontal' ) { this.initiated = false; return; } deltaY = 0; } else if ( this.directionLocked == 'v' ) { if ( this.options.eventPassthrough == 'horizontal' ) { e.preventDefault(); } else if ( this.options.eventPassthrough == 'vertical' ) { this.initiated = false; return; } deltaX = 0; }
第一个条件判断只要是定义了此次滑动的方向是什么。h
表示水平方向,v
表示竖直方向。咱们是要向下滑动,因此咱们关注的是竖直方向。看第二个条件判断,若是是竖直方向,那么将水平方向的deltaX
值变为0。这样作的目的是保持绝对的竖直方向。由于移动实际仍是根据元素的位移值来的。当probe
的版本为2如下的时候,是根据css3的transform
属性来移动位移的,为3版本的时候是根据决定对位来移动的。因此这里只要不把咱们的deltaY
置为0就说明木有什么问题。继续往下看代码:
deltaX = this.hasHorizontalScroll ? deltaX : 0; deltaY = this.hasVerticalScroll ? deltaY : 0; newX = this.x + deltaX; newY = this.y + deltaY; // ... // 这里是移动 this._translate(newX, newY);
测试中发现,这个hasVerticalScroll
一直是false
,那么deltaY
一直就是0,也就是移动了也白移动。找到问题缘由。那么,这个hasVerticalScroll
是从哪里来的?全局找呀找,在refresh
中找到这样几行代码:
this.wrapperWidth = this.wrapper.clientWidth; this.wrapperHeight = this.wrapper.clientHeight; var rect = utils.getRect(this.scroller); /* REPLACE START: refresh */ this.scrollerWidth = rect.width; this.scrollerHeight = rect.height; this.maxScrollX = this.wrapperWidth - this.scrollerWidth; this.maxScrollY = this.wrapperHeight - this.scrollerHeight; /* REPLACE END: refresh */ this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0; this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
refresh
方法会在IScroll
实例化的时候调用一次。粗略一看,scrollY
内置为true
,因此只有maxScrollY
会大于0。往上看。this.wrapperHeight - this.scrollerHeight
确定是大于0的呀,这就是问题所在。
那么看看咱们最开始代码,这里的wrapperHeight
为文档高度,scrollerHeight
为内容高度,因此wrapperHeight
高度始终大于scrollHeight
。可是,手机端页面夹杂的列表,通常都有头部、底部,而中间部分通常都会采用padding
的形式来使得列表在全局滚动,这样就不须要每次都要特定地计算列表的高度。
针对以上问题,只要咱们可以使内部的滚动部分高度大于容器高度,那么就能触发滚动。
能够设置一个min-height
属性为900px
(900只是一个示例,只要够大就能够),这样就能够保证能够滑动。
计算当前的容器高度,而后比容器高度多一个像素便可。