本文實例講述了JS實現slide文字框縮放伸展效果代碼。分享給大家供大家參考,具體如下:
這裡介紹的slide文字框縮放伸展效果,自己閒來無事寫的,不是太好,可能在兼容性方面還需努力,展開速度方面,IE8和火狐的展開速度不一樣,火狐下更快一些,至今沒找到原因,也請JS高人指點江山。
運行效果截圖如下:
在線演示地址如下:
http://demo.jb51.net/js/2015/js-slide-txt-dlg-show-codes/
具體代碼如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="content" content="text/html charset=gb2312"> <title>slide文字框縮放伸展效果</title> <style type="text/css"> *{margin:0; padding:0;} ul{list-style:none;} .box{width:300px; margin:10px; border:1px solid #ccc; overflow:hidden; position:relative; font-size:12px;} .oHx{height:30px; background:#eee; line-height:30px; font-size:14px; text-indent:14px; cursor:pointer;} .fold{position:absolute; top:9px; right:12px;} .box_content{line-height:18px; overflow:hidden; display:none;} </style> <script type="text/javascript"> function getElementsByClassName(className,id,tag){ tag = tag || "*"; if(id){ var id = typeof id == "string" ? $(id) : id; }else{ var id = document.body; } var els = id.getElementsByTagName(tag),arr = []; for(var i=0,n=els.length;i<n;i++){ for(var j=0,k=els[i].className.split(" "),l=k.length;j<l;j++){ if(k[j]==className){ arr.push(els[i]); break; } } } return arr; }; function Slide(slideClass,slideBtn,slideCon,slideSpeed) { this.oSlides = getElementsByClassName(slideClass); this.oTimer = null; this.slideBtn = slideBtn; this.slideCon = slideCon; this.slideSpeed = slideSpeed; } Slide.prototype = { oTimer:null, _init:function (){ this._slideEvent(); }, _slideEvent:function (){ var This = this; for(var i = 0,n=This.oSlides.length;i<n;i++){ (function(n){ var oSlide = This.oSlides[n]; var oSlideBtn = getElementsByClassName(This.slideBtn,oSlide)[0]; var oSlideCon = getElementsByClassName(This.slideCon,oSlide)[0]; oSlideBtn.onclick = function (){ if(oSlideCon.style.display == "block" && This.oTimer == null){ This._slideClose(oSlideCon); }else if(!(oSlideCon.style.display == "block" ) && This.oTimer == null){ This._slideOpen(oSlideCon); } } })(i) } }, _slideOpen:function (slideCon){ var This = this; slideCon.style.display = "block"; slideCon.style.height = "auto"; var slideHeight = slideCon.offsetHeight; slideCon.style.height = 0 + "px"; This.oTimer = setInterval(function (){ if(slideCon.offsetHeight < slideHeight){ slideCon.style.height = slideCon.offsetHeight + 2 + "px"; }else{ clearInterval(This.oTimer); This.oTimer = null; } },This.slideSpeed); }, _slideClose:function (slideCon){ var This = this; This.oTimer = setInterval(function (){ if(slideCon.offsetHeight <= 0){ clearInterval(This.oTimer); slideCon.style.display = "none"; This.oTimer = null; }else{ slideCon.style.height =slideCon.offsetHeight - 2 + "px"; } },This.slideSpeed); } } </script> </head> <body> <div class="box"> <div class="oHx slide">收縮2</div> <div class="box_content"> <ul class="uft" style="padding:10px;"> <li><a href="#" target="_blank">scscms V1.0 陽光企業網站系統</a></li> <li><a href="#" target="_blank">24點,VC++游戲源碼</a></li> <li><a href="#" target="_blank">可記錄圖像的C#數據庫記錄單程序</a></li> <li><a href="#" target="_blank">jQuery 1.4 參考指南的實例源代碼</a></li> </ul> </div> </div> <div class="box"> <div class="oHx slide">收縮3</div> <div class="box_content"> <ul class="uft"style="padding:10px;"> <li><a href="#" target="_blank">scscms V1.0 陽光企業網站系統</a></li> <li><a href="#" target="_blank">24點,VC++游戲源碼</a></li> <li><a href="#" target="_blank">可記錄圖像的C#數據庫記錄單程序</a></li> <li><a href="#" target="_blank">jQuery 1.4 參考指南的實例源代碼</a></li> </ul> </div> </div> <script type="text/javascript"> var mySlide = new Slide("box","slide","box_content",10); mySlide._slideEvent(); </script> </body> </html>
希望本文所述對大家JavaScript程序設計有所幫助。