简体   繁体   English

简单的jQuery条件语句

[英]Simple jQuery conditional statement

I have a very simple script on my site. 我的网站上有一个非常简单的脚本。

If header is visible it'll remove the class fixed , if not, it'll add it. 如果header是可见的,它将删除fixed类,否则将添加它。

$(function() {
        $(window).scroll(function() {
            $(".wrapper > header").visible(true) ?
                $(".wrapper > main > nav").removeClass("fixed") :
                $(".wrapper > main > nav").addClass("fixed");
        });
    });

As well as toggling between the fixed class, I'd also like to toggle between a fixed2 class. 除了在fixed类之间切换之外,我还想在fixed2类之间切换。

So add: 因此添加:

$(".scrollWrapper").removeClass("fixed2") :
$(".scrollWrapper").addClass("fixed2");

But I'm unsure how to add this. 但是我不确定如何添加它。

Since you would have two statements, you need to revert to a normal if/else: 由于将有两个语句,因此需要恢复为正常的if / else:

if($(".wrapper > header").visible(true)){
     $(".wrapper > main > nav").removeClass("fixed");
     $(".scrollWrapper").removeClass("fixed2");
}else{
     $(".wrapper > main > nav").addClass("fixed");
     $(".scrollWrapper").addClass("fixed2");
}

Alternatively, don't use any conditionals and use the toggleClass method for this: 或者,不要使用任何条件,并为此使用toggleClass方法:

var toggle = $(".wrapper > header").visible(true);
$(".wrapper > main > nav").toggleClass("fixed", toggle);
$(".scrollWrapper").toggleClass("fixed2", toggle);

NOTE: the above supposes you have a visible -plugin, as otherwise that method won't work. 注意:以上假设您有一个visible -plugin,否则该方法将不起作用。 To test for visibility you should use is .is(':visible') 要测试可见性,您应该使用.is(':visible')

There is no property called visible in jquery object. jQuery对象中没有所谓的visible属性。 So you need to use 所以你需要使用

$(".wrapper > header").is(":visible)

so the expression becomes : 因此表达式变为:

 $(".wrapper > header").is(":visible") ?
            $(".wrapper > main > nav").addClass("fixed") :  $(".wrapper > main > nav").removeClass("fixed") ;

For Toggling : 切换:

$(".scrollWrapper").toggleClass('fixed fixed2');

if your doing a few things based on a condition its probably better to use an if statement as opposed to ternary. 如果您根据条件进行一些操作,最好使用if语句而不是三元语句。

$(function() {
    $(window).scroll(function() {
        if($(".wrapper > header").visible(true)){
            $(".wrapper > main > nav").removeClass("fixed");
            $(".scrollWrapper").removeClass("fixed2");
        }else{
            $(".wrapper > main > nav").addClass("fixed");
            $(".scrollWrapper").addClass("fixed2");
        }
    });
});

Ternarys are great but as everything they have their use-cases. 三元函数很棒,但是正如它们都有用例一样。 This may not be one of them. 这可能不是其中之一。

You can use an if-else statement instead of a ternary one. 您可以使用if-else语句代替三元语句。 After all, jQuery is a JavaScript library. 毕竟,jQuery是一个JavaScript库。

$(function() {
    $(window).scroll(function() {
        if ($(".wrapper > header").visible(true)) {
            $(".wrapper > main > nav").removeClass("fixed");
            $(".scrollWrapper").removeClass("fixed2");
        } else {
            $(".wrapper > main > nav").addClass("fixed");
            $(".scrollWrapper").addClass("fixed2");
        }
    });
});

If you're really set on using only ternary statements, you just have to add another one: 如果您真的只使用三元语句,则只需添加另一个:

$(function() {
    $(window).scroll(function() {
        $(".wrapper > header").visible(true) ?
            $(".wrapper > main > nav").removeClass("fixed") :
            $(".wrapper > main > nav").addClass("fixed");
        $(".wrapper > header").visible(true) ?
            $(".scrollWrapper").removeClass("fixed2") :
            $(".scrollWrapper").addClass("fixed2");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM