为什么这行代码不起作用?

时间:2023-02-07 21:05:49

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?

我试图基本上这样做,所以用户不能连续两次点击#shareheart,使动画搞砸了。也许你可以说我试图创建一个活动状态而不添加和删除类?

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语句,我在这里做错了吗?

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)
        });
    }
});

3 个解决方案

#1


4  

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.

如果共享文本是动画的,或者跨度不可见,则条件将返回0(假值)。

#2


6  

$('.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.

...将为您提供一个jQuery对象(如果作为布尔值测试,将始终是真实的)。不会测试条件并返回文本的结果,它会过滤jQuery对象匹配的元素。

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.

...如果前半部分正在查看是否没有“share-text”类没有动画的元素。

#3


1  

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:

我假设你误解了not()的功能。它过滤集合,它不测试元素是否符合给定的选择器。你可能想要这样的东西:

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

#1


4  

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.

如果共享文本是动画的,或者跨度不可见,则条件将返回0(假值)。

#2


6  

$('.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.

...将为您提供一个jQuery对象(如果作为布尔值测试,将始终是真实的)。不会测试条件并返回文本的结果,它会过滤jQuery对象匹配的元素。

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.

...如果前半部分正在查看是否没有“share-text”类没有动画的元素。

#3


1  

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:

我假设你误解了not()的功能。它过滤集合,它不测试元素是否符合给定的选择器。你可能想要这样的东西:

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