简体   繁体   English

我的菜单没有使用jQuery正确切换“全屏”类

[英]My menu isn't toggling the “fullscreen” class correctly using jQuery

This is the link to my pen on CodePen where I am making this menu - http://codepen.io/PartTimeCoder/pen/YqYmgv 这是链接到我的CodePen猪圈里我提出这个菜单- http://codepen.io/PartTimeCoder/pen/YqYmgv

In my jQuery code, I have the following: 在我的jQuery代码中,我具有以下内容:

$(".menu").click(function() {
    $(this).addClass("fullscreen");
    $("p").show();
});

if ($('.menu').hasClass("fullscreen")) {
    $(".toggle").click(function() {
        $(".menu").removeClass("fullscreen");
    });
} else {
    $(".toggle").click(function() {
        $(this).toggleClass("active");
        $(".menu").toggleClass("active");
        $("p").hide();
    });
}

When the menu has the fullscreen class I just want to remove the fullscreen class from the current section, but not completely close the menu. 当菜单具有全屏类时,我只想从当前部分中删除全屏类,但不能完全关闭菜单。 I don't know how to do this, after struggling with jQuery and Javascript versions of hasClass, I still couldn't get it to work. 我不知道该怎么做,在苦苦挣扎了hasClass的jQuery和Javascript版本之后,我仍然无法使它正常工作。 All help is appreciated! 感谢所有帮助! Thanks in advance! 提前致谢!

Simply invert the if else to inside the click function like so: 只需将if else转换为click函数内部,如下所示:

$(".menu").click(function() {
    $(this).addClass("fullscreen");
    $("p").show();
});


$(".toggle").click(function() {
    if ($('.menu').hasClass("fullscreen")) {
        $(".menu").removeClass("fullscreen");
    } else {
        $(this).toggleClass("active");
        $(".menu").toggleClass("active");
        $("p").hide();
    }
});

http://codepen.io/anon/pen/JXpjXq http://codepen.io/anon/pen/JXpjXq

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

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