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)};
$('btn').onclick = function(){
Scroll('test',this.scrollHeight,1.2);
}
$('menu').onclick = function(){
Scroll('menu',this.scrollHeight,1.2);
}
}
Html代碼:
程式碼
<dl>
<dt id="btn" style="cursor:pointer; background-color:#009999; width:200px;">展開收縮</dt>
<dd id="test" style="background-color:#33CCCC; overflow:hidden; width:200px; height:0;">
內容
</dd>
</dl>
程式碼
<div id="menu" style="border:1px solid #ccc;width:160px;height:16px;text-align:center;cursor:pointer;overflow:hidden;">
內容內容內容內容內容
</div>