Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespacing animations

Tags:

jquery

Is it possible to namespace animations? Specifically, my problem is that on a given element $myElement I am doing animations of two types. Now I would like to use .stop() on only one of these types, not both.

How can I do that?

EDIT

Code available here: http://jsfiddle.net/y34ME/1/

My problem is that when I click the span I want it to fade away, regardless of whether I do a mouseout. Currently, the mouseout interrupts the fading away because of the .stop(), but I need the .stop() to prevent mouseover and mouseout events to queue up.

like image 375
Randomblue Avatar asked Sep 24 '11 18:09

Randomblue


2 Answers

I think what you really want is to not trigger the mouseout at all if you're already fading the element away. Andrew's method works well, but if you want to keep your event handlers intact (for example, if there's a way to show this element again), use a state class:

$('p').delegate('span:not(.hidden)', {
    'mouseover': function () {
       $(this).stop(true, true).animate({backgroundColor: 'blue'}, 1000);
    },
    'mouseout':function () {
       $(this).stop(true, true).animate({backgroundColor: 'white'}, 1000);
    },
    'click': function () {
       $(this).addClass('hidden').stop(true, true).animate({backgroundColor: 'green'}, 1000).fadeTo(2000, 0);
    }
});

http://jsfiddle.net/y34ME/4/

like image 150
Elliot Nelson Avatar answered Sep 19 '22 08:09

Elliot Nelson


Is there a reason you need to use delegate? This code seems to do what you want:

$("span").hover(
    function() {
        $(this).animate({backgroundColor: "blue"}, 1000);
    },
    function() {
        $(this).animate({backgroundColor: "white"}, 1000);
    }
);

$("span").click(function() {
    $(this).animate({backgroundColor: "green"}, 1000).fadeTo(2000, 0);
});
like image 35
coffeeandcode Avatar answered Sep 21 '22 08:09

coffeeandcode