本文分享行動端網頁開發中「滾動」與「慣性緩動」的實作方法,供參考。
1)純 CSS:overflow: scroll
<div id="parent" style="overflow:scroll;> <div id='content'>內容區域</div> </div>
問題:在 Android 上有 bug,滾動到底後會跳回最頂端。解法是使用後面兩種方式。
2)JS 手刻
思路:比較手指在螢幕上移動前後的位置差,據此改變內容元素 content 的位置。
第一步:把 parent 的 overflow 設為 hidden,content 設為 position: relative、top: 0。
第二步:監聽 touch 事件。
var parent = document.getElementById('parent'); parent.addEventListener('touchstart', function(e) { // do touchstart }); parent.addEventListener('touchmove', function(e) { // do touchmove }); parent.addEventListener('touchend', function(e) { // do touchend });
第三步:實作滾動(此處僅示範垂直方向)。
var parent = document.getElementById('parent'); var content = document.getElementById('content') var startY = 0; // 初始位置 var lastY = 0; // 上一次位置 parent.addEventListener('touchstart', function(e) { lastY = startY = e.touches[0].pageY; }); parent.addEventListener('touchmove', function(e) { var nowY = e.touches[0].pageY; var moveY = nowY - lastY; var contentTop = content.style.top.replace('px', ''); // 設定 top 值移動 content content.style.top = (parseInt(contentTop) + moveY) + 'px'; lastY = nowY; });
第四步:優化。上述程式碼在手機上的流暢度明顯不如 PC。
3)使用 iScroll 4 框架
var scroll = new iScroll('parent', { hScrollbar: false, vScrollbar: true, checkDOMChanges : true });
官網:https://cubiq.org/iscroll-4
思路:取手指最後一段時間在螢幕上滑動的平均速度 v,讓 v 依一個遞減函式變化,直到速度降到 0 或撞到邊界為止。
/** 此處僅實作垂直滾動 */ var parent = document.getElementById('parent'); var content = document.getElementById('content') var startY = 0; // 初始位置 var lastY = 0; // 上一次位置 /** 用於緩動的變量 */ var lastMoveTime = 0; var lastMoveStart = 0; var stopInertiaMove = false; // 是否停止緩動 parent.addEventListener('touchstart', function(e) { lastY = startY = e.touches[0].pageY; /** 緩動 */ lastMoveStart = lastY; lastMoveTime = e.timeStamp || Date.now(); stopInertiaMove = true; }); parent.addEventListener('touchmove', function(e) { var nowY = e.touches[0].pageY; var moveY = nowY - lastY; var contentTop = content.style.top.replace('px', ''); content.style.top = (parseInt(contentTop) + moveY) + 'px'; lastY = nowY; /** 緩動 */ var nowTime = e.timeStamp || Date.now(); stopInertiaMove = true; if(nowTime - lastMoveTime > 300) { lastMoveTime = nowTime; lastMoveStart = nowY; } }); parent.addEventListener('touchend', function(e) { var nowY = e.touches[0].pageY; var moveY = nowY - lastY; var contentTop = content.style.top.replace('px', ''); var contentY = (parseInt(contentTop) + moveY); content.style.top = contentY + 'px'; lastY = nowY; /** 緩動 */ var nowTime = e.timeStamp || Date.now(); var v = (nowY - lastMoveStart) / (nowTime - lastMoveTime); //最後一段時間手指滑動速度 stopInertiaMove = false; (function(v, startTime, contentY) { var dir = v > 0 ? -1 : 1; //加速度方向 var deceleration = dir*0.0006; var duration = v / deceleration; // 速度遞減至 0 所需時間 var dist = v * duration / 2; //最終移動距離 function inertiaMove() { if(stopInertiaMove) return; var nowTime = e.timeStamp || Date.now(); var t = nowTime-startTime; var nowV = v + t*deceleration; // 速度方向改變表示已到 0 if(dir*nowV < 0) { return; } var moveY = (v + nowV)/2 * t; content.style.top = (contentY + moveY) + "px"; setTimeout(inertiaMove, 10); } inertiaMove(); })(v, nowTime, contentY); });
幾個實作細節值得留意:用 stopInertiaMove 旗標讓使用者能在緩動途中用手指中斷動畫,這是原生滾動感的關鍵;只取「最後 300ms 內」的速度而非整段手勢的平均速度,可以避免長距離拖曳後甩動過頭;dir * nowV < 0 用來判斷速度已反向,也就是該停止。
若要在正式專案中使用,仍強烈建議直接採用成熟框架——手刻版本在边界回弹、多軸滾動與效能上都需要額外打磨。
推薦幾個程式碼格式化與美化工具:線上 JavaScript 美化格式化工具(https://tools.jb51.net/code/js)、JavaScript 壓縮/格式化/加密工具(https://tools.jb51.net/code/jscompress)、JSON 線上格式化工具(https://tools.jb51.net/code/jsoncodeformat)、線上 JSON 檢驗美化工具(https://tools.jb51.net/code/json)。
更多 jQuery 相關內容有興趣的讀者可查看本站專題:《jQuery 常用外掛及用法總結》、《jQuery 中 Ajax 用法總結》、《jQuery 表格操作技巧彙總》、《jQuery 拖拽特效與技巧總結》、《jQuery 擴充技巧總結》、《jQuery 常見經典特效彙總》、《jQuery 動畫與特效用法總結》及《jQuery 選擇器用法總結》。