基于iScroll實(shí)現(xiàn)下拉刷新和上滑加載效果
本文實(shí)例為大家分享了iScroll下拉刷新上滑加載展示的具體代碼,供大家參考,具體內(nèi)容如下
html代碼:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" /> <title>iScroll下拉刷新上滑加載</title> <link rel="stylesheet" href="style/main.css"/> </head> <body> <div class="header">header</div> <div id="wrapper"> <div id="scroller"> <div id="pullDown"> <span class="pullDownLabel">下拉刷新</span> </div> <ul id="thelist"> <!--<li>原始數(shù)據(jù)</li>--> </ul> <div id="pullUp"> <span class="pullUpLabel">上拉加載更多</span> </div> </div> </div> <div class="footer">footer</div> <script type="text/javascript" src="script/iscroll.js"></script> <script type="text/javascript" src="script/main.js"></script> </body> </html>
css代碼:
body,ul,li {padding:0;margin:0;border:0}
body {font-size:12px;font-family:microsoft yahei}
.header {position:absolute;top:0; left:0;width:100%;height:45px;line-height:45px;font-size:16px;text-align:center;background:#e6e6e6}
.footer {position:absolute;bottom:0; left:0;width:100%;height:48px;line-height:48px;font-size:16px;text-align:center;background:#e6e6e6}
#wrapper {position:absolute;top:45px; bottom:48px;left:0;width:100%}
#scroller li {padding:0 10px;height:60px;line-height:60px;background:#ecf6ff;margin-top:10px}
#pullDown, #pullUp {padding:0 10px;height:30px;line-height:30px;color:#888;text-align:center}
js代碼:
var myScroll,pullDownEl, pullDownOffset,pullUpEl, pullUpOffset,generatedCount = 0;
function loaded() {
//動(dòng)畫部分
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);
//初始狀態(tài),加載數(shù)據(jù)
function loadAction(){
var el, li;
el = document.getElementById('thelist');
for (i=0; i<10; i++) {
li = document.createElement('li');
li.innerText = '初始數(shù)據(jù)--' + (++generatedCount);
el.appendChild(li, el.childNodes[0]);
}
myScroll.refresh();
}
//下拉刷新當(dāng)前數(shù)據(jù)
function pullDownAction () {
setTimeout(function () {
//這里執(zhí)行刷新操作
myScroll.refresh();
}, 400);
}
//上拉加載更多數(shù)據(jù)
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);
}
示例:
demo地址:http://test.ixiewei.com/iscroll-4-demo
下載地址:iScroll下拉刷新上滑加載
截圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- H5基于iScroll實(shí)現(xiàn)下拉刷新和上拉加載更多
- IScroll那些事_當(dāng)內(nèi)容不足時(shí)下拉刷新的解決方法
- iscroll-probe實(shí)現(xiàn)下拉刷新和下拉加載效果
- iOS下拉刷新 UIScrollVie異常閃動(dòng)問題
- 基于iscroll.js實(shí)現(xiàn)下拉刷新和上拉加載效果
- 基于HTML5上使用iScroll實(shí)現(xiàn)下拉刷新,上拉加載更多
- iscroll碰到Select無法選擇下拉刷新的解決辦法
- iscroll.js的上拉下拉刷新時(shí)無法回彈的解決方法
- JQuery插件iScroll實(shí)現(xiàn)下拉刷新,滾動(dòng)翻頁特效
- iscroll實(shí)現(xiàn)下拉刷新功能
相關(guān)文章
JavaScript DOM節(jié)點(diǎn)操作方法總結(jié)
下面小編就為大家?guī)硪黄狫avaScript DOM節(jié)點(diǎn)操作方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
js實(shí)現(xiàn)截取某個(gè)字符串前面的內(nèi)容
這篇文章主要介紹了js實(shí)現(xiàn)截取某個(gè)字符串前面的內(nèi)容,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Javascript中構(gòu)造函數(shù)要注意的一些坑
JavaScript語言是一門面向?qū)ο蟮恼Z言,但JS中并沒有類的概念的。于是JavaScript采用構(gòu)造函數(shù)的方式來模擬類的效果,即我們通過函數(shù)來創(chuàng)建對象。這也證明了函數(shù)在JavaScript中具有非常重要的地位。本文主要介紹了Javascript中構(gòu)造函數(shù)的一些坑,需要的朋友可以參考。2017-01-01
IE7中javascript操作CheckBox的checked=true不打勾的解決方法
在IE7下,生成的Checkbox無法正確的打上勾。 原因是 chkbox控件還沒初始化(appendChild),就開始操作它的結(jié)果2009-12-12
使用OpenLayers3 添加地圖鼠標(biāo)右鍵菜單
這篇文章主要介紹了使用OpenLayers3 添加地圖鼠標(biāo)右鍵菜單的相關(guān)資料,需要的朋友可以參考下2015-12-12
JavaScript編程的10個(gè)實(shí)用小技巧
盡管我使用Javascript來做開發(fā)有很多年了,但它常有一些讓我很驚訝的小特性。對于我來說,Javascript是需要持續(xù)不斷的學(xué)習(xí)的。2014-04-04

