DIV CSS 佈局教程網

JS 實現方塊互換位置動畫:讀取尺寸與座標再交換
編輯:關於JavaScript     

點一下兩個方塊,它們就交換位置與尺寸——這是做圖片切換、卡片排序時很實用的一種互動。核心觀念只有一個:先把「對方現在的樣子」讀出來,當成「自己的目標」,反之亦然。

關鍵程式碼

var now = s_pic_li[0];

for (var i = 0; i < s_pic_li.length; i++) {
    s_pic_li[i].onclick = function () {
        if (this == now) return false;

        var w  = now.offsetWidth;
        var h  = now.offsetHeight;
        var l  = now.offsetLeft;
        var t  = now.offsetTop;

        var w1 = this.offsetWidth;
        var h1 = this.offsetHeight;
        var l1 = this.offsetLeft;
        var t1 = this.offsetTop;

        startrun(now,  { width: w1, height: h1, left: l1, top: t1 });
        startrun(this, { width: w,  height: h,  left: l,  top: t  });

        now = this;
    };
}

迴圈替每一塊掛上 click 事件,點擊時讀取雙方資訊,交換後傳給運動函式。now 記住「目前選中的那一塊」,下次點擊時才知道要跟誰換。

完整程式碼

<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>交換運動</title>
<style>
body, ul, li { margin:0; padding:0; font:18px/1.5 arial; color:#333; }
#big_pic { width:500px; height:400px; background:#ccc; text-align:center; position:absolute; }
#s_pic li {
    float:left; width:100px; height:80px; display:inline;
    background:#06c; text-align:center; position:absolute; top:310px;
}
</style>
<script>
window.onload = function () {
    var s_pic    = document.getElementById("s_pic");
    var s_pic_li = s_pic.getElementsByTagName("li");
    var now = s_pic_li[0];

    for (var i = 0; i < s_pic_li.length; i++) {
        s_pic_li[i].onclick = function () {
            if (this == now) return false;

            var w = now.offsetWidth, h = now.offsetHeight;
            var l = now.offsetLeft,  t = now.offsetTop;
            var w1 = this.offsetWidth, h1 = this.offsetHeight;
            var l1 = this.offsetLeft,  t1 = this.offsetTop;

            startrun(now,  { width:w1, height:h1, left:l1, top:t1 });
            startrun(this, { width:w,  height:h,  left:l,  top:t  });
            now = this;
        };
    }
};

function getstyle(obj, name) {
    if (obj.currentStyle) {
        return obj.currentStyle[name];
    } else {
        return getComputedStyle(obj, false)[name];
    }
}

function startrun(obj, json, fn) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
        var isall = true;
        for (var attr in json) {
            var cur = 0;
            if (attr == "opacity") {
                cur = Math.round(parseFloat(getstyle(obj, attr)) * 100);
            } else {
                cur = parseInt(getstyle(obj, attr));
            }

            var speed = (json[attr] - cur) / 8;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

            if (cur != json[attr]) { isall = false; }

            if (attr == "opacity") {
                obj.style.filter = "alpha(opacity=" + (cur + speed) + ")";
                obj.style.opacity = (cur + speed) / 100;
            } else {
                obj.style[attr] = cur + speed + "px";
            }
        }

        if (isall) {
            clearInterval(obj.timer);
            if (fn) { fn(); }
        }
    }, 30);
}
</script>
</head>
<body>
<ul id="s_pic">
  <li style="left:0; top:0; width:400px; height:300px">0</li>
  <li style="left:0;">1</li>
  <li style="left:110px;">2</li>
  <li style="left:220px;">3</li>
  <li style="left:330px;">4</li>
</ul>
</body>
</html>

這裡的 startrun 已經是「緩動函式」的雛形:速度等於「差值除以 8」,差得遠就跑得快,越接近目標越慢,視覺上就有自然的減速感。Math.ceil/Math.floor 則確保速度至少為 1,避免小數被截斷後永遠到不了終點。

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