展开层与关闭层的动态缓冲效果特效

本文介绍了一个JavaScript函数Scroll,用于实现元素的展开与关闭效果,并带有平滑的运动缓冲。该函数可以应用于不同高度的元素,通过调整速度参数实现不同的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="zh-CN"lang="zh-CN">
<head>
<title>层展开/关闭-运动缓冲效果</title>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<metaname="keywords"content="小秦,展开,关闭,运动缓冲,效果,javascript,封装"/>
<metaname="description"content="展开/关闭-运动缓冲效果"/>
<metaname="copyright"content="Copyright2008XQin.cn"/>
<metaname="author"content="小秦"/>
<scripttype="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关闭(对象的高度等于原始高度)
*/
functionScroll(obj,h,s){
varh=h||200;
vars=s||1.2;
varobj=typeof(obj)=="string"?document.getElementById(obj):obj;
if(obj==undefined){returnfalse;}
varstatus=obj.getAttribute("status")==null;
varoh=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{
varoldH=Math.ceil(obj.getAttribute("oldHeight"));
}
varreSet=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);
returnstatus;
}
window.onload=function(){
var$=function(id){returndocument.getElementById(id)};
$('menu').onclick=function(){
Scroll('menu',this.scrollHeight,1.2);
}
$('test').onclick=function(){
Scroll('test',300,1.2);
}
}
</script>
</head>
<body>
<divid="test"style="border:1pxsolid#f00;width:200px">单击后展开指定高度300px</div>
<divid="menu"style="position:absolute;top:10px;left:400px;border:1pxsolid#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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值