javascript - JQuery: how to make a superposition of many moving animations which start asynchronously? -
i want make 1 animation superimposed on another. the second (the third, fourth) animation can start asynchronously. need specify difference between new , old positions (not absolute 'left').
e.g.
// 1 moment $( '#my_div' ).animate( { 'leftshift': "+20px", 2000, 'easeoutquad' } ); // moment, may second later $( '#my_div' ).animate( { 'leftshift': "+50px" }, 2000, 'easeoutquad' );
is possible add results of several animations?
the graph clarify want (x-axis: time, y-axis: speed of distance change).
i'd see speeds of animations added, not one-by-one animations.
what did roko c. buljan offer?
but don't want deferred animations, want real-time animation.
note. current syntax ("+20px", "+50px") not supported now. it's example.
i solved problem case.
i need smooth animation. so, when user initiates new push, should new impulse. so, object mustn't stopped before.
i choose 'easing' function smooth movement - 'easeinoutquad'. implemented own version (i removed not necessary parameters standart 'easing' options of jquery):
// browsers works bad if there float px function asyncanimate( obj, props, duration, easing ) { function _gettime() { var d = new date(); return d.gettime(); } var starttime = _gettime(); var duration = duration; var animnum = asyncanimate._animnum; var propints = {}; // increment ints var propfloats = {}; for( var iprop in props ) { var sprop = obj.css( iprop ); propfloats[ iprop ] = 0.0; // in beginning floats 0 } var animinterval = setinterval( function() { var curtime = _gettime(); if( curtime <= starttime + duration ) { var timediff = curtime - starttime; var step = easing( timediff / duration, timediff, 0, 1, duration ); var dstep; var prevstep = asyncanimate._prevsteps[ animnum ]; if( prevstep == null ) { dstep = step; } else { dstep = step - prevstep; } asyncanimate._prevsteps[ animnum ] = step; for( var iprop in props ) { var prop = props[ iprop ]; // can increment int px (for crossbrowser solution), // so, need save float part var propstep = prop * dstep; // calculate total int part var savedfloatpart = propfloats[ iprop ]; var totalpropstep = propstep + savedfloatpart; var totalpropstepint = parseint( totalpropstep ); var totalpropstepfloat = totalpropstep - totalpropstepint; if( math.abs( totalpropstepint ) > 0 ) { obj.css( iprop, "+=" + string( totalpropstepint ) + "px" ); } // reset saved int/float parts propfloats[ iprop ] = totalpropstepfloat; } } else { clearinterval( animinterval ); } }, 10 ); asyncanimate._animnum++; } asyncanimate._prevsteps = {}; asyncanimate._animnum = 0;
examples of usage (do not forgent include js easing functions) or create own:
asyncanimate( $( '#div1' ), { 'margin-left': 50 }, 1000, $.easing.easeinoutelastic ); // ... asyncanimate( $( '.green' ), { 'width': -50, 'height': -50 }, 250, $.easing.easeinoutquad );
you can see how works (try press buttons in short intervals).
i hope it's easy adapt other types of animations.
it seems animate() of jquery not support behavior currently.
Comments
Post a Comment