HTML5手機開發——滾動和慣性緩動

來源:互聯網
上載者:User

標籤:

1. 滾動

  以下是三種實現方式:

  1) 利用原生的css屬性 overflow: scroll

<div id="parent" style="overflow:scroll;>
    <div id=‘content‘>內容地區</div>
</div>

  Notice:

  在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;

});
parent.addEventListener(‘touchend‘, function(e) {
    // do touchend
    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) 使用iScroll4架構

  var scroll = new iScroll(‘parent‘, {

  hScrollbar: false,

  vScrollbar: true,

  checkDOMChanges : true

  });

  架構官網:http://cubiq.org/iscroll-4

  2.慣性緩動

  思路:取手指最後一段時間在螢幕上划動的平均速度v,讓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‘, ‘‘);
    // 設定top值移動content
    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) {
    // do touchend
    var nowY = e.touches[0].pageY;
    var moveY = nowY - lastY;
    var contentTop = content.style.top.replace(‘px‘, ‘‘);
    var contentY = (parseInt(contentTop) + moveY);
    // 設定top值移動content
    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);
});

HTML5手機開發——滾動和慣性緩動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.