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 = '松手開始更新...'; 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 = '松手開始更新...'; 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頁(yè)面隨滾動(dòng)條動(dòng)態(tài)加載效果的簡(jiǎn)單實(shí)現(xiàn)(推薦)
下面小編就為大家?guī)?lái)一篇JQuery頁(yè)面隨滾動(dòng)條動(dòng)態(tài)加載效果的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02jQuery對(duì)象的length屬性用法實(shí)例
這篇文章主要介紹了jQuery對(duì)象的length屬性用法,實(shí)例分析了length屬性獲取集合中對(duì)象數(shù)目的使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12jQuery使用中可能被XSS攻擊的一些危險(xiǎn)環(huán)節(jié)提醒
XSS指的是跨站腳本攻擊,比如人們常說(shuō)的向$傳入字符串或者字符串轉(zhuǎn)換可執(zhí)行函數(shù)等一些安全方面值得注意的細(xì)節(jié),下面就為大家整理了jQuery使用中可能被XSS攻擊的一些危險(xiǎn)環(huán)節(jié)提醒2016-05-05表單類各種類型(文本框)失去焦點(diǎn)效果jquery代碼
基于jquery實(shí)現(xiàn)表單類各種類型(文本框)失去焦點(diǎn)效果,代碼簡(jiǎn)單實(shí)用,感興趣的朋友可以參考下,希望對(duì)你有所幫助2013-04-04JQuery擴(kuò)展插件Validate—4設(shè)置錯(cuò)誤提示的樣式
JQuery擴(kuò)展插件Validate—4設(shè)置錯(cuò)誤提示的樣式,使用Validate的朋友可以參考下。2011-09-09jQuery基礎(chǔ)學(xué)習(xí)技巧總結(jié)
jQuery基礎(chǔ)學(xué)習(xí)技巧總結(jié)...2007-06-06基于jquery的simpleValidate簡(jiǎn)易驗(yàn)證插件
簡(jiǎn)易驗(yàn)證插件simpleValidate使用心得,基于JQuery,修改小BUG后能兼容各瀏覽器,附自用版本供下載2014-01-01防止jQuery ajax Load使用緩存的方法小結(jié)
本篇文章主要是對(duì)防止jQuery ajax Load使用緩存的方法進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02深入理解Jquery表單驗(yàn)證(使用formValidator)
表單驗(yàn)證在web中中的應(yīng)用很廣,本篇文章主要介紹了Jquery表單驗(yàn)證(使用formValidator),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01