iscroll上拉加载更多下拉刷新

项目中需要用到滚动,本想自己写,但总会有一些大bug,最后不得不采用iscroll来做,不过这个做起来还真是方便,效果图如下:


这个是最终的项目效果图,下面贴出代码供大家参考:

注意:所贴代码并不是上面的全部代码,经过修改过后的

结构:

<div id="wrapper">
	<div id="scroller">
		<div id="pullDown">
			<span class="pullDownLabel">下拉刷新</span>
		</div>
		<ul id="thelist">
			<!--<li>原始数据</li>-->
		</ul>
		<div id="pullUp">
			<span class="pullUpLabel">上拉加载更多</span>
		</div>
	</div>
</div>

js代码:

var myScroll,pullDownEl, pullDownOffset,pullUpEl, pullUpOffset,generatedCount = 0;

function loaded() {
	//动画部分
	pullDownEl = document.getElementById('pullDown');
	pullDownOffset = pullDownEl.offsetHeight;
	pullUpEl = document.getElementById('pullUp');	
	pullUpOffset = pullUpEl.offsetHeight;
	myScroll = new iScroll('wrapper', {
		useTransition: true,
		topOffset: pullDownOffset,
		onRefresh: function () {
			if (pullDownEl.className.match('loading')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新';
			} else if (pullUpEl.className.match('loading')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多';
			}
		},
		onScrollMove: function () {
		
			if (this.y > 5 && !pullDownEl.className.match('flip')) {
				pullDownEl.className = 'flip';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '释放刷新';
				this.minScrollY = 0;
			} else if (this.y < 5 && pullDownEl.className.match('flip')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
				this.minScrollY = -pullDownOffset;
			} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
				pullUpEl.className = 'flip';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '释放刷新';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
				this.maxScrollY = pullUpOffset;
			}
		},
		onScrollEnd: function () {
			if (pullDownEl.className.match('flip')) {
				pullDownEl.className = 'loading';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中';				
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl.className.match('flip')) {
				pullUpEl.className = 'loading';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中';				
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});
	
	loadAction();
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);//阻止冒泡
document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 0); }, false);

//初始状态,加载数据
function loadAction(){
	var el, li;
	el = document.getElementById('thelist');
	for (i=0; i<10; i++) {
		li = document.createElement('li');
		li.innerText = '这个是数据哦--' + (++generatedCount);
		el.appendChild(li, el.childNodes[0]);
	}
	myScroll.refresh();
}

//下拉刷新当前数据
function pullDownAction () {
	setTimeout(function () {
		//这里执行刷新操作
		
		myScroll.refresh();	
	}, 400);
}

//上拉加载更多数据
function pullUpAction () {
	setTimeout(function () {
		var el, li;
		el = document.getElementById('thelist');
		for (i=0; i<10; i++) {
			li = document.createElement('li');
			li.innerText = '上拉加载--' + (++generatedCount);
			el.appendChild(li, el.childNodes[0]);
		}
		myScroll.refresh();
	}, 400);
}

大家可以根据自己的需求去修改为自己想要的效果,注意需要先引入iscroll文件才可以。


如果觉得有帮助请支持我吧!

猜你喜欢

转载自blog.csdn.net/u014307349/article/details/80901660