博客已遷移至http://zlwis.me。瀏覽器
使用過iscroll.js的上拉下拉刷新效果的朋友應該都碰到過這個問題:在iOS的瀏覽器中,上拉或下拉刷新時,當手指劃出屏幕後,頁面沒法彈回。不少人由於解決不了這個問題,乾脆就那樣不解決了,還有的直接就不用HTML了,使用原生代替HTML頁面。app
相信不少朋友也有本身的解決辦法,只是沒寫出來,因此網上都搜不到解決方案。在不少QQ羣裏面也有不少人在問該怎麼解決這個問題,因此我寫這篇文章記錄一下個人解決方案,但願對一些朋友有所幫助。動畫
上拉下拉刷新的主要代碼:this
myScroll = new iScroll('wrapper', { vScrollbar: false, useTransition: true, topOffset: pullDownOffset, onRefresh: function () { if (pullDownEl.className.match('loading')) { pullDownEl.className = ''; pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...'; } else if (pullUpEl.className.match('loading')) { pullUpEl.className = ''; pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...'; } }, onScrollMove: function () { if (this.y > 5 && !pullDownEl.className.match('flip')) { pullDownEl.className = 'flip'; pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...'; 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 = 'Release to refresh...'; 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 = 'Loading...'; pullDownAction(); } else if (pullUpEl.className.match('flip')) { pullUpEl.className = 'loading'; pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...'; pullUpAction(); } } });
頁面沒法彈回的緣由在於:手指劃出屏幕後touchend
事件沒法觸發,回彈動畫就沒法執行。解決辦法就是:當手指接近屏幕邊緣的時候,手動觸發動畫方法。code
在onScrollMove
方法中插入判斷代碼:事件
onScrollMove: function () { if((this.y < this.maxScrollY) && (this.pointY < 1)){ this.scrollTo(0, this.maxScrollY, 400); return; } else if (this.y > 0 && (this.pointY > window.innerHeight - 1)) { this.scrollTo(0, 0, 400); return; } ...... }
下面解釋一下這段代碼的意思。ip
this.y
是頁面已經滾動的垂直距離,this.maxScrollY
是最大垂直滾動距離,this.pointY
手指當前的垂直座標。get
當this.y < this.maxScrollY
,就是已經處於上拉的過程,當(this.y < this.maxScrollY) && (this.pointY < 1)
時,處於上拉且手指已經觸及屏幕邊緣,這時候手動觸發this.scrollTo(0, this.maxScrollY, 400)
,頁面就開始回彈。博客
下拉過程也能夠同理分析。it
歡迎留下你的解決方法。