<!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" xml:lang="zh-CN" lang="zh-CN">
<head>
<title>层展开/关闭 - 运动缓冲效果</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="keywords" content="小秦,展开,关闭,运动缓冲,效果,javascript,封装" />
<meta name="description" content="展开/关闭 - 运动缓冲效果" />
<meta name="copyright" content="Copyright 2008 XQin.cn" />
<meta name="author" content="小秦" />
<script type="text/javascript">
/*
    函数名称: Scroll
    Scroll(obj, h, s)
    参数说明:
        obj,[object]  id值或对象.     必需
          h,[height]  展开后的高度.   可选(默认为200px)
          s,[speed]   展开速度,值越小展开速度越慢. 可选(默认为1.2){建议取值为1.1到2.0之间[例如:1.17]}.
    函数返回值:
        true    展开(对象的高度等于展开后的高度)
        false   关闭(对象的高度等于原始高度)
*/
function Scroll(obj, h, s){
    if(obj == undefined){return false;}
    var h = h || 200;
    var s = s || 1.2;
    var obj = typeof(obj)=="string"?document.getElementById(obj):obj;
    var status = obj.getAttribute("status")==null;
    var oh = parseInt(obj.offsetHeight);
    obj.style.height = oh;
    alert(obj.oldHeight);
    
    obj.style.display = "normal";
    obj.style.overflow = "hidden";
    if(obj.getAttribute("oldHeight") == null)
    {
        obj.setAttribute("oldHeight", oh);
    }else
    {
        var oldH = Math.ceil(obj.getAttribute("oldHeight"));
    }
    var reSet = function(){
if(status)
{
    if(oh < h)
    {                oh = Math.ceil(h-(h-oh)/s);
                obj.style.height = oh+"px";
    }
    else
    {
                obj.setAttribute("status",false);
                window.clearInterval(IntervalId);
            }
}
else
{
            obj.style.height = oldH+"px";
            obj.removeAttribute("status");
            window.clearInterval(IntervalId);
        }
    }
    var IntervalId = window.setInterval(reSet,10);  
return status;
}
window.onload= function(){
    document.getElementById('detail').onclick = function(){
        Scroll('detail', 300, 1.3);
    }
    document.getElementById('text').onclick = function(){
        Scroll('text');
    }
}
</script>
</head>
<body>
<p id="detail" style="border:1px dashed #f00;">这是一个段落啦!!哇哈哈哈<br/>点我一下下啦:)</p>
<div id="text" style="border:1px solid #0f0">Hello World!<br/>你敢点我不  -_|||</div>
<button onclick="Scroll('text',400 , 1.2)">Hello World</button>
</body>
</html>
我想问一下js中有这个oldHeight属性吗?我怎么没找到呢?

解决方案 »

  1.   

    看代码里的这段
      if(obj.getAttribute("oldHeight") == null)
        {
            obj.setAttribute("oldHeight", oh);
    明显是自定义属性嘛....
      

  2.   

    不是说要用var 对变量进行定义吗?
      

  3.   

    这个是DOM的自定义属性,不是JS变量.....if(obj.getAttribute("oldHeight") == null){ //如果obj这个DOM的oldHeight属性不存在
       obj.setAttribute("oldHeight", oh);      //就给他设置一个oldHeight属性,值为oh
    }