当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 带缓冲的 收缩与展开内容效果

带缓冲的 收缩与展开内容效果

2019年08月01日  | 移动技术网IT编程  | 我要评论
兼容ie5.5+   firefox2.0(因为我只有这两个浏览器,所以只在它们中做了测试)

看到blueidea很多朋友都发了一些 收缩与展开内容的效果,唯一差的就是一个缓冲效果.不多说,运行一下就知道了,呵呵.
最大高度还需要固定数值.没有很好的解决方案.有兴趣的朋友帮忙解决一下拉,谢谢.
这个代码其他部分不会再发出更新和完善之后的了.如果需要封装就自己做做吧,呵呵.

放出代码为分享学习之用.请尊重别人的作品勿作商业用途!!!!!
复制代码 代码如下:

<html>
<head>
<script>
/*
by auntion
qq 82874972
欢迎喜欢javascript 和 ajax的朋友++我qq,大家共同进步,呵呵
使用方法
调用效果: effect(1,2);
  其中1为: 被改变对象的id
  其中2为: 控制容器的id  可在使用:  this.parentnode.id  取得(父标签的id)
注意给对象id的时候一定不要重复.
*/
function $g(read_id) { return document.getelementbyid(read_id) }
function effect(objectid,parentid){
    if ($g(objectid).style.display == 'none'){
    start(objectid,'opens');
    $g(parentid).innerhtml = "<a href=# onclick=javascript:effect('"+objectid+"','"+parentid+"');>关闭</a>"
    }else{ 
    start(objectid,'close');
    $g(parentid).innerhtml = "<a href=# onclick=javascript:effect('"+objectid+"','"+parentid+"');>打开</a>"
    }
}
function start(objid,method){
var boxheight = $g(objid).offsetheight;               //获取对象高度
var minheight = 5;                                    //定义对象最小高度
var maxheight = 130;                                 //定义对象最大高度
var boxaddmax = 1;                                    //递增量初始值
var every_add = 0.15;                                //每次的递(减)增量  [数值越大速度越快]
var reduce = (boxaddmax - every_add);
var add    = (boxaddmax + every_add);
if (method == "close"){
var alter_close = function(){                        //构建一个虚拟的[递减]循环
    boxaddmax /= reduce;
    boxheight -= boxaddmax;
    if (boxheight <= minheight){
        $g(objid).style.display = "none";
        window.clearinterval(boxaction);
    }
    else $g(objid).style.height = boxheight;
}
var boxaction = window.setinterval(alter_close,1);
}
else if (method == "opens"){
var alter_opens = function(){                        //构建一个虚拟的[递增]循环
    boxaddmax *= add;
    boxheight += boxaddmax;
    if (boxheight >= maxheight){
        $g(objid).style.height = maxheight;
        window.clearinterval(boxaction);
    }else{
    $g(objid).style.display= "block";
    $g(objid).style.height = boxheight;
    }
}
var boxaction = window.setinterval(alter_opens,1);
}
}
</script>
<style>
body,div,table { font-size:12px;}
#control{ width:200; background-color:#ccc; font-size:12px; font-color:#333333; text-align:center; }
#control a { font-weight:900; line-height:30px; color:#333333; }
.test{ height:130;width:200;background-color:#ccc;display:block;overflow:hidden; }
.style1 {
    font-size: 8px;
    color: #ffffff;
}
</style>
</head>
<body>
<div id="control">
  <table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="100%" align="center" bgcolor="#2dd5ff" id="testtab"><a href="#" onclick="effect('test',this.parentnode.id);">关闭</a></td>
    </tr>
  </table>
</div>
<div id="test" class="test">
  <table width="100%" height="130" border="0" cellpadding="4" cellspacing="0" bgcolor="#eeeeee">
    <tr>
      <td colspan="3" align="center" valign="top">这<br>
        里<br>
        是<br>
        第<br>
        二<br>
        ,<br>
        很<br>
        正<br>
        常<br></td>
    </tr>
  </table>
</div>
<div id="control">
  <table width="100%" height="10" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="100%" align="center" valign="bottom" bgcolor="#00c0f0"><span class="style1">▲</span></td>
    </tr>
  </table>
</div>
<br>
<div id="control">
  <table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="100%" align="center" bgcolor="#2dd5ff" id="test1tab"><a href="#" onclick="effect('test1',this.parentnode.id);">关闭</a></td>
    </tr>
  </table>
</div>
<div id="test1" class="test">
  <table width="100%" height="130" border="0" cellpadding="4" cellspacing="0" bgcolor="#eeeeee">
    <tr>
      <td colspan="3" align="center" valign="top">这<br>
        里<br>
        是<br>
        第<br>
        一<br>
        ,<br>
        很<br>
        正<br>
        常<br></td>
    </tr>
  </table>
</div>
<div id="control">
  <table width="100%" height="10" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="100%" align="center" valign="bottom" bgcolor="#00c0f0"><span class="style1">▲</span></td>
    </tr>
  </table>
</div>
</body>
</html>

两个都关闭的状态

[ctrl+a 全选 注:如需引入外部js需刷新才能执行]

一个打开一个关闭的状态

[ctrl+a 全选 注:如需引入外部js需刷新才能执行]

美化下的

[ctrl+a 全选 注:如需引入外部js需刷新才能执行]

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网