JQuery animimate - any way to "knock down" a full trigger?

Suppose I have a viewport with a specific width and height. I clone an element from the field of view on the right side and fly through the viewport in a random position ywith this code:

    ...
    .css({
        'left': BASE_NODE_INIT_LEFT_PX,
        'top' : rand(BASE_NODE_INIT_TOP_MIN_PX, BASE_NODE_INIT_TOP_MAX_PX) + 'px',
        'width': _width + 'px',
        'height': _height + 'px',
    })
    .animate({
        left: BASE_NODE_ANIMATE_DISTANCE_X_PX
    }
    ...

Simple animation. Here's the interesting part: at every step I would like to apply some physics to this element.

Something like that:

        ...
        step:function(currentStep){
            if($(this).data('animating-wind') !== true)
            {
                $(this).data('animating-wind',true);
                var wind = (rand(1,2) == 2) ? '+=' + rand(1, 100) + 'px' : '-=' + rand(1, 100) + 'px';
                $(this).animate({
                    'top': wind,
                    'text-indent': wind,
                },{
                    duration: 500,
                    complete: function(){
                        $(this).data('animating-wind',false);
                    },
                    queue: false
                });
            }
        }
        ...

Basically, what happens, instead of flying straight across from right to left, it slows down, accelerates, rises and falls by accident, just as I assume.

, , , "" , , , ( my complete:function(){ ...$(this).remove(); ...}

, , JQuery , , : " , - ". , .

, - , , , , ' onComplete. , , - , complete:function(){} , step:function(){} " " stop(true,false) if true. - 100 400-1200 , .

TL; : , - , ?

UPDATE: @mu :

/** 
 * Create a new clone of the master div for user interaction. Position it 
 * randomly off the canvas, and animate it across at a random speed.
 */

function spawn_target() {
    spawn_timeout = setTimeout(function() {
        var bonus = (rand(1, BONUS_ODDS) == BONUS_ODDS) ? ' ' + BONUS_CLASS : '';
        var _img_src = (bonus.length > 0) ? BONUS_NODE_IMG_SRC : BASE_NODE_IMG_SRC;
        var _width = $('#' + BASE_NODE_ID).width();
            _width = Math.floor(rand(_width / 3, _width));
        var _height = _width;
        var _framerate = 10 - Math.floor(_height/10);
        var _clone = $('#' + BASE_NODE_ID).clone().addClass(CLONE_CLASS + bonus).attr('id', CLONE_ID).appendTo('#' + CANVAS_ID).css({
            'left': BASE_NODE_INIT_LEFT_PX,
            'top': rand(BASE_NODE_INIT_TOP_MIN_PX, BASE_NODE_INIT_TOP_MAX_PX) + 'px',
            'width': _width + 'px',
            'height': _height + 'px',
        })

        $(_clone).children('img').attr('src', _img_src);

        /**
         * Handle the actual flight across the viewport 
         * @param object _this The clone we are animating manually
         * @return object pointer This contains the interval so we can clear it later
        */
        function fly(_this) {

            var animating_wind = false;
            var pointer = {
                timer_id: null
            };
            pointer.timer_id = setInterval(function() {

                // Get rid of the node if it is no longer visible in the viewport
                if (!$(_this).is(':onviewport')) {
                    clearInterval(pointer.timer_id);
                    $(_this).remove();
                    adj_game_data(GAME_DATA_LIVES_ID, -1);
                    check_lives();
                    spawn_target();
                }

                // Move node along with slight realism
                var distance = rand(FLY_DISTANCE_MIN, FLY_DISTANCE_MAX);
                $(_this).css('left', '-=' + distance + 'px');

                // Only trigger the wind animation when it is idle
                if (animating_wind !== true) {
                    animating_wind = true;

                    // Setup the wind physics
                    var _wind_power = rand(WIND_POWER_MIN, WIND_POWER_MAX);
                    var _wind_dir = (rand(1, 2) == 2) ? '+=' : '-=';

                    // Override wind direction to keep node inside viewport
                    if(($(_this).offset().top + $(_this).height() + _wind_power) > CANVAS_HEIGHT)
                    {
                        _wind_dir = '-=';
                    }
                    if(($(_this).offset().top - _wind_power) < CANVAS_TOP_BUFFER_PX)
                    {
                         _wind_dir = '+=';   
                    }

                    var _wind = _wind_dir + _wind_power + 'px';  

                    // Apply the wind physics and don't let the node break the top or bottom
                    // boundaries of the viewport
                    $(_this).animate({
                        'top': _wind
                    }, {
                        duration: WIND_ANIMATION_DURATION,
                        complete: function() {
                            animating_wind = false;
                        },
                        queue: false
                    });
                }
            }, _framerate);

            return pointer;
        }
        $(_clone).data('interval', fly(_clone));

    }, rand(SPAWN_TARGET_TIMEOUT_MIN, SPAWN_TARGET_TIMEOUT_MAX));
}

: http://jsfiddle.net/AlienWebguy/DmtQ7/embedded/result/

, , JQuery - , . $(obj).stop() clearInterval($(obj).data('interval').timer_id);. * ...

+5
1

, , (-JavaScript):

function animate_it($thing) {
    var wind     = false;
    var timer_id = setInterval(function() {
        // Compute the movement for this step.
        // Then factor in the wind effect and update the `wind` variable.
        // Then apply the movement to `$thing`.
        if(it_is_off_the_page($thing))
            clearInterval(timer_id);
    }, n);
}

-, , animate_it . , , n .

, jQuery animate , , . ( ) .

, timer_id , . :

function animate_it($thing) {
    var wind    = false;
    var pointer = { timer_id: null };
    pointer.timer_id = setInterval(function() {
        // Compute the movement for this step.
        // Then factor in the wind effect and update the `wind` variable.
        // Then apply the movement to `$thing`.
        if(it_is_off_the_page($thing)) {
            clearInterval(pointer.timer_id);
            pointer.timer_id = null;
        }
    }, n);
    return pointer;
}

var timer = animate_it($thing);

// And then later, to shut it down...
if(timer.timer_id)
    clearInterval(timer.timer_id);

, " " animate_it, , .


animate , animate , . , animate ( , ).

+1

All Articles