Javascript实现div的toggle效果实例分析

<script type="text/javascript" language="javascript"> function $(obj) { return document.getElementById(obj); } function ToggleDiv() { this.ToggleId='silder'; //被伸缩的对象ID this.ParentId='container'; //被伸缩的对象的父ID this.minHeight=1; //最小高度 this.maxHeight=200; //最大高度 this.speed=1; //伸缩速度 this.offset=0.15; //偏移量 this.load=function() { if($(this.ToggleId).style.display=='none') //如果是隐藏的就打开 { StartToggle('open',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset); } else //如果是打开的就隐藏 { StartToggle('close',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset); } } } function StartToggle(method,toggleid,parentid,minheight,maxheight,speed,offset) { if(typeof(method)!='string' || method.toLowerCase()=='') { method='open'; } if(method.toLowerCase()=='open') { var addspeed=speed+offset; var openfun=function() { var originheight=$(toggleid).offsetHeight==0?1:$(toggleid).offsetHeight; var newheight=originheight+addspeed; addspeed=addspeed+offset; if(parseInt(newheight)<parseInt(maxheight)) { $(toggleid).style.height=newheight+'px'; $(toggleid).style.display='block'; } else if(parseInt(newheight)>=parseInt(maxheight)) { $(toggleid).style.height=maxheight+'px'; $(toggleid).style.display='block'; $(parentid).innerHTML='收缩'; window.clearInterval(addtimer); } } var addtimer=window.setInterval(openfun,100); } else if(method.toLowerCase()=='close') { var addspeed=speed+offset; var reducefunction=function() { var originheight=$(toggleid).offsetHeight; var newheight=originheight-addspeed; addspeed=addspeed+offset; if(parseInt(newheight)>parseInt(minheight)) { $(toggleid).style.height=newheight+'px'; $(toggleid).style.display='block'; } else { $(toggleid).style.display='none'; $(toggleid).style.height='1px'; $(parentid).innerHTML='展开'; window.clearInterval(reducetimer); } } var reducetimer=window.setInterval(reducefunction,100); } } function DoToggle(obj1,obj2) { var tog=new ToggleDiv(); tog.ToggleId=obj1; tog.ParentId=obj2; tog.minHeight=5; tog.maxHeight=110; tog.speed=10; tog.offset=3; tog.load(); } </script>

用法示例如下:

<div> <h2>展开</h2> <div> 伸缩效果<br /> 伸缩效果<br /> 伸缩效果<br /> 伸缩效果<br />伸缩效果<br /> 伸缩效果<br /> </div> </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,所以最后执行。。。不知道我这样理解对不对。

完成收功!

希望本文所述对大家的javascript程序设计有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgdssg.html