Created
September 29, 2008 08:36
-
-
Save os0x/13572 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// Tweener Like snippet | |
new Tween(div.style,{time:1, onComplete:function(){},left:{to:0,from:100,tmpl:"+$#px"}}); | |
// @require http://gist.github.com/13572.txt | |
*/ | |
function Tween(item, opt) { | |
var self = this, TIME = 10, time = (opt.time||1) * 1000, TM_EXP = /(\+)?\$([\#\d])/g, sets = [], | |
easing = opt.transition || function(t, b, c, d){return c*t/d + b;}, _T = {time:1,onComplete:1,transition:1,delay:1}; | |
for (var k in opt) if (!_T[k]) { | |
var set = opt[k], from = set.from || parseFloat(item[k]) || 0, values = [], tmpl = set.tmpl || '$#'; | |
sets.push({key:k, from:from, to:set.to, tmpl:tmpl}); | |
} | |
var L = sets.length, delay = opt.delay*1000 || 0, startTime = new Date()*1 + delay, endTime = time + startTime, dist = endTime - startTime, run = function(){ | |
var now = new Date()*1, tim = now - startTime; | |
for (var k = 0; k < L; ++k) { | |
var set = sets[k], val = easing(tim, set.from, set.to - set.from, dist); | |
item[set.key] = set.tmpl.replace(TM_EXP, function(m, p, m1){return p && val < 0 ? 0 : (m1 == '#' ? val : val.toFixed(m1));}); | |
} | |
if (tim <= dist) {setTimeout(function(){run.call(self);},TIME);} | |
else { | |
for (var k = 0; k < L; ++k) {item[sets[k].key] = sets[k].tmpl.replace(TM_EXP, sets[k].to);} | |
if (typeof opt.onComplete == 'function') opt.onComplete(item); | |
} | |
}; | |
delay ? setTimeout(function(){run();},delay) : run(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment