JavaScript 实现缓冲动画效果的三种代码方案
作者:页面旅途时间:2026-10-24 06:16:54浏览:380
本文介绍了使用 JavaScript 实现元素高度缓冲展开与关闭效果的三种代码方案。从基础的菜鸟版代码入手,逐步过渡到封装良好的中级 Scroll 函数,最后提及高级 Tween 实现。文章详细解析了各版本的核心逻辑、参数设置及注意事项,帮助开发者理解由快到慢的运动缓冲原理及其在 Web 开发中的应用。
JavaScript 缓冲动画实现概述
以下是初学者的基础实现代码:
理解这段代码有助于掌握缓冲效果的基本原理。
具体实现代码如下:
function f_s() {
var obj = document.getElementById("top");
obj.style.display = "block";
obj.style.height = "1px";
var sw = function () {
var s_width = parseInt(obj.style.height);
if (s_width < 350) {
obj.style.height = (s_width + Math.ceil((350 - s_width) / 15)) + "px";
}
} else {
clearInterval(st);
}
}
var st = window.setInterval(sw, 1);
}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="author" content="Stri" /> <title>缓冲效果</title> <style type="text/css"> * { padding:0px; margin:0px; } #top { width:80px; height:350px; float:left; background-color:#090; display:none; color:#fff; text-align:right; border:1px solid #000; s } #top0 { width:80px; float:right; height:350px; background-color:#090; display:none; color:#fff; text-align:right; border:1px solid #000; s } </style> <link href="cssStyle/style_1_common.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="page_head"></div> <div id="wrap"> <h1>缓冲效果</h1> 打开这是一个由快到慢的过程关闭<br> 打开这是一个由慢到快的过程关闭 <div id="top"></div> <div id="top0"></div> </div> <div id="page_foot"></div> </body> </html> [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
中级版本:封装 Scroll 函数
中级版本实现
相关代码如下:
/*
函数名称: 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;
obj.style.display = "block";
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;
}<!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="小秦" /> </head> <body> <p id="detail" style="border:1px dashed #f00;">这是一个段落啦!!哇哈哈哈点我一下下啦:)</p> <div id="text" style="border:1px solid #0f0">Hello World!你敢点我不 -_|||</div> <button onclick="Scroll('text', 400, 1.2)">Hello World</button> </body> </html>[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
高级版本:Tween 效果实现
高级版本介绍
此版本功能更为全面,但代码复杂度较高,可能不易理解。
http://www.cnblogs.com/cloudgamer/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Tween</title> </head> <body> <div style="padding-left:50px;"> <div style="position:relative; border:1px solid #000000; width:550px; height:50px;"> <div id="idMove" style="width:50px; height:50px; background:#930; position:absolute;"></div> </div> <div style="position:relative;width:550px;height:200px; margin-top:50px;"> <div id="idChart" style="border:1px solid #000;height:200px;"> </div> <div id="idLine" style="position:absolute;top:0;left:0;height:200px;width:1px;background:#000;"></div> </div> </div> <div> <p> Tween类型: <br> <label> <input name="vTween" type="radio" value="Linear" checked="checked" /> Linear </label> <label> <input name="vTween" type="radio" value="Quad" /> Quadratic </label> <label> <input name="vTween" type="radio" value="Cubic" /> Cubic </label> <label> <input name="vTween" type="radio" value="Quart" /> Quartic </label> <label> <input name="vTween" type="radio" value="Quint" /> Quintic </label> <label> <input name="vTween" type="radio" value="Sine" /> Sinusoidal </label> <br> <label> <input name="vTween" type="radio" value="Expo" /> Exponential </label> <label> <input name="vTween" type="radio" value="Circ" /> Circular </label> <label> <input name="vTween" type="radio" value="Elastic" /> Elastic </label> <label> <input name="vTween" type="radio" value="Back" /> Back </label> <label> <input name="vTween" type="radio" value="Bounce" /> Bounce </label> </p> <p> ease类型: <br> <label> <input name="vEase" type="radio" value="easeIn" checked="checked" /> easeIn </label> <label> <input name="vEase" type="radio" value="easeOut" /> easeOut </label> <label> <input name="vEase" type="radio" value="easeInOut" /> easeInOut </label> </p> <input id="idSpeed" type="button" value="减慢速度" /> <input id="idRun" type="button" value=" Run " /> </div> </body> </html> [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
打包下载
作者最新文章
JS监听滚动事件:回到顶部按钮的显示与隐藏逻辑
2026-11-05 16:19:37
JavaScript怎么读写LocalStorage与SessionStorage
2026-11-05 15:40:11
anime.js 与 GSAP 对比:轻量级项目该怎么选择动画库
2026-11-05 11:24:55
flv.js 与 hls.js 在直播场景下的选型对比
2026-11-05 08:18:52
JavaScript能做什么?前端开发中的作用与应用场景解析
2026-11-04 23:17:54
