在觸控裝置上,「下拉刷新、上拉載入更多」已經是標準手勢。iScroll 這支外掛能處理平滑的捲動與回彈,並透過 onScrollMove、onScrollEnd 等回呼讓我們插入自己的邏輯。以下拆解完整實作。
<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>
</ul>
<div id="pullUp">
<span class="pullUpIcon"></span>
<span class="pullUpLabel">上拉加載更多...</span>
</div>
</div>
</div>
翻頁本身透過 Ajax 呼叫一般處理程序(.ashx),把頁碼傳過去,取回分頁後的 JSON 陣列。
function pullDownAction() {
setTimeout(function () {
var el, li;
el = document.getElementById('thelist');
$.ajax({
type : "GET",
url : "LoadMore.ashx",
data : { page: generatedCount },
dataType: "json",
success : function (data) {
$(data).each(function () {
li = document.createElement('li');
li.innerHTML = '<img src="' + this.src + '"/>';
el.insertBefore(li, el.childNodes[0]); // 插在列表最前面
});
}
});
myScroll.refresh(); // 資料載入後務必呼叫,重新計算捲動範圍
}, 1000); // 模擬網路延遲,正式環境請移除
}
function pullUpAction() {
setTimeout(function () {
var el, li;
el = document.getElementById('thelist');
$.ajax({
type : "GET",
url : "LoadMore.ashx",
data : { page: generatedCount },
dataType: "json",
success : function (data) {
$(data).each(function () {
li = document.createElement('li');
li.innerHTML = '<img src="' + this.src + '"/>';
el.appendChild(li); // 接在列表後面
});
}
});
myScroll.refresh();
}, 1000);
}
兩者的差別只在插入位置:刷新用 insertBefore 放最前面,載入更多用 appendChild 放最後面。
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();
} else if (pullUpEl.className.match('flip')) {
pullUpEl.className = 'loading';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '加載中...';
pullUpAction();
}
}
});
setTimeout(function () {
document.getElementById('wrapper').style.left = '0';
}, 800);
}
// 禁用原生捲動,避免與 iScroll 的手勢衝突
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
整個手勢靠 className 的三種狀態串起來:預設、flip(拉超過門檻、提示可以放手)、loading(正在請求)。onScrollMove 負責隨位移切換狀態並改文案,onScrollEnd 看到 flip 就正式觸發請求,onRefresh 則在資料回來後把狀態復原。
最容易漏掉的一步是 myScroll.refresh():iScroll 在初始化時會量好可捲動的範圍,內容變多之後若不重新計算,捲到底了就再也拉不動。
另外提醒,範例中 touchmove 全面 preventDefault 會讓整頁無法原生捲動,實作時請確認只有列表區域需要這個處理;同時記得為「已無更多資料」設計一個結束狀態,避免使用者一直下拉卻沒有回應。