這篇文章主要介紹了Javascript實現div的toggle效果的方法,實例分析了采用純javascript實現toggle效果的相關技巧,需要的朋友可以參考下
本文實例講述了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 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為時間間隔。
有個有趣的東東:
- function a()
- {
- setTimeout(function(){alert('1');},0);
- alert('2');
- }
猜輸出結果是什么?
答案: 21 ,而不是12哦。這是因為,JS函數執行也像其它編程語言一樣有堆棧的。alert('1')因為有setTimeout,所以最后執行。。。不知道我這樣理解對不對。
完成收功!
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答
圖片精選