JS收缩层的效果

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
  3. <head>
  4. <title>层展开/关闭 - 运动缓冲效果</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  6. <meta name="keywords" content="小秦,展开,关闭,运动缓冲,效果,javascript,封装" />
  7. <meta name="description" content="展开/关闭 - 运动缓冲效果" />
  8. <meta name="copyright" content="Copyright 2008 XQin.cn" />
  9. <meta name="author" content="小秦" />
  10. <script type="text/javascript">
  11. /*
  12.     函数名称: Scroll
  13.     Scroll(obj, h, s)
  14.     参数说明:
  15.         obj,[object]  id值或对象.     必需
  16.           h,[height]  展开后的高度.   可选(默认为200px)
  17.           s,[speed]   展开速度,值越小展开速度越慢. 可选(默认为1.2){建议取值为1.1到2.0之间[例如:1.17]}.
  18.     函数返回值:
  19.         true    展开(对象的高度等于展开后的高度)
  20.         false   关闭(对象的高度等于原始高度)
  21. */
  22. function Scroll(obj, h, s){
  23.     if(obj == undefined){return false;}
  24.     var h = h || 200;
  25.     var s = s || 1.2;
  26.     var obj = typeof(obj)=="string"?document.getElementById(obj):obj;
  27.     var status = obj.getAttribute("status")==null;
  28.     var oh = parseInt(obj.offsetHeight);
  29.     obj.style.height = oh;
  30.     obj.style.display = "block";
  31.     obj.style.overflow = "hidden";
  32.     if(obj.getAttribute("oldHeight") == null){
  33.         obj.setAttribute("oldHeight", oh);
  34.     }else{
  35.         var oldH = Math.ceil(obj.getAttribute("oldHeight"));
  36.     }
  37.     var reSet = function(){
  38.         if(status){
  39.             if(oh < h){
  40.                 oh = Math.ceil(h-(h-oh)/s);
  41.                 obj.style.height = oh+"px";
  42.             }else{
  43.                 obj.setAttribute("status",false);
  44.                 window.clearInterval(IntervalId);
  45.             }
  46.         }else{
  47.             obj.style.height = oldH+"px";
  48.             obj.removeAttribute("status");
  49.             window.clearInterval(IntervalId);
  50.         }
  51.     }
  52.     var IntervalId = window.setInterval(reSet,10);
  53.     return status;
  54. }
  55. window.onload= function(){
  56.     document.getElementById('detail').onclick = function(){
  57.         Scroll('detail', 300, 1.3);
  58.     }
  59.     document.getElementById('text').onclick = function(){
  60.         Scroll('text');
  61.     }
  62. }
  63. </script>
  64. </head>
  65. <body>
  66. <p id="detail" style="border:1px dashed #f00;">这是一个段落啦!!哇哈哈哈<br/>点我一下下啦:)</p>
  67. <div id="text" style="border:1px solid #0f0">Hello World!<br/>你敢点我不  -_|||</div>
  68. <button onclick="Scroll('text', 400, 1.2)">Hello World</button>
  69. </body>
  70. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值