DIV CSS 佈局教程網

iscroll.js 上拉下拉刷新後無法回彈的原因與解法
編輯:關於JavaScript     

用過 iscroll.js 做上拉下拉刷新的朋友,大概都遇過這個狀況:在 iOS 的 Safari 裡,手指往上或往下拖到刷新時,一旦手指滑出螢幕範圍再放開,頁面就卡在那裡彈不回來。有人卡在這裡遲遲無法解決,索然乾脆不管;更極端一點的,直接放棄 HTML 改用原生前後端重寫整個頁面。

其實這個問題不算難解,只是網路上能搜到的說明不多,很多討論群裡反覆有人提問。因此把這裡的處理方式記錄下來,希望對遇到同樣狀況的朋友有幫助。

基本的刷新結構

 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(); } } }); 

彈不回來的原因

問題出在事件時序上。iScroll 的回彈動畫是掛在 touchend 之後執行的,但當手指已經滑出螢幕邊緣時,瀏覽器不會再觸發 touchend,後續的回彈動畫自然永遠等不到被呼叫,畫面就停在拖曳的最後狀態。

解法:提早一步手動觸發動畫

思路很單純——既然等不到 touchend,那就在手指還在螢幕內、即將抵達邊緣的那一刻,主動呼叫回彈。做法是在 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; } ...... } 

這幾行在判斷什麼

先釐清三個變數:this.y 是內容已經滾動的垂直距離,this.maxScrollY 是上拉時的最大垂直位移,this.pointY 則是指尖目前所在的垂直座標。

第一個條件處理上拉:this.y < this.maxScrollY 代表正處於上拉進程中,再加上 this.pointY < 1 表示手指已經貼到螢幕上緣,這一刻就呼叫 this.scrollTo(0, this.maxScrollY, 400),讓頁面在 400 毫秒內補完回彈,接著 return 中止這次 move 的後續處理,避免和原本的 flip 判斷互相打架。

第二個條件是下拉的鏡像版本:手指接近 window.innerHeight - 1(螢幕下緣)時,直接 scrollTo(0, 0, 400) 回到頂部。

實作時記得把 400 這個時間值調成和你 CSS transition 一致的節奏,否則會出現動畫長短不一、看起來卡頓的情形。這個小修正是純粹的事件補救,不需要改動 iScroll 本身,升級函式庫時也不必擔心衝突。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved