Javascript 层展开/关闭 - 运动缓冲效果

王朝学院·作者佚名  2009-05-07
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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>

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航