¡¡¡¡ÕâÀïÏÈ˵Ã÷һϹ¦ÄܺÍÓ÷¨£¬ÒÔ¼°×¢Òâµã£¬ËæºóÊÇÒ»¸öºÜ¼òµ¥µÄ¿ÉÔËÐÐʾÀý¡£
Ó÷¨¼°×¢ÒâÊÂÏ anim(elemId, cssObj, time, animType, funObj) ²ÎÊý˵Ã÷£º elemId £¨±ØÑ¡£©ÐèҪʩ¼Ó¶¯»Ð§¹ûµÄÔªËØid cssObj £¨±ØÑ¡£©¶¯»½áÊøÊ±µÄÑùʽ£¬¶ÔÏóÀàÐÍ£¬¼üÖµ¶ÔÐÎʽ£¬ ÆäÖмüÊÇÄÜÖ±½ÓÓÃÔÚJSÖеġ°Íշ塱ÐÎʽµÄcssÊôÐÔ£¬¶ø²»ÊÇÔÀ´µÄcssÊôÐÔ¡£ ÀýÈ磺{ marginLeft: '200px', top: '200px', borderWidth: '8px'} time £¨±ØÑ¡£©¶¯»³ÖÐøÊ±¼ä£¨µ¥Î»ms£© animType £¨¿ÉÑ¡£©Ä¬ÈÏΪÏßÐԱ仯£¬´úÂëÀïµÄTweenÀàÐͰüº¬¿ÉÑ¡µÄÆäËû²ÎÊý funObj £¨¿ÉÑ¡£©Èç¹ûÒª´ËÑ¡ÏÐèÒª¼ÓÈ뿪ʼºÍ½áÊøÊ±ºòÖ´Ðеĺ¯Êý¡£ ÐÎÈ磺{ elΪelemIdËùÖ¸ÏòµÄÔªËØ start: function (el) { el[removed] = 'start!'; }, complete: function (el) { el[removed] = 'Completed!'; } } ¼¸µã×¢ÒâÊÂÏ 1¡¢Ã»ÓÐ×öµÍ°æ±¾ä¯ÀÀÆ÷¼æÈÝ£¬Ö§³ÖIE8+¡¢FF¡¢chrome¡¢safari¡¢opera 2¡¢×¢ÒâÓÃÄÜÖ±½ÓÓÃÔÚJSÖеġ°Íշ塱ÐÎʽµÄcssÊôÐÔ£¨±¾À´Ó¦°Ñcssת¡°Íշ塱ÐÎʽ£¬ µ«ÊÇ»ù±¾ËùÓÐJS³ÌÐòÔ±¶¼ÄÜÖ±½Óд³öÍÕ·åÐÎʽ£¬ËùÒÔûת£© 3¡¢Èç¹ûÐèÒª°Ñ¶¯»Ó¦Óõ½¾ø¶Ô¶¨Î»£¨position:absolute;£©ÔªËØÉÏ£¬ ÐèҪעÒâÔÚÕâÐ©ÔªËØÉÏÉèÖÃCSSµÄ·½·¨¡£ ÀýÈ磺ÉèÖÃtopºÍmarginTop£¬¶ÔÓÚ¾ø¶Ô¶¨Î»ÔªËØ£¬Ó¦¸ÃÉèÖÃtop¶ø²»ÊÇmarginTop£¬ ¸ü²»Ó¦¸Ã½«¶þÕß»ìºÏʹÓã¬ÒòΪ¶þÕߵIJο¼µãÊDz»Ò»ÑùµÄ£¬Í¬Ê±ÉèÖúÜÈÝÒ×Ôì³É»ìÂÒ¡£ ËùÒÔ£¬ÕâÀïÒ²²»Ö§³ÖͬʱÉèÖöþÕß¡£ ÆäËûÏàËÆµÄͬÀí£¨leftºÍmarginLeft¡¢rightºÍmarginRight£© ͬʱÉèÖÃtopºÍbottom¡¢leftºÍrightÒ²²»Ö§³Ö¡£ 4¡¢ÒýÓÃÁËTween»º¶¯Ëã·¨£¬Ö§³ÖÏßÐÔ¡¢½¥Èë½¥³öµÈ¶àÖֱ仯·½Ê½¡£ 5¡¢¡°¶¯»¶ÓÁС±¹¦ÄÜÉÐδʵÏÖ£¬´Ë°æ±¾Îª³õ¼¶°æ±¾£¬Êè©֮´¦»¹Çë¶à¶àÖ¸Õý¡£<!DOCTYPE HTML> <html> <head> <title></title> <style type="text/css"> #container{ border:1px solid #000; width:500px; height:400px;} #aa{ border:1px solid #000; width:100px; height:40px; background-color:#0f0; position:absolute; left:50px; top:50px; } #bb{ border:1px solid #000; width:500px; height:40px; margin-top:100px;} #cc{ border:1px solid #000; width:500px; height:40px;} </style> </head><body> <div id="container"> <input id="Abegin" type="button" value="¿ªÊ¼" /> <input id="Apause" type="button" value="ÔÝÍ£" /> <input onclick="location.reload()" type="button" value="Ë¢ÐÂÍøÒ³" /> <div id="aa"><br /></div> <div id="bb">dfdfddfsd</div> <div id="cc">gregreger</div> </div> <script type="text/javascript"> (function () { 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 color = { sub: function (str, start, len) { if (len) return str.substring(start, start + len); else return str.substring(start); }, hex: function (i) { // ·µ»Ø16½øÖÆÑÕÉ«±íʾ if (i < 0) return "00"; else if (i > 255) return "ff"; else { var str = "0" + i.toString(16); return str.substring(str.length - 2); } }, //»ñÈ¡ÑÕÉ«Êý¾Ý GetColors: function (sColor) { sColor = sColor.replace("#", ""); var r, g, b; if (sColor.length > 3) { r = color.sub(sColor, 0, 2); g = color.sub(sColor, 2, 2); b = color.sub(sColor, 4, 2); } else { r = color.sub(sColor, 0, 1); g = color.sub(sColor, 1, 1); b = color.sub(sColor, 2, 1); r += r; g += g; b += b; } return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)]; } } var fn = { getElement: function (id) { return typeof id == "string" ? document.getElementById(id) : id; }, objType: function (obj) { switch (Object.prototype.toString.call(obj)) { case "[object Object]": return "Object"; case "[object Number]": return "Number"; case "[object Array]": return "Array"; } }, getStyle: function (elem, name) { // var w3style; if (document.defaultView) { var style = document.defaultView.getComputedStyle(elem, null); name == "borderWidth" ? name = "borderLeftWidth" : name; // ½â¾ö±ê×¼ä¯ÀÀÆ÷½âÎöÎÊÌâ w3style = name in style ? style[name] : style.getPropertyValue(name); w3style == "auto" ? w3style = "0px" : w3style; } return elem.style[name] || (elem.currentStyle && (elem.currentStyle[name] == "auto" ? "0px" : elem.currentStyle[name])) || w3style; }, getOriCss: function (elem, cssObj) { // ´Ë´¦Ö»ÄÜ»ñÈ¡ÊôÐÔֵΪÊýÖµÀàÐ͵ÄstyleÊôÐÔ var cssOri = []; for (var prop in cssObj) { if (!cssObj.hasOwnProperty(prop)) continue; //if (prop != "opacity") cssOri.push(parseInt(fn.getStyle(elem, prop))); //else cssOri.push(100 * fn.getStyle(elem, prop)); if (fn.getStyle(elem, prop) == "transparent" || /^#|rgb\(/.test(fn.getStyle(elem, prop))) { if (fn.getStyle(elem, prop) == "transparent") { cssOri.push([255, 255, 255]); } if (/^#/.test(fn.getStyle(elem, prop))) { cssOri.push(color.GetColors(fn.getStyle(elem, prop))); } if (/^rgb\(/.test(fn.getStyle(elem, prop))) { //cssOri.push([fn.getStyle(elem, prop).replace(/^rgb\(\)/g, "")]); var regexp = /^rgb\(([0-9]{0,3}),\s([0-9]{0,3}),\s([0-9]{0,3})\)/g; var re = fn.getStyle(elem, prop).replace(regexp, "$1 $2 $3").split(" "); //cssOri.push(re); // reΪ×Ö·û´®Êý×é cssOri.push([parseInt(re[0]), parseInt(re[1]), parseInt(re[2])]); } } else if (prop == "opacity") { cssOri.push(100 * fn.getStyle(elem, prop)); } else { cssOri.push(parseInt(fn.getStyle(elem, prop))); } } return cssOri; }, getEndCss: function (cssobj) { var cssEnd = []; for (var prop in cssobj) { if (!cssobj.hasOwnProperty(prop)) continue; //if (prop != "opacity") cssEnd.push(parseInt(cssobj[prop])); //else cssEnd.push(100 * cssobj[prop]); if (prop == "opacity") { cssEnd.push(100 * cssobj[prop]); } else if (/^#/.test(cssobj[prop])) { cssEnd.push(color.GetColors(cssobj[prop])); } else { cssEnd.push(parseInt(cssobj[prop])); } } return cssEnd; } } function _anim(/*elemId, cssObj, time, animType, funObj*/) { this.init.apply(this, arguments[0]); } _anim.prototype = { init: function () { this.elem = fn.getElement(arguments[0]); this.cssObj = arguments[1]; this.cssOri = fn.getOriCss(this.elem, arguments[1]); this.cssEnd = fn.getEndCss(arguments[1]); this.durtime = arguments[2]; this.animType = "Tween.Linear"; this.funObj = null; this.start = false; this.complete = false; this.onPause = false; this.onRestart = false; if (arguments.length < 3) { throw new Error("ÖÁÉÙÒª´«Èë3¸ö²ÎÊý"); } else if (arguments.length == 4) { if (fn.objType(arguments[3]) == "Object") { this.funObj = arguments[3]; for (var p in this.funObj) { if (p.toString() == "start") this.start = true; if (p.toString() == "complete") this.complete = true; } } if (typeof (arguments[3]) == "string") { this.animType = arguments[3]; } } else if (arguments.length == 5) { this.animType = arguments[3]; if (fn.objType(arguments[4]) == "Object") { this.funObj = arguments[4]; for (var p in this.funObj) { if (p.toString() == "start") this.start = true; if (p.toString() == "complete") this.complete = true; } } } this.startAnim(); }, startAnim: function () { if (this.start) this.funObj["start"].call(this, this.elem); var that = this; var t = 0; var props = []; for (var pro in this.cssObj) { if (!this.cssObj.hasOwnProperty(pro)) continue; props.push(pro); } var tt = new Date().getTime(); clearInterval(this.timer); this.timer = setInterval(function () { if (that.onPause) { clearInterval(that.timer); return; } if (t < that.durtime / 10) { t++; for (var i = 0; i < props.length; i++) { var b, c; fn.objType(that.cssOri[i]) != "Array" && (b = that.cssOri[i]); //¿ªÊ¼Öµ fn.objType(that.cssEnd[i]) != "Array" && (c = that.cssEnd[i] - that.cssOri[i]); // ±ä»¯Á¿ var d = that.durtime / 10; // ³ÖÐøÊ±¼ä if (fn.objType(that.cssOri[i]) == "Array" && fn.objType(that.cssEnd[i]) == "Array") { var b1 = that.cssOri[i][0], b2 = that.cssOri[i][1], b3 = that.cssOri[i][2]; var c1 = that.cssEnd[i][0] - that.cssOri[i][0], c2 = that.cssEnd[i][1] - that.cssOri[i][1], c3 = that.cssEnd[i][2] - that.cssOri[i][2]; var r = color.hex(Math.ceil((eval(that.animType))(t, b1, c1, d))), g = color.hex(Math.ceil((eval(that.animType))(t, b2, c2, d))), b = color.hex(Math.ceil((eval(that.animType))(t, b3, c3, d))); that.elem.style[props[i]] = "#" + r + g + b; } else if (props[i].toString() == "opacity") { that.elem.style[props[i]] = Math.ceil((eval(that.animType))(t, b, c, d)) / 100; } else { that.elem.style[props[i]] = Math.ceil((eval(that.animType))(t, b, c, d)) + "px"; } } } else { for (var i = 0; i < props.length; i++) { if (fn.objType(that.cssOri[i]) == "Array" && fn.objType(that.cssEnd[i]) == "Array") { var c1 = that.cssEnd[i][0], c2 = that.cssEnd[i][1], c3 = that.cssEnd[i][2]; var r = color.hex(Math.ceil((eval(that.animType))(t, b1, c1, d))), g = color.hex(Math.ceil((eval(that.animType))(t, b2, c2, d))), b = color.hex(Math.ceil((eval(that.animType))(t, b3, c3, d))); that.elem.style[props[i]] = "#" + r + g + b; } else if (props[i].toString() == "opacity") { that.elem.style[props[i]] = that.cssEnd[i] / 100; } else { that.elem.style[props[i]] = that.cssEnd[i] + "px"; } } clearInterval(that.timer); if (that.complete) that.funObj["complete"].call(that, that.elem); //alert(new Date().getTime() - tt); } }, 10); // Ò»°ãÒª¸ø10ºÁÃëÒì²½µ÷ÓÃʱ¼ä£¬²»ÄÜÊÇ1 }, pause: function () { this.onPause = true; } } window.anim = function () { return new _anim(arguments); } })(); </script> <script type="text/javascript"> var bbtn = document.getElementById("Abegin"), pbtn = document.getElementById("Apause"); var a; bbtn.onclick = function () { a = anim("aa", { left: '200px', top: '200px', width: '200px', height: '200px', backgroundColor: '#f00', borderWidth: '8px' }, 4000, 'Tween.Bounce.easeInOut', { start: function (el) { el[removed] = 'start!'; }, complete: function (el) { el[removed] = 'Completed!'; } } ); } pbtn.onclick = function () { a.pause(); } anim("bb", { marginTop:'160px', opacity:0.1, width: '200px', height: '200px', borderWidth: '8px' }, 2000, 'Tween.Bounce.easeIn', { start: function (el) { el[removed] = 'start!'; }, complete: function (el) { el[removed] = 'Completed!'; } } ); </script> </body> </html>
ÍÆ¼öÐÅÏ¢
- jqueryʵÏÖÍøÕ¾Í¼Æ¬ÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔØ½ø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøÕ¾Í¼Æ¬ÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼ÆÄ£Ê½(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆß¸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ