Jquery 插件iScroll 實(shí)現(xiàn)下拉刷新,上拉加載!

今天給大家?guī)淼氖荋5的上拉加載和下拉刷新特效。仿的是原生app的樣式,不過外觀可能有點(diǎn)差別。

首先我們使用的是jq的iScroll插件來編寫。

到官網(wǎng)下載即可:

https://github.com/cubiq/iscroll

下面是實(shí)現(xiàn)的運(yùn)行效果:


上下拉動(dòng),刷新和加載

下面的是頁面html代碼,要符合iScroll 代碼原則:

(代碼放不進(jìn)來,只能截圖了)
html代碼

下面就是關(guān)鍵的js代碼了


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

/**

*下拉刷新 (自定義實(shí)現(xiàn)此方法)

* myScroll.refresh();? //數(shù)據(jù)加載完成后,調(diào)用界面更新方法

*/

functionpullDownAction() {

? setTimeout(function() {

? var el, li, i;

? el=document.getElementById('ul');

? for(i=0;i<3;i++) {

? ? ?li=document.createElement('li');

? ? li.innerText='添加'+(++generatedCount);

? ? 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!

}

/**

*滾動(dòng)翻頁 (自定義實(shí)現(xiàn)此方法)

* myScroll.refresh();? //數(shù)據(jù)加載完成后,調(diào)用界面更新方法

*/

functionpullUpAction() {

setTimeout(function() {// <-- Simulate network congestion, remove setTimeout from production!

varel,li,i;

el=document.getElementById('ul');

for(i=0;i<3;i++) {

li=document.createElement('li');

li.innerText='添加'+(++generatedCount);

el.appendChild(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控件

*/

functionloaded() {

pullDownEl=document.getElementById('pullDown');

pullDownOffset=pullDownEl.offsetHeight;

pullUpEl=document.getElementById('pullUp');

pullUpOffset=pullUpEl.offsetHeight;

myScroll= newiScroll('app', {

scrollbarClass:'myScrollbar',/*重要樣式*/

useTransition: false,/*此屬性不知用意,本人從true改為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('app').style.left='0'; },800);

}

//初始化綁定iScroll控件

document.addEventListener('touchmove',function(e) {e.preventDefault(); },false);

document.addEventListener('DOMContentLoaded',loaded,false);

在這個(gè)代碼實(shí)現(xiàn)后,僅僅能做到刷新和加載,如果想要模擬app里面的上拉刷新和下拉加載,我們還需要一點(diǎn)css3 的動(dòng)畫來實(shí)現(xiàn)

html,body{

width:100%;

height:100%;

}

body,ul,li{

padding:0;

margin:0;

border:0;

}

body{

font-size:12px;

-webkit-user-select:none;

-webkit-text-size-adjust:none;

font-family:helvetica;

}

#app{

position:absolute;z-index:1;

top:0px;bottom:50px;left:0;

width:100%;

background:#555;

overflow:auto;

}

#scroller{

position:relative;

/* -webkit-touch-callout:none;*/

-webkit-tap-highlight-color:rgba(0,0,0,0);

float:left;

width:100%;

padding:0;

}

#scroller ul{

position:relative;

list-style:none;

padding:0;

margin:0;

width:100%;

text-align:left;

}

#scroller li{

padding:0 10px;

height:40px;

line-height:40px;

border-bottom:1px solid#ccc;

border-top:1px solid#fff;

background-color:#fafafa;

font-size:14px;

}

#scroller li>a{

display:block;

}

/**

*

*下拉樣式Pull down styles

*

*/

#pullDown,#pullUp{

background:#fff;

height:40px;

line-height:40px;

padding:5px10px;

border-bottom:1px solid#ccc;

font-weight:bold;

font-size:14px;

color:#888;

}

#pullDown.pullDownIcon,#pullUp.pullUpIcon{

display:block;float:left;

width:30px;height:30px;

background:url('img/pull-icon@2x.png')0 0no-repeat;

-webkit-background-size:30px60px;background-size:30px60px;

-webkit-transition-property:-webkit-transform;

-webkit-transition-duration:250ms;

}

#pullDown.pullDownIcon{

-webkit-transform:rotate(0deg)translateZ(0);

}

#pullUp.pullUpIcon{

-webkit-transform:rotate(-180deg)translateZ(0);

}

/**

*動(dòng)畫效果css3代碼

*/

#pullDown.flip.pullDownIcon{

-webkit-transform:rotate(-180deg)translateZ(0);

}

#pullUp.flip.pullUpIcon{

-webkit-transform:rotate(0deg)translateZ(0);

}

#pullDown.loading.pullDownIcon,#pullUp.loading.pullUpIcon{

background-position:0 100%;

-webkit-transform:rotate(0deg)translateZ(0);

-webkit-transition-duration:0ms;

-webkit-animation-name:loading;

-webkit-animation-duration:2s;

-webkit-animation-iteration-count:infinite;

-webkit-animation-timing-function:linear;

}

@-webkit-keyframesloading{

from{-webkit-transform:rotate(0deg)translateZ(0); }

to{-webkit-transform:rotate(360deg)translateZ(0); }

}

以上代碼就可以實(shí)現(xiàn)開始的gif動(dòng)圖中的內(nèi)容,如果有問題,可以留言。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1. tab列表折疊效果 html: 能源系統(tǒng)事業(yè)部 崗位名稱: 工作地點(diǎn) 崗位名...
    lilyping閱讀 1,900評(píng)論 0 1
  • 單例模式 適用場景:可能會(huì)在場景中使用到對(duì)象,但只有一個(gè)實(shí)例,加載時(shí)并不主動(dòng)創(chuàng)建,需要時(shí)才創(chuàng)建 最常見的單例模式,...
    Obeing閱讀 2,109評(píng)論 1 10
  • 今天天氣好晴朗處處好風(fēng)光伴隨著好天氣的到來心情都更加明朗了呢是不是該學(xué)點(diǎn)燒腦的內(nèi)容了呢傻球也要出來曬曬太陽咯~ C...
    Iris_mao閱讀 635評(píng)論 0 2
  • 青山墨人閱讀 291評(píng)論 5 6
  • 不敢不拼命,因?yàn)楦緵]有其他的路,這就是道只有一個(gè)選項(xiàng)的選擇題,多么痛的領(lǐng)悟,領(lǐng)悟之后也只能忍著痛前行。 環(huán)望四周...
    輝狼_fish閱讀 147評(píng)論 3 1