IScroll那些事——内容不足时下拉刷新

之前项目中的列表是采用的IScroll,但是在使用IScroll有一个问题就是:当内容不足全屏的时候,是木有办法往下拉的,这样就达不到刷新的目的了。【这是本人工作中遇到的,具体例子具体分析,这里只作一个参考

大致的例子是这样的:

<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

1. 思路

首先既然要下拉,肯定会触发touchstarttouchmove以及touchend事件。搜索touchmove,很好,在_initEvents中的注册了这个事件。

_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又是一个什么鬼,然后我去查了一下文档,发现了这么一个东西。文档地址

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接口。我们接着往下看,发现了这么一个例子。

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

好了,这个梗先告一段落。还是继续看源码。在handleEvent.js中,有这么一段东西。

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的形式来使得列表在全局滚动,这样就不需要每次都要特定地计算列表的高度。

2. 解决方案

针对以上问题,只要我们能够使内部的滚动部分高度大于容器高度,那么就能触发滚动。

2.1 粗略做法

可以设置一个min-height属性为900px(900只是一个示例,只要够大就可以),这样就可以保证可以滑动。

2.2 精准做法

计算当前的容器高度,然后比容器高度多一个像素即可。

posted @ 2017-07-18 00:12  糊糊糊糊糊了  阅读(1702)  评论(0编辑  收藏  举报