DIV CSS 佈局教程網

用 JavaScript 打造重力彈跳與拖拽的物理動畫
編輯:關於JavaScript     

想讓頁面上的方塊像真實物體一樣被丟出去、撞牆、反彈,最後慢慢停下來?這篇筆記把整個過程封裝成一個可重複使用的小元件 gcdMove,只要傳入元素與初始速度,就能得到重力、碰撞與拖拽三段行為。

線上展示

https://www.ihuxu.com/project/gcdmove/

調用方式

var GCDM = gcdMove(oDiv, 100, 0);
GCDM.startMove();  // 開始運動
GCDM.stopMove();   // 結束運動

參數說明

obj 是要操作的元素,通常是一個 div;iSpeedX 與 iSpeedY 分別是水平(向右為正)與垂直(向下為正)的初始速度,兩者都可以傳 0,讓元素先靜止在頁面上等使用者拖曳。

核心思路

整個效果其實只靠一個 30 毫秒的定時器:

  • 每一輪先把 iSpeedY 加上 3,模擬重力加速度;
  • 用 offsetLeft / offsetTop 加上速度算出下一個位置;
  • 碰到視窗邊界時,把速度乘以 -0.8 完成反彈,同時乘 0.8 模擬能量損耗;
  • 水平速度小於 1 時歸零,當元素靜止在底部就清除定時器,動畫自然結束。

拖拽的部分則是透過 onmousedown 記錄滑鼠與元素的位移差 disX / disY,在 document.onmousemove 中更新位置,並用前後兩次座標差反推速度;放手時把這個速度交給 start(),元素就會「被甩出去」。

完整程式碼

/**
* @Desc 重力碰撞拖拽運動 - Gravity Crash Drag Move(gcdMove)
* @Author GenialX
* @URL www.ihuxu.com
* @Date 2013.06.22
*/
function gcdMove(obj, iSpeedX, iSpeedY) {
    _this = this;              // public identifier
    var iTimer;
    var iLastX = 0;
    var iLastY = 0;

    // 運動主迴圈
    start = function () {
        clearInterval(iTimer);
        iTimer = setInterval(function () {
            iSpeedY += 3;                                   // 重力加速度
            var l = obj.offsetLeft + iSpeedX;
            var t = obj.offsetTop + iSpeedY;

            if (t >= document.documentElement.clientHeight - obj.offsetHeight) {
                iSpeedY *= -0.8;                            // 底部碰撞反彈
                iSpeedX *= 0.8;
                t = document.documentElement.clientHeight - obj.offsetHeight;
            } else if (t <= 0) {
                iSpeedY *= -1;                              // 頂部碰撞反彈
                iSpeedX *= 0.8;
                t = 0;
            }

            if (l >= document.documentElement.clientWidth - obj.offsetWidth) {
                iSpeedX *= -0.8;                            // 右側碰撞反彈
                l = document.documentElement.clientWidth - obj.offsetWidth;
            } else if (l <= 0) {
                iSpeedX *= -0.8;                            // 左側碰撞反彈
                l = 0;
            }

            if (Math.abs(iSpeedX) < 1) { iSpeedX = 0; }     // 摩擦力
            if (iSpeedX == 0 && iSpeedY == 0 &&
                t == document.documentElement.clientHeight - obj.offsetHeight) {
                clearInterval(iTimer);                      // 完全靜止則停止
            }

            obj.style.left = l + 'px';
            obj.style.top = t + 'px';
        }, 30);
    }

    // 拖拽
    _this.startMove = function () {
        obj.onmousedown = function (ev) {
            clearInterval(iTimer);
            var oEvent = ev || event;
            var disX = oEvent.clientX - obj.offsetLeft;
            var disY = oEvent.clientY - obj.offsetTop;

            document.onmousemove = function (ev) {
                var oEvent = ev || event;
                var l = oEvent.clientX - disX;
                var t = oEvent.clientY - disY;
                obj.style.left = l + 'px';
                obj.style.top = t + 'px';

                if (iLastX == 0) { iLastX = l; }
                if (iLastY == 0) { iLastY = t; }
                iSpeedX = l - iLastX;      // 以位移差推算速度
                iSpeedY = t - iLastY;
                iLastX = l;
                iLastY = t;
            };
        };

        obj.onmouseup = function () {
            document.onmousedown = null;
            document.onmousemove = null;
            document.onmouseup = null;
            start();                       // 放手後延續慣性
        };

        start();
    };

    // 停止並重置狀態
    _this.stopMove = function () {
        clearInterval(iTimer);
        obj.onmousedown = null;
        document.onmousemove = null;
        obj.onmouseup = null;
        iLastX = 0; iLastY = 0;
        iSpeedX = 0; iSpeedY = 0;
        disX = 0; disY = 0;
    };

    // 建構區:速度未傳入時歸零
    var gcdMove = function () {
        if (!iSpeedX) { iSpeedX = 0; }
        if (!iSpeedY) { iSpeedY = 0; }
    };
    gcdMove();
}

可以怎麼改

想更「黏」一點,就把碰撞係數 0.8 調小;想更「彈」就調大。重力值 3 也可以依畫面比例調整。若元素需要限制在容器內而非整個視窗,只要把 document.documentElement.clientWidth / clientHeight 換成容器元素的 clientWidth / clientHeight,再把定位基準設為該容器即可。

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