Javascript实现div的toggle效果实例分析
<script type="text/javascript" language="javascript">
function $(obj)
{
本文实例讲述了Javascript实现div的toggle效果。分享给大家供大家参考。具体分析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | <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> |
用法示例如下:
1 2 3 4 5 6 7 8 9 10 | <div style= "border:1px dashed blue;width:200px;" > <h2 id= "container" onclick= "javascript:DoToggle('silder',this.id);" onmouseover= "this.style.cursor='pointer';" >展开</h2> <div id= "silder" style= "display:none" > 伸缩效果<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为时间间隔。
有个有趣的东东:
1 2 3 4 5 | function a() { setTimeout( function (){alert( '1' );},0); alert( '2' ); } |
猜输出结果是什么?
答案: 21 ,而不是12哦。这是因为,JS函数执行也像其它编程语言一样有堆栈的。alert('1')因为有setTimeout,所以最后执行。。。不知道我这样理解对不对。
完成收功!
希望本文所述对大家的javascript程序设计有所帮助。