Javascript實現div的toggle效果

原文:http://www.cnblogs.com/McJeremy/archive/2008/06/04/1213498.html

JavaScript:

1<script type="text/javascript" language="javascript">
 2     function $(obj)
 3     {
 4       return document.getElementById(obj);
 5     }

 6     function ToggleDiv()
 7     {
 8        this.ToggleId='silder'//被伸縮的對象ID
 9        this.ParentId='container'//被伸縮的對象的父ID
10        this.minHeight=1//最小高度
11        this.maxHeight=200//最大高度
12        this.speed=1//伸縮速度
13        this.offset=0.15//偏移量
14        this.load=function()
15        {
16           if($(this.ToggleId).style.display=='none'//如果是隱藏的就打開
17           {
18               StartToggle('open',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
19           }

20           else //如果是打開的就隱藏
21           {
22                  StartToggle('close',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
23           }

24        }

25     }

26     function StartToggle(method,toggleid,parentid,minheight,maxheight,speed,offset)
27     {
28        if(typeof(method)!='string' || method.toLowerCase()=='')
29        {
30          method='open';
31        }

32        if(method.toLowerCase()=='open')
33        {
34           var addspeed=speed+offset;
35           var openfun=function()
36           {
37               var originheight=$(toggleid).offsetHeight==0?1:$(toggleid).offsetHeight;
38               var newheight=originheight+addspeed;
39               addspeed=addspeed+offset;
40               if(parseInt(newheight)<parseInt(maxheight))
41               {               
42                  $(toggleid).style.height=newheight+'px';                  
43                  $(toggleid).style.display='block';
44               }

45               else if(parseInt(newheight)>=parseInt(maxheight))
46               {
47                  $(toggleid).style.height=maxheight+'px';                  
48                  $(toggleid).style.display='block';
49                  $(parentid).innerHTML='收縮';
50                  window.clearInterval(addtimer);
51               }

52           }

53           var addtimer=window.setInterval(openfun,100);
54        }

55        else if(method.toLowerCase()=='close')
56        {
57            var addspeed=speed+offset;
58            var reducefunction=function()
59            {
60               var originheight=$(toggleid).offsetHeight;
61               var newheight=originheight-addspeed;
62               addspeed=addspeed+offset;
63               if(parseInt(newheight)>parseInt(minheight))
64               {
65                  $(toggleid).style.height=newheight+'px';
66                  $(toggleid).style.display='block';
67               }

68               else
69               {
70                  $(toggleid).style.display='none';
71                  $(toggleid).style.height='1px';
72                   $(parentid).innerHTML='展開';
73                  window.clearInterval(reducetimer);
74               }

75            }

76           var reducetimer=window.setInterval(reducefunction,100);
77        }

78     }

79     function DoToggle(obj1,obj2)
80     {
81        var tog=new ToggleDiv();
82        tog.ToggleId=obj1;
83        tog.ParentId=obj2;
84        tog.minHeight=5;
85        tog.maxHeight=110;
86        tog.speed=10;
87        tog.offset=3;
88        tog.load();
89     }

90  </script>


 1 <div  style="border:1px dashed blue;width:200px;">
 2    <h2  id="container" onclick="javascript:DoToggle('silder',this.id);" onmouseover="this.style.cursor='pointer';">展開</h2>
 3    <div id="silder" style="display:none">
 4      伸縮效果<br />
 5      伸縮效果<br />
 6      伸縮效果<br />
 7      伸縮效果<br />伸縮效果<br />
 8      伸縮效果<br />
 9    </div>
10  </div>

經驗總結:

1、在style.display='none'與style.visibility='hidden'時讀取對象的offsetHeight值將會有所不同。
   style.display='none'讀出來的,將是 0 ,而style.visibility='hidden'時讀取的是對象加載時的offsetHeight,比如 108等。
2、style.height的值並不是整型或number型的,別忘了它是有單位的哦:如 "108px"而不是"108",而offsetHeight的值是 108.
3、setTimeout和setInterval
   它們都有兩種使用方法,以setTimeout爲例:
 方法一:setTimeout(function,interval,args)  參數一爲函數名或匿名函數,參數2爲時間間隔,參數3到N是所調用函數的參數,如下例:
    setTimeout(function(){alert('1');},1000)  setTimeout(GetStr,1000,'McJeremy')
 方法二:setTimeout(object,function,interval) 參數一爲調用的對象,參數2爲對象中的方法,參數3爲時間間隔。
 有個有趣的東東:
 function a()
{
   setTimeout(function(){alert('1');},0);
   alert('2');
}
----猜輸出結果是什麼?---答案: 21 ,而不是12哦。這是因爲,JS函數執行也像其它編程語言一樣有堆棧的。alert('1')因爲有setTimeout,所以最後執行。。。不知道我這樣理解對不對。

----打完收功。

<h3>
   心靜似高山流水不動,心清若巫峯霧氣不沾。
</h3>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章