javascript 緩衝效果實現代碼 推薦_javascript技巧

來源:互聯網
上載者:User
菜鳥版代碼如下:
理解這段代碼就基本上掌握了
複製代碼 代碼如下:

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> <script type="text/javascript"> function $(id) { return document.getElementById(id); } 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) / 10)) + "px"; } else { clearInterval(st); } } var st = window.setInterval(sw, 1); } function s_f() { var obj = document.getElementById("top0"); var mg = 1; obj.style.display = "block"; obj.style.height = "1px"; var sw = function () { var s_width = parseInt(obj.style.height); if (s_width < 350) { mg *= 1.05; obj.style.height = (s_width + mg) + "px"; } else { obj.style.height = "350px"; clearInterval(st); } } var st = window.setInterval(sw, 1); } function close_f_s(id, w) { var obj = document.getElementById(id); var sw1 = function () { var s_height = parseInt(obj.style.height); if (s_height > 0) { obj.style.height = (s_height - Math.ceil(s_height / 15)) + "px"; } else { clearInterval(st1); obj.style.display = "none"; } } var st1 = window.setInterval(sw1, 1); } </script> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

中級版本
複製代碼 代碼如下:

/*
函數名稱: 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="小秦" /> <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; 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; } 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" >這是一個段落啦!!哇哈哈哈點我一下下啦:)</p> <div id="text" >Hello World!你敢點我不 -_|||</div> <button onclick="Scroll('text', 400, 1.2)">Hello World</button> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

進階版本
這個很全,不過,我是沒有看懂的.- -!!
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 > <div > <div id="idMove" ></div> </div> <div > <div id="idChart" > </div> <div id="idLine" ></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> <script> /* 演算法來源:http://www.robertpenner.com/easing/ */ var Tween = { Linear: function(t,b,c,d){ return c*t/d + b; }, Quad: { easeIn: function(t,b,c,d){ return c*(t/=d)*t + b; }, easeOut: function(t,b,c,d){ return -c *(t/=d)*(t-2) + b; }, easeInOut: function(t,b,c,d){ if ((t/=d/2) < 1) return c/2*t*t + b; return -c/2 * ((--t)*(t-2) - 1) + b; } }, Cubic: { easeIn: function(t,b,c,d){ return c*(t/=d)*t*t + b; }, easeOut: function(t,b,c,d){ return c*((t=t/d-1)*t*t + 1) + b; }, easeInOut: function(t,b,c,d){ if ((t/=d/2) < 1) return c/2*t*t*t + b; return c/2*((t-=2)*t*t + 2) + b; } }, Quart: { easeIn: function(t,b,c,d){ return c*(t/=d)*t*t*t + b; }, easeOut: function(t,b,c,d){ return -c * ((t=t/d-1)*t*t*t - 1) + b; }, easeInOut: function(t,b,c,d){ if ((t/=d/2) < 1) return c/2*t*t*t*t + b; return -c/2 * ((t-=2)*t*t*t - 2) + b; } }, Quint: { easeIn: function(t,b,c,d){ return c*(t/=d)*t*t*t*t + b; }, easeOut: function(t,b,c,d){ return c*((t=t/d-1)*t*t*t*t + 1) + b; }, easeInOut: function(t,b,c,d){ if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b; return c/2*((t-=2)*t*t*t*t + 2) + b; } }, Sine: { easeIn: function(t,b,c,d){ return -c * Math.cos(t/d * (Math.PI/2)) + c + b; }, easeOut: function(t,b,c,d){ return c * Math.sin(t/d * (Math.PI/2)) + b; }, easeInOut: function(t,b,c,d){ return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b; } }, Expo: { easeIn: function(t,b,c,d){ return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; }, easeOut: function(t,b,c,d){ return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; }, easeInOut: function(t,b,c,d){ if (t==0) return b; if (t==d) return b+c; if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; } }, Circ: { easeIn: function(t,b,c,d){ return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b; }, easeOut: function(t,b,c,d){ return c * Math.sqrt(1 - (t=t/d-1)*t) + b; }, easeInOut: function(t,b,c,d){ if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b; return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b; } }, Elastic: { easeIn: function(t,b,c,d,a,p){ if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3; if (!a || a < Math.abs(c)) { a=c; var s=p/4; } else var s = p/(2*Math.PI) * Math.asin (c/a); return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b; }, easeOut: function(t,b,c,d,a,p){ if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3; if (!a || a < Math.abs(c)) { a=c; var s=p/4; } else var s = p/(2*Math.PI) * Math.asin (c/a); return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b); }, easeInOut: function(t,b,c,d,a,p){ if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5); if (!a || a < Math.abs(c)) { a=c; var s=p/4; } else var s = p/(2*Math.PI) * Math.asin (c/a); if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b; return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b; } }, Back: { easeIn: function(t,b,c,d,s){ if (s == undefined) s = 1.70158; return c*(t/=d)*t*((s+1)*t - s) + b; }, easeOut: function(t,b,c,d,s){ if (s == undefined) s = 1.70158; return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; }, easeInOut: function(t,b,c,d,s){ if (s == undefined) s = 1.70158; if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; } }, Bounce: { easeIn: function(t,b,c,d){ return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b; }, easeOut: function(t,b,c,d){ if ((t/=d) < (1/2.75)) { return c*(7.5625*t*t) + b; } else if (t < (2/2.75)) { return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b; } else if (t < (2.5/2.75)) { return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b; } else { return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; } }, easeInOut: function(t,b,c,d){ if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 + b; else return Tween.Bounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b; } } } ////////////////////////////////////////////////////////// var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; }; var Each = function(list, fun){ for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); } }; ////////////////////////////////////////////////////////// var fun, iChart = 550, iDuration = 100; function Move(){ var oM = $("idMove").style, oL = $("idLine").style, t=0, c=500, d=iDuration; oM.left=oL.left="0px"; clearTimeout(Move._t); function _run(){ if(t<d){ t++; oM.left = Math.ceil(fun(t,0,c,d)) + "px"; oL.left = Math.ceil(Tween.Linear(t,0,iChart,d)) + "px"; Move._t = setTimeout(_run, 10); }else{ oM.left = c + "px"; oL.left = iChart + "px"; } } _run(); } //////////////////////////////////////////////////////// function Chart(){ var a = []; for (var i = 0; i < iChart; i++) { a.push('<div + (i-1) + 'px;top:' + (Math.ceil(fun(i,200,-200,iChart))) + 'px;"><\/div>'); } $("idChart").innerHTML = a.join(""); } //////////////////////////////////////////////////////// var arrTween = document.getElementsByName("vTween"); var arrEase = document.getElementsByName("vEase"); Each(arrTween, function(o){ o.onclick = function(){ SetFun(); Chart(); } }) Each(arrEase, function(o){ o.onclick = function(){ SetFun(); Chart(); } }) function SetFun(){ var sTween, sEase; Each(arrTween, function(o){ if(o.checked){ sTween = o.value; } }) Each(arrEase, function(o){ if(o.checked){ sEase = o.value; } }) fun = sTween == "Linear" ? Tween.Linear : Tween[sTween][sEase]; } $("idRun").onclick = function(){ SetFun(); Chart(); Move(); } $("idSpeed").onclick = function(){ if(iDuration == 100){ iDuration = 500; this.value = "加快速度"; }else{ iDuration = 100; this.value = "減慢速度"; } } </script> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

打包下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.