android.nick
android.nick

Reputation: 11207

why doesn't this line of code work?

im trying to basically make it so the user can't click #shareheart twice in a row, making the animation screw up. Maybe you could say i'm trying to create an active state without adding and removing classes?

why doesn't this work? The first piece of code is what's not working, it's not following this if statement, did i do something wrong here?

if($('.share-text').not(':animated') && $('.share-text span').is(':visible')) {
    // do something
}

Here's the full code:

$('#shareheart').click(function() {
    if ($('.share-text:animated').length == 0 && $('.share-text span').is(':visible')) {
        $('.share-text span').animate({'opacity': '0'}, 800, function() {
            $("#share-what").fadeOut(400)
            $('.share-text').stop(true, false).animate({'width': 'toggle','padding-left': 'toggle','padding-right': 'toggle'}, 800)
            $('#short-url').css('background-image', "url('images/drink.png')");
        })
    } else {
        $('.share-text').stop(false, true).animate({'width': 'toggle','padding-left': 'toggle','padding-right': 'toggle'}, 800, function() {
            $('.share-text span').animate({'opacity': '1'}, 800)
        });
    }
});

Upvotes: 1

Views: 100

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074365

$('.share-text').not(':animated')

...will give you a jQuery object (and so will always be truthy if tested as a boolean). not doesn't test a condition and return the result of the text, it filters the elements matched by the jQuery object.

You haven't said what the code is supposed to do, but you might want:

if ($('.share-text:animated').length == 0 && $('.share-text span').is(':visible')) {
    // do something
}

...if the first half is looking to see if there are no elements with the "share-text' class that are not animated.

Upvotes: 6

Kobi
Kobi

Reputation: 138017

Alternatively, you can combine your condition to a single selector:

if($(".share-text:not(:animated) span:visible").length)
{
}

The condition will return 0 (a false value) if it share-text is a animated, or if the span is invisible.

Upvotes: 4

Felix Kling
Felix Kling

Reputation: 816462

I assume you misunderstand the functioning of not(). It filters the set, it does not test whether the elements are conform to the given selector. You probably want something like:

!$('.share-text').is(':animated')

Upvotes: 1

Related Questions