本文通过Javascript实现伸缩和展开特效功能,给人感觉动画的效果,div标签与Js完美结合,特效相当的给力,并且不受任何内容的限制,收缩和展示的速度是可以通过修改时间来控制循环时间以及可以控制高度大小。
具体实现,Javascript代码如下:
<script type="text/javascript"> //高度 var mh = 30; //每次变化的量 var step = 1; //循环时间 var ms = 15; 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>
HTML调用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>收缩展示www.yoodb.com</title> <style type="text/css"> div.yoodb{ border:solid 1px;overflow:hidden; } div.yoodb 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="yoodb"><h5 onclick="toggle(this.parentNode)">点击此处层伸缩</h5> <p>欢迎大家收藏www.yoodb.com技术网站,每天定时更新内容。</p> </div> </body> </html>