帮忙修改个小代码
下面的代码是一个DIV层伸缩的效果, 现在问题是只伸的时候有动画,请大大们帮修改下缩的时候也有动画,谢谢哦。。 还有就是那个时间控制, 最小只能1.1吗 能再慢一点么?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>kissy</title>
<style>
*{ margin: 0; padding: 0;}
html{overflow:hidden;border:0px;}
body {margin: 0px;padding:0px;border:0px;overflow:hidden;font:12px Arial, Helvetica, sans-serif;background-color:Transparent;}
.main{width: 100%;overflow: hidden;position: relative;}
.title h3{background: #f9f9f9;border-bottom: 1px solid #DDD;line-height: 25px;height: 25px;overflow: hidden;zoom: 1;padding-left: 10px;padding-top: 6px;}
.title span{float:right;margin-top:-25px;}
#layer-body{height:0;}
</style>
<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){
if(obj == undefined){return false;}
var h = h || 262;
var s = s || 1.1;
var obj = typeof(obj)=="string"?document.getElementById(obj):obj;
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(){
document.getElementById('IDfm').onclick = function(){
Scroll('layer-body');
}
}
</script>
</head>
<body>
<div class="main">
<div class="title"><h3>标题</h3> <span id="IDfm">切换</span></div>
<div id="layer-body">一些内容</div>
<div style="border:1px #E3E3E3 solid;height:262px">下层</div>
</div>
</body>
</html>
[解决办法]
给你个新的。你的代码有点多
<!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">
<head>
<title>runcode</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="Author" content="Sheneyan" />
<script type="text/javascript">
var mh = 30;//最小高度
var step = 5;//每次变化的px量
var ms = 10;//每隔多久循环一次
function toggle(o){
if (!o.tid)o.tid = "_" + Math.random() * 100;
if (!window.toggler)window.toggler = {};
if (!window.toggler[o.tid]){
window.toggler[o.tid]={
obj:o,
//maxHeight:o.offsetHeight,
maxHeight:o.scrollHeight+mh, //这里改动处
minHeight:mh,
timer:null,
// action:1
action:-1 //这里改动处
};
}
o.style.height = o.offsetHeight + "px";
if (window.toggler[o.tid].timer)clearTimeout(window.toggler[o.tid].timer);
window.toggler[o.tid].action *= -1;
window.toggler[o.tid].timer = setTimeout("anim('"+o.tid+"')",ms );
}
function anim(id){
var t = window.toggler[id];
var o = window.toggler[id].obj;
if (t.action < 0){
if (o.offsetHeight <= t.minHeight){
clearTimeout(t.timer);
return;
}
}
else{
if (o.offsetHeight >= t.maxHeight){
clearTimeout(t.timer);
return;
}
}
o.style.height = (parseInt(o.style.height, 10) + t.action * step) + "px";
window.toggler[id].timer = setTimeout("anim('"+id+"')",ms );
}
</script>
<style type="text/css">
div.xx{border:solid 1px;overflow:hidden; height:/*这里改动处*/30px;}
div.xx h5{border:solid 1px;border-width:0 0 1px;padding:0;margin:0;height:30px;line-height:30px;cursor:pointer;background:#eee;}
</style>
</head>
<body>
<div class="xx"><h5 onclick="toggle(this.parentNode)">伸缩效果</h5>
<p>内容</p>
<p>内容</p>
<p>内容</p>
<p>内容</p>
</div>
</body>
</html>