Javascript:
program code
/*
Function name: Scroll
Scroll(obj, h, s)
Parameter description:
obj,[object] id value or object. Required
h,[height] Height after expansion. Optional (default is 200px)
s, [speed] expansion speed, the smaller the value, the slower the expansion speed. Optional (default is 1.2) {recommended value is between 1.1 and 2.0 [for example: 1.17]}.
Function return value:
true expand (the height of the object is equal to the expanded height)
false off (the height of the object is equal to the original height)
*/
functionScroll(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);
}
}
varIntervalId = 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 code:
program code
<dl>
<dt id="btn" style="cursor:pointer; background-color:#009999; width:200px;">Expand and collapse</dt>
<dd id="test" style="background-color:#33CCCC; overflow:hidden; width:200px; height:0;">
content
</dd>
</dl>
program code
<div id="menu" style="border:1px solid #ccc;width:160px;height:16px;text-align:center;cursor:pointer;overflow:hidden;">
content content content content content
</div>