DIV CSS 佈局教程網

JavaScript 實現 div 伸縮 toggle 效果實例分析
編輯:JavaScript綜合知識     

「點擊展開、再點擊收合」的伸縮效果,是網頁常見的互動模式。本文以原生 JavaScript 實作一個 div 的 toggle 伸縮動畫,並歸納幾個實作時容易踩到的眉角。

核心程式

<script type="text/javascript" language="javascript">
function $(obj){ return document.getElementById(obj); }

function ToggleDiv(){
    this.ToggleId  = 'silder';    // 被伸縮的物件 ID
    this.ParentId  = 'container'; // 被伸縮物件的父 ID
    this.minHeight = 1;           // 最小高度
    this.maxHeight = 200;         // 最大高度
    this.speed     = 1;           // 伸縮速度
    this.offset    = 0.15;        // 偏移量(加速)
    this.load = function(){
        if($(this.ToggleId).style.display == 'none'){   // 隱藏則展開
            StartToggle('open', this.ToggleId, this.ParentId,
                this.minHeight, this.maxHeight, this.speed, this.offset);
        } else {                                        // 顯示則收合
            StartToggle('close', this.ToggleId, this.ParentId,
                this.minHeight, this.maxHeight, this.speed, this.offset);
        }
    };
}

function StartToggle(method, toggleid, parentid, minheight, maxheight, speed, offset){
    if(typeof(method) != 'string' || method.toLowerCase() == ''){ method = 'open'; }
    if(method.toLowerCase() == 'open'){
        var addspeed = speed + offset;
        var openfun = function(){
            var originheight = $(toggleid).offsetHeight == 0 ? 1 : $(toggleid).offsetHeight;
            var newheight = originheight + addspeed;
            addspeed = addspeed + offset;
            if(parseInt(newheight) < parseInt(maxheight)){
                $(toggleid).style.height = newheight + 'px';
                $(toggleid).style.display = 'block';
            } else {
                $(toggleid).style.height = maxheight + 'px';
                $(toggleid).style.display = 'block';
                $(parentid).innerHTML = '收縮';
                window.clearInterval(addtimer);
            }
        };
        var addtimer = window.setInterval(openfun, 100);
    } else if(method.toLowerCase() == 'close'){
        var addspeed = speed + offset;
        var reducefunction = function(){
            var originheight = $(toggleid).offsetHeight;
            var newheight = originheight - addspeed;
            addspeed = addspeed + offset;
            if(parseInt(newheight) > parseInt(minheight)){
                $(toggleid).style.height = newheight + 'px';
                $(toggleid).style.display = 'block';
            } else {
                $(toggleid).style.display = 'none';
                $(toggleid).style.height = '1px';
                $(parentid).innerHTML = '展開';
                window.clearInterval(reducetimer);
            }
        };
        var reducetimer = window.setInterval(reducefunction, 100);
    }
}

function DoToggle(obj1, obj2){
    var tog = new ToggleDiv();
    tog.ToggleId  = obj1;
    tog.ParentId  = obj2;
    tog.minHeight = 5;
    tog.maxHeight = 110;
    tog.speed     = 10;
    tog.offset    = 3;
    tog.load();
}
</script>

使用範例

<div style="border:1px dashed blue; width:200px;">
  <h2 id="container" onclick="javascript:DoToggle('silder', this.id);"
     onmouseover="this.style.cursor='pointer'">展開</h2>
  <div id="silder" style="display:none">
    伸縮效果<br/>伸縮效果<br/>伸縮效果<br/>
  </div>
</div>

經驗小結

1. display:none 與 visibility:hidden 下讀取 offsetHeight 的結果不同:前者讀到 0,後者讀到元素實際高度(如 108)。

2. style.height 的值帶有單位(如 "108px"),並非純數字;而 offsetHeight 回傳的是純數字 108,兩者做運算時要留意型別。

3. setTimeout 與 setInterval 有兩種寫法:一種是第一參數為函式、第二為間隔、其後為參數;另一種是第一參數為物件、第二為方法名、第三為間隔。此外要注意,setTimeout(fn, 0) 不會立即執行,而是排到目前堆疊之後——例如先 alert('2') 再執行 setTimeout 內的 alert('1'),輸出順序會是 2、1。

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