Javascript实现div的toggle效果小记及其它。

本文分享了一个手写的Toggle伸缩效果实现过程,通过JavaScript控制元素的高度变化来达到展开和收缩的效果,并探讨了一些关于offsetHeight及style属性使用的注意事项。

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

闲着无聊。想起网上经常见过的Toggle效果,虽然网上有不少的代码和类库可以实现。但是还是想自己亲手写写。。。
 ---今天,终于操起刀来大杀四方-----大言不惭了^_^。
翠花,上酸菜!
 1None.gif<script type="text/javascript" language="javascript">
 2None.gif     function $(obj)
 3ExpandedBlockStart.gifContractedBlock.gif     dot.gif{
 4InBlock.gif       return document.getElementById(obj);
 5ExpandedBlockEnd.gif     }

 6None.gif     function ToggleDiv()
 7ExpandedBlockStart.gifContractedBlock.gif     dot.gif{
 8InBlock.gif        this.ToggleId='silder'//被伸缩的对象ID
 9InBlock.gif        this.ParentId='container'//被伸缩的对象的父ID
10InBlock.gif        this.minHeight=1//最小高度
11InBlock.gif        this.maxHeight=200//最大高度
12InBlock.gif        this.speed=1//伸缩速度
13InBlock.gif        this.offset=0.15//偏移量
14InBlock.gif        this.load=function()
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif           if($(this.ToggleId).style.display=='none'//如果是隐藏的就打开
17ExpandedSubBlockStart.gifContractedSubBlock.gif           dot.gif{
18InBlock.gif               StartToggle('open',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
19ExpandedSubBlockEnd.gif           }

20InBlock.gif           else //如果是打开的就隐藏
21ExpandedSubBlockStart.gifContractedSubBlock.gif           dot.gif{
22InBlock.gif                  StartToggle('close',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
23ExpandedSubBlockEnd.gif           }

24ExpandedSubBlockEnd.gif        }

25ExpandedBlockEnd.gif     }

26None.gif     function StartToggle(method,toggleid,parentid,minheight,maxheight,speed,offset)
27ExpandedBlockStart.gifContractedBlock.gif     dot.gif{
28InBlock.gif        if(typeof(method)!='string' || method.toLowerCase()=='')
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif          method='open';
31ExpandedSubBlockEnd.gif        }

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

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

52ExpandedSubBlockEnd.gif           }

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

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

68InBlock.gif               else
69ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
70InBlock.gif                  $(toggleid).style.display='none';
71InBlock.gif                  $(toggleid).style.height='1px';
72InBlock.gif                   $(parentid).innerHTML='展开';
73InBlock.gif                  window.clearInterval(reducetimer);
74ExpandedSubBlockEnd.gif               }

75ExpandedSubBlockEnd.gif            }

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

78ExpandedBlockEnd.gif     }

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

90None.gif  </script>
接下来上汤:
 1None.gif <div  style="border:1px dashed blue;width:200px;">
 2None.gif    <h2  id="container" onclick="javascript:DoToggle('silder',this.id);" onmouseover="this.style.cursor='pointer';">展开</h2>
 3None.gif    <div id="silder" style="display:none">
 4None.gif      伸缩效果<br />
 5None.gif      伸缩效果<br />
 6None.gif      伸缩效果<br />
 7None.gif      伸缩效果<br />伸缩效果<br />
 8None.gif      伸缩效果<br />
 9None.gif    </div>
10None.gif  </div>
11None.gif  
-------
当然,由于本厨的手艺有限,导致味道啥的都差了些。。各位见谅了。。
代码中有些东东是多余的或者是重复的。本想精简单一下,但是一想,思路有了就行了。
-------
以下是本次练习中的一些经验:
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,所以最后执行。。。不知道我这样理解对不对。

----打完收功。

转载于:https://www.cnblogs.com/McJeremy/archive/2008/06/04/1213498.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值