简体   繁体   English

在单击函数上两次使用toggleclass,而不是对响应菜单使用show()hide()

[英]Using toggleclass twice on click function instead of show() hide() for responsive menu

I'm experimenting with a responsive menu and my aim is to have a normal menu that goes to display : none when the browser is resized to a smaller size, and is instead replaced by a grid symbol (for now it's just a coloured box). 我正在尝试一个响应菜单,我的目标是要显示一个普通菜单:将浏览器调整为较小的尺寸时显示为无,而是由网格符号代替(目前它只是一个彩色框) 。

Some HTML is here: 一些HTML在这里:

<div class="mplace">

<a class="grid" href="#"></a>
<a class="close" href="#"></a>

<div class="holder">

<ul class="menlist">
<li><a href="http://www.w3schools.com">Home</a></li><li>
<a href="http://www.bing.com">Bing</a>
</li><li><a href="http://www.yahoo.com">Yahoo</a>
</li><li><a href="http://www.google.com">Google</a>
</li><li><a href="http://www.w3schools.com/about/default.asp">Contact</a></li>

</ul>

</div>
</div>

Anyway, I don't know if this is a real world problem but I want to be able to click the symbol and have the menu appear, then when the close symbol is clicked the menu is hidden again. 无论如何,我不知道这是否是一个现实问题,但我希望能够单击该符号并显示菜单,然后单击关闭符号后,菜单将再次隐藏。 Then the normal CSS should override the jquery so that if the browser is again resized to full screen the grid symbol will be hidden. 然后,普通的CSS应该覆盖jquery,以便如果再次将浏览器调整为全屏大小,则网格符号将被隐藏。

The problem I have is this is not happening with the jquery I am using. 我的问题是我使用的jQuery不会发生这种情况。 I have tried using toggleClass instead to toggle between display : none, and display : block, but this leaves the grid symbol showing when you go back to full screen. 我尝试使用toggleClass来代替在display:none和display:block之间切换,但是当您回到全屏状态时,这会显示网格符号。

In any case. 任何状况之下。 This jquery works to alternate between symbols and show and hide the menu but when you go back to full screen after having clicked the symbol it does not go back to display : none. 该jquery可以在符号之间交替显示并显示和隐藏菜单,但是当您单击符号后返回全屏显示时,它不会返回显示:无。 You can see the issue at this JSfiddle 您可以在此JSfiddle中看到问题

$(document).ready(function() {

$('.grid').click(function(){
    $('.holder ul').toggleClass('menlist');
    $('.grid').hide();
    $('.close').show();
});

$('.close').click(function(){
    $('.holder ul').toggleClass('menlist');
    $('.close').hide();
    $('.grid').show();
});

});

However, what I want to be able to do is just put toggleClass like this. 但是,我想要做的就是像这样放置toggleClass。 In this case the class of 'hide' and 'show' would be in the CSS as display : none and display : block 在这种情况下,“隐藏”和“显示”的类将在CSS中显示为:none和display:block

$(document).ready(function() {

$('.grid').click(function(){
    $('.holder ul').toggleClass('menlist');
    $('.grid').toggleClass('hide');
    $('.close').toggleClass('show');
});

});

This would mean when the grid button is clicked it would be set to display : none while the x symbol would be set to display : block. 这意味着单击网格按钮时,它将设置为显示:无,而将x符号设置为显示:块。 Then vice versa would happen with some more jquery. 然后反之亦然会发生更多的jquery。

I have tried everything I can think of, but it just isn't working. 我已经尝试了所有我能想到的方法,但是它没有用。

You really just need to use toggleClass and then toggle a class to your menu and give this your original menu css of display:none then the toggled class a css of display:block like the following: 您实际上只需要使用toggleClass,然后将一个类切换到菜单,并为其提供原始菜单CSS的display:none,然后将切换后的类的display:block的CSS如下所示:

You can put both your menu open and menu close buttons on the same jquery click function seperated by a comma and it will add and remove the class accordingly. 您可以将菜单打开按钮和菜单关闭按钮放在同一个jquery click函数上,并用逗号分隔,它将相应地添加和删除类。

Here is a fiddle Fiddle Demo 这是一个小提琴小提琴演示

$(document).ready(function() {
  $('.grid, .menu-close').click(function(){
        $('.menu-list').toggleClass("menu-open");
  });
});

then your css will be like the following: 那么您的CSS将如下所示:

.menu-list{
  display:none;
}
.menu-list.menu-open{
  display:block;
}

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

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