這篇文章主要介紹了js實現div層緩慢收縮與展開的方法,通過javascript自定義函數實現div層的收縮與展開效果,非常具有實用價值,需要的朋友可以參考下
本文實例講述了js實現div層緩慢收縮與展開的方法。分享給大家供大家參考。具體分析如下:
這是一個層伸縮與展開的特效代碼,帶有點動畫效果,緩慢展開,DIV+JS聯合打造,看上去還不錯。不受內容約束,收縮與展開的速度是可以控制的,高度、循環時間也可以改。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>層收縮</title>
- <script type="text/javascript">
- var mh = 30;//高度
- var step = 1;//每次變化的量
- var ms = 10;//循環時間
- function toggle(o){
- if (!o.tid)o.tid = "_" + Math.random() * 100;
- if (!window.toggler)window.toggler = {};
- if (!window.toggler[o.tid]){
- window.toggler[o.tid]={
- obj:o,
- maxHeight:o.offsetHeight,
- minHeight:mh,
- timer:null,
- action:1
- };
- }
- o.style.height = o.offsetHeight + "px";
- if (window.toggler[o.tid].timer)clearTimeout(window.toggler[o.tid].timer);
- window.toggler[o.tid].action *= -1;
- window.toggler[o.tid].timer = setTimeout("anim('"+o.tid+"')",ms );
- }
- function anim(id){
- var t = window.toggler[id];
- var o = window.toggler[id].obj;
- if (t.action < 0){
- if (o.offsetHeight <= t.minHeight){
- clearTimeout(t.timer);
- return;
- }
- }
- else{
- if (o.offsetHeight >= t.maxHeight){
- clearTimeout(t.timer);
- return;
- }
- }
- o.style.height = (parseInt(o.style.height, 10) + t.action * step) + "px";
- window.toggler[id].timer = setTimeout("anim('"+id+"')",ms );
- }
- </script>
- <style type="text/css">
- div.xx{
- border:solid 1px;overflow:hidden;
- }
- div.xx h5{
- border:solid 1px;border-width:0 0 1px;
- padding:0;margin:0;height:28px;
- line-height:30px;cursor:pointer;
- background:#eee;
- }
- </style>
- </head>
- <body>
- <div class="xx"><h5 onclick="toggle(this.parentNode)">點擊此處層伸縮</h5>
- <p>Tomat6.0的配置視頻教程,AVI電影格式,相當清淅了,比起圖文版的教程更貼切,
- 不會配置Tomcat的網友,你可不要錯過哦,由于視頻抓圖挺麻煩的,
- 試了多次還是抓了一個不太清淅的,大家將就吧。</p>
- </div>
- </body>
- </html>
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答
圖片精選