JQuery插件iScroll實(shí)現(xiàn)下拉刷新,滾動(dòng)翻頁(yè)特效
JQuery插件:iScroll
頁(yè)面布局:
<div id="wrapper">
<div id="scroller">
<div id="pullDown">
<span class="pullDownIcon"></span><span class="pullDownLabel">下拉刷新...</span>
</div>
<ul id="thelist">
<li>
<img src="img/page1_img1.jpg" />
</li>
<li>
<img src="img/page1_img2.jpg" />
</li>
<li>
<img src="img/page1_img3.jpg" />
</li>
<li>
<img src="img/page1_img1.jpg" />
</li>
<li>
<img src="img/page1_img2.jpg" />
</li>
<li>
<img src="img/page1_img3.jpg" />
</li>
</ul>
<div id="pullUp">
<span class="pullUpIcon"></span><span class="pullUpLabel">上拉加載更多...</span>
</div>
</div>
翻頁(yè),是通過(guò)ajax請(qǐng)求,把頁(yè)碼傳入一般處理程序,在一般處理程序中獲得分頁(yè)后的數(shù)據(jù)返回json數(shù)組對(duì)象。
下拉刷新:
/**
* 下拉刷新 (自定義實(shí)現(xiàn)此方法)
* myScroll.refresh(); // 數(shù)據(jù)加載完成后,調(diào)用界面更新方法
*/
function pullDownAction() {
setTimeout(function () {
var el, li, i;
el = document.getElementById('thelist');
//==========================================
$.ajax({
type: "GET",
url: "LoadMore.ashx",
data: { page: generatedCount },
dataType: "json",
success: function (data) {
var json = data;
$(json).each(function () {
li = document.createElement('li');
// li.innerText = 'Generated row ' + (++generatedCount);
li.innerHTML = '<img src="' + this.src + '"/>';
el.insertBefore(li, el.childNodes[0]);
})
}
});
myScroll.refresh(); //數(shù)據(jù)加載完成后,調(diào)用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
}, 1000); // <-- Simulate network congestion, remove setTimeout from production!
}
上拉刷新
function pullUpAction() {
setTimeout(function () {
var el, li, i;
el = document.getElementById('thelist');
//==========================================
$.ajax({
type: "GET",
url: "LoadMore.ashx",
data: { page: generatedCount },
dataType: "json",
success: function (data) {
var json = data;
$(json).each(function () {
li = document.createElement('li');
// li.innerText = 'Generated row ' + (++generatedCount);
li.innerHTML = '<img src="' + this.src + '"/>;
el.insertBefore(li, el.childNodes[0]);
})
}
});
//============================================
myScroll.refresh(); // 數(shù)據(jù)加載完成后,調(diào)用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
}, 1000); // <-- Simulate network congestion, remove setTimeout from production!
}
初始化
/**
* 初始化iScroll控件
*/
function loaded() {
pullDownEl = document.getElementById('pullDown');
pullDownOffset = pullDownEl.offsetHeight;
pullUpEl = document.getElementById('pullUp');
pullUpOffset = pullUpEl.offsetHeight;
myScroll = new iScroll('wrapper', {
scrollbarClass: 'myScrollbar', /* 重要樣式 */
useTransition: false,
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 = '松手開(kāi)始更新...';
this.minScrollY = 0;
} else if (this.y < 5 && pullDownEl.className.match('flip')) {
pullDownEl.className = '';
pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
this.minScrollY = -pullDownOffset;
} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
pullUpEl.className = 'flip';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手開(kāi)始更新...';
this.maxScrollY = this.maxScrollY;
} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
pullUpEl.className = '';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加載更多...';
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?)
}
}
});
setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}
//初始化綁定iScroll控件
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
相關(guān)文章
jQuery對(duì)象與DOM對(duì)象之間的相互轉(zhuǎn)換
本文主要給大家介紹的是jQuery對(duì)象與DOM對(duì)象之間的相互轉(zhuǎn)換的方法和示例,非常實(shí)用,這里推薦給有需要的小伙伴參考下。2015-03-03
jQuery實(shí)現(xiàn)寬屏圖片輪播實(shí)例教程
這篇文章為大家分享了一個(gè)jQuery實(shí)現(xiàn)寬屏圖片輪播實(shí)例教程,外觀看上去非常大氣,感興趣的小伙伴們可以參考一下2015-11-11
通過(guò)jquery toggleClass()屬性制作文章段落更改背景顏色
jQuery制作文章段落更改背景顏色屬性jquery toggleClass()屬性。文中給大家附實(shí)例代碼和源碼,感興趣的朋友參考下吧2018-05-05
JQuery實(shí)現(xiàn)簡(jiǎn)單時(shí)尚快捷的氣泡提示插件
在程序提交后,為了提高用戶體驗(yàn)我們需要驗(yàn)證并提示出錯(cuò)的位置,利用JQuery我們可以輕松實(shí)現(xiàn)氣泡提示,需要的朋友可以了解下2012-12-12
詳解jQuery停止動(dòng)畫——stop()方法的使用
本文主要對(duì)jQuery停止動(dòng)畫——stop()方法的使用進(jìn)行詳細(xì)介紹,對(duì)學(xué)習(xí)jQuery動(dòng)畫有很好的幫助,下面就跟小編一起來(lái)看下吧2016-12-12
Jquery cookie插件實(shí)現(xiàn)原理代碼解析
這篇文章主要介紹了Jquery cookie插件實(shí)現(xiàn)原理代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
jQuery EasyUI API 中文文檔 - MenuButton菜單按鈕使用介紹
jQuery EasyUI API 中文文檔 - MenuButton菜單按鈕使用介紹,使用jQuery EasyUI的朋友可以參考下。2011-10-10

