簡單實用的js緩衝效果

不好意思,這篇文章幾天前被和諧了,具體原因未知,現在放出保存在電腦上的代碼。

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<title>層展開/關閉 - 運動緩衝效果</title>
<script type="text/javascript">
/*
    函數名稱: Scroll
    Scroll(obj, h, s)
    參數說明:
        obj,[object]  id值或對象.     必需
          h,[height]  展開後的高度.   可選(默認爲200px)
          s,[speed]   展開速度,值越小展開速度越慢. 可選(默認爲1.2){建議取值爲1.1到2.0之間[例如:1.17]}.
    函數返回值:
        true    展開(對象的高度等於展開後的高度)
        false   關閉(對象的高度等於原始高度)
*/
function Scroll(obj, h, s){
    var h = h || 200;
    var s = s || 1.2;
    var obj = typeof(obj)=="string"?document.getElementById(obj):obj;
    if(obj == undefined){return false;}
    var status = obj.getAttribute("status")==null;
    var oh = parseInt(obj.offsetHeight);
    obj.style.height = oh;
    obj.style.display = "block";
 obj.style.overflow = "hidden";
    if(obj.getAttribute("oldHeight") == null){
        obj.setAttribute("oldHeight", oh);
    }else{
        var oldH = Math.ceil(obj.getAttribute("oldHeight"));
    }
    var reSet = function(){
        if(status){
            if(oh < h){
                oh = Math.ceil(h-(h-oh)/s);
                obj.style.height = oh+"px";
            }else{
                obj.setAttribute("status",false);
                window.clearInterval(IntervalId);
            }
        }else{
            obj.style.height = oldH+"px";
            obj.removeAttribute("status");
            window.clearInterval(IntervalId);
        }
    }
    var IntervalId = window.setInterval(reSet,10);
 return status;
}
window.onload= function(){
    var $ = function(id){return document.getElementById(id)};
    $('menu').onclick = function(){
        Scroll('menu',this.scrollHeight,1.1);
    }
    $('test').onclick = function(){
        Scroll('test',300,1.2);
    }
}
</script>
</head>
<body>
<div id="test" style="border:1px solid #f00;width:200px">單擊後 展開指定高度 300px</div>
<div id="menu" style="position:absolute;top:10px;left: 400px;border:1px solid #ccc;width:160px;height:16px;text-align:center;cursor:pointer;overflow:hidden;">
單擊後 根據展開的高度根據內容而變<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
11<br />
12<br />
13<br />
14<br />
15<br />
16<br />
17<br />
18<br />
19<br />
20<br />
21<br />
22<br />
23<br />
24<br />
25<br />
26<br />
27<br />
28<br />
29<br />
</div>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章