在下js菜鸟,从网上看到一段关于层展开的效果代码,希望用于自己的项目,可怎么弄都弄不好,希望大家帮帮忙。期望效果:通过点击checkBox,展开层。var speed=50;
var maxheight=450;//点击checkBox引发此函数
function divPopup(event,item){
        var ruleDiv = document.getElementById("smsDiv");
if(item.checked){
           DivShow(ruleDiv);
}else{
           DivHide(ruleDiv);
}
}//DIV展开与收起的两个函数
function DivShow(item) {
  item.style.pixelHeight+=speed;
  if(item.style.pixelHeight<maxheight){
setTimeout("DivShow("+item+");",1);
  }
}function DivHide(item) {
  item.style.pixelHeight-=speed;
  if(item.style.pixelHeight>0){
setTimeout("DivHide("+item+");",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=gb2312" />
    <title>flash效果展开层、关闭层,并且可以拖动层丨芯晴网页特效丨CsrCode.Cn</title>
    <style type="text/css">
    body,span,div,td{font-size:12px;line-height:1.5em;color:#849BCA;}
    #bodyL{
     float:left;
     width:84px;
     margin-right:2px;
    }
    a.od{
     width:80px;
     height:25px;
     line-height:25px;
     text-align:center;
     font-weight:bold;
     border: 2px solid #849BCA; 
     display:block;
     color:#547BC9;
     float:left;
     text-decoration:none;
     margin-top:2px;
    }
    a.od:link{
     background:#EEF1F8;
    }
    a.od:visited{
     background:#EEF1F8;
    }
    a.od:hover{
     background:#EEE;
    }
    a.od:active{
     background:#EEE;
    }
    #fd{ 
     width:500px;
     height:200px;
     background:#EDF1F8; 
     border: 2px solid #849BCA;
     margin-top:2px;
     margin-left:2px;
     float:left;
     overflow:hidden;
     position:absolute;
     left:0px;
     top:0px;
     cursor:move;
     float:left;
     /*filter:alpha(opacity=50);*/
     
    }
    .content{
     padding:10px;
    }
    </style>
    </head>
    <body>
    <div id="bodyL">
     <a href="#" class="od" onclick = "show('fd');return false;">打开层</a>
     <a href="#" class="od" onclick = "closeed('fd');return false;">关闭层</a>
    </div> 
    <div id="fd" style="display:none;filter:alpha(opacity=100);opacity:1;">
     <div class="content">移动层<br>这个层是可以拖动的,按着鼠标试试……<br><p align="left">本站『<a href="http://www.CsrCode.cn">芯晴网页特效</a>』收集了许多网络上比较好的网页特效,希望有你所需要的特效。<br>本特效由 <a href="http://www.CsrCode.cn">芯晴网页特效</a>丨CsrCode.Cn 收集于互联网,只为兴趣与学习交流,不作商业用途。</p>
     </div> 
    </div>
     
    <script type="text/javascript">
     var prox;
     var proy;
     var proxc;
     var proyc;
     function show(id){/*--打开--*/
      clearInterval(prox);
      clearInterval(proy);
      clearInterval(proxc);
      clearInterval(proyc);
      var o = document.getElementById(id);
      o.style.display = "block";
      o.style.width = "1px";
      o.style.height = "1px"; 
      prox = setInterval(function(){openx(o,500)},10);
     } 
     function openx(o,x){/*--打开x--*/
      var cx = parseInt(o.style.width);
      if(cx < x)
      {
       o.style.width = (cx + Math.ceil((x-cx)/5)) +"px";
      }
      else
      {
       clearInterval(prox);
       proy = setInterval(function(){openy(o,200)},10);
      }
     } 
     function openy(o,y){/*--打开y--*/ 
      var cy = parseInt(o.style.height);
      if(cy < y)
      {
       o.style.height = (cy + Math.ceil((y-cy)/5)) +"px";
      }
      else
      {
       clearInterval(proy);   
      }
     } 
     function closeed(id){/*--关闭--*/
      clearInterval(prox);
      clearInterval(proy);
      clearInterval(proxc);
      clearInterval(proyc);  
      var o = document.getElementById(id);
      if(o.style.display == "block")
      {
       proyc = setInterval(function(){closey(o)},10);   
      }  
     } 
     function closey(o){/*--打开y--*/ 
      var cy = parseInt(o.style.height);
      if(cy > 0)
      {
       o.style.height = (cy - Math.ceil(cy/5)) +"px";
      }
      else
      {
       clearInterval(proyc);    
       proxc = setInterval(function(){closex(o)},10);
      }
     } 
     function closex(o){/*--打开x--*/
      var cx = parseInt(o.style.width);
      if(cx > 0)
      {
       o.style.width = (cx - Math.ceil(cx/5)) +"px";
      }
      else
      {
       clearInterval(proxc);
       o.style.display = "none";
      }
     } 
     
     
     /*鼠标拖动*/ 
     var od = document.getElementById("fd"); 
     var dx,dy,mx,my,mouseD;
     var odrag;
     var isIE = document.all ? true : false;
     document.onmousedown = function(e){
      var e = e ? e : event;
      if(e.button == (document.all ? 1 : 0))
      {
       mouseD = true;   
      }
     }
     document.onmouseup = function(){
      mouseD = false;
      odrag = "";
      if(isIE)
      {
       od.releaseCapture();
       od.filters.alpha.opacity = 100;
      }
      else
      {
       window.releaseEvents(od.MOUSEMOVE);
       od.style.opacity = 1;
      }  
     }
     od.onmousedown = function(e){
      odrag = this;
      var e = e ? e : event;
      if(e.button == (document.all ? 1 : 0))
      {
       mx = e.clientX;
       my = e.clientY;
       od.style.left = od.offsetLeft + "px";
       od.style.top = od.offsetTop + "px";
       if(isIE)
       {
        od.setCapture();    
        od.filters.alpha.opacity = 50;
       }
       else
       {
        window.captureEvents(Event.MOUSEMOVE);
        od.style.opacity = 0.5;
       }
       
      } 
     }
     document.onmousemove = function(e){
      var e = e ? e : event;  if(mouseD==true  &&  odrag)
      {  
       var mrx = e.clientX - mx;
       var mry = e.clientY - my; 
       od.style.left = parseInt(od.style.left) +mrx + "px";
       od.style.top = parseInt(od.style.top) + mry + "px";   
       mx = e.clientX;
       my = e.clientY;
       
      }
     }
    </script>
    </body>
    </html>
    另存为网页,先看看效果。然后里面代码可以参考一下
      

  2.   

    给你改了,从哪找的,根本不是事,是你改错的?
    <script>
    var speed=50;
    var maxheight=450;//点击checkBox引发此函数
    function divPopup(item){
    if(item.checked){
    DivShow("smsDiv");
    }else{
    DivHide("smsDiv");
    }
    }//DIV展开与收起的两个函数
    function DivShow(ruleDiv) {
    var item = document.getElementById(ruleDiv);
    item.style.pixelHeight+=speed;
    if(item.style.pixelHeight<maxheight){
    setTimeout("DivShow('"+ruleDiv+"');",1);
    }
    }function DivHide(ruleDiv) {
    var item = document.getElementById(ruleDiv);
    item.style.pixelHeight-=speed;
    if(item.style.pixelHeight>0){
    setTimeout("DivHide('"+ruleDiv+"');",1);
    }
    }</script>
      

  3.   

    既然学习JS,那就得认识下Jquery(JS 类库)。<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><!--载入jquery-->
    <div><a href="#" onclick="javascript:$('#demo').hide();">关闭</a>&nbsp;<a href="#" onclick="javascript:$('#demo').show();">打开</a></div>
    <div style="background-color:#F00; width:800px; height:300px;" id="demo"></div>
    打开、关闭的效果很多,你可以查下手册
      

  4.   

    如果用jquery的话,还有比楼上更简单的方法:
    <input type="checkbox" name="aa" value="展开/关闭" onclick="#('#smsDiv').toggle();">
    只要这样就可以。
      

  5.   

    如果用jquery的话,还有比楼上更简单的方法:
    <input type="checkbox" name="aa" value="展开|关闭" onclick="$('#smsDiv').toggle();">
    只要这样就可以。
      

  6.   

    我想楼主挺看重原来代码的一个原因,可能是动画效果吧。
    这个用jquery也可以实现。用它与 animate联合起来就可以创建一些效果,如一个带渐显的滑动效果:
    $(document).ready(function() {
    $("a").toggle(function() {
    $(".stuff").animate({
    height: 'hide',
    opacity: 'hide'
    },'slow');
    },
    function() {
    $(".stuff").animate({
    height: 'show',
    opacity: 'show'
    },'slow');
    });
    });
    有时间可以看看。
      

  7.   

    感谢楼上的yeyuxuan2006提供给我JQUERY的思路!感谢theforever热心的帮助!最后通过jquery的slideDown/slideUp函数解决了,再次感谢~