现在的收缩的功能我已经实现了.
但展开的不会写..麻烦大家帮我完成..
展开的函数是a1()
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
    <style type="text/css">
        *{margin:0;padding:0;}
        ul{list-style:none outside none}
.title,.content{width:300px;margin:0 auto;}
.title span{cursor:pointer}
.title{height:25px;background:#aaa;}
.content{ background:#ccc;overflow:hidden;opacity:1;filter:alpha(opacity=100)}
    </style>
<script type="text/javascript">
var h;
var timer = null;
var timers = null;
var i=0;
var arr = ['0.9','0.8','0.7','0.6','0.5','0.4','0.3','0.2','0.1','0'];
var IEarr = [90,80,70,60,50,40,30,20,10,0];


function b(){
var c = document.getElementById("content");
if(c.offsetHeight != 0){
h = c.offsetHeight-30;
c.style.height = h+"px";
i++;
if(navigator.appName.indexOf('Microsoft') != -1){
c.style.filter = "alpha(opacity="+IEarr[i]+")";
if(i ==9){
c.style.display = 'none';
return;
}
}
else{
c.style.opacity = arr[i];
}
}
else{
c.style.display = 'none';
i=0;
clearTimeout(timer);

}
timer = setTimeout(function(){b()},50)
} function a1(){
var a = document.getElementById("content");
a.style.display = "block";
if(a.offsetHeight < 360){

var y = a.offsetHeight + 30;
a.style.height = y +"px";
i++;
a.style.opacity = arr[i].reverse();
}else{

clearTimeout(timers);
}
timers = setTimeout(function(){a1()},50)
}
function autoHeight(){
var c = document.getElementById("content");
var t = document.getElementById("title").getElementsByTagName("span")[0];
t.onclick = function(){
if(c.style.display == 'none'){
a1();
}
else{
b();

}
}
}
window.onload = autoHeight;
</script>
 </head>
 <body> <div class="title" id="title">
<span>收缩</span>
</div>
<div class="content" id="content">
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
</div> </body>
</html>

解决方案 »

  1.   

    看了下代码,给你提出问题出现的地方,具体还是你自己改吧:
    首先if(c.offsetHeight != 0){这句根本就没起到作用,因为根据你的计算这句永远没有为假的时候你可以用if(c.offsetHeight >= 200){代替测试下就知道了
    其次arr[i].reverse();是不成立的因为reverse是给数组等对象用的,而你的arr[i]是啥啊,估计应该是arr.reverse()吧
      

  2.   

    别折腾了,用jquery两行就搞定了
      

  3.   

    怎么可能没有为假啊.我在else里加了alert都能弹出来
      

  4.   

    你是那层的else出现alert了,我咋没出现呢?
      

  5.   

    h = c.offsetHeight-30;你看这个有等于0的时候吗?