简体   繁体   English

非自举式可折叠导航栏

[英]Non-Bootstrap Collapsible Navbar

I'm trying to set-up my code so that when I click a link with the id of "#toggleNav" 我正在尝试设置代码,以便当我单击ID为“ #toggleNav”的链接时

    <a href="#" id="toggleNav"><i class="fa fa-bars"></i></a>

It will add or remove the ".hidden" class of an element with an id of "#nav" 它将添加或删除ID为“ #nav”的元素的“ .hidden”类

    <ul class="navbar navCollapse" id="nav">
      <span class="links">
        <a href="#">
          <li>Home</li>
        </a>
    ...

".hidden" class: “ .hidden”类:

    *.hidden {
      display: none;
    }

So that it will act as a toggle on a navigation bar, but since I have this only on if the screen size is less than 600px, I would like it to default to .hidden ONLY if the screen is 599px or less, AND default to shown ONLY if the size is 600px or more. 这样它就可以充当导航栏上的切换按钮,但是由于只有在屏幕尺寸小于600像素时才启用此开关,因此我希望仅在屏幕小于599像素时将其默认设置为.hidden,而将默认设置为仅在尺寸为600px或更大时显示。

If there's either an easier way to do it, or I need a plugin, I would be grateful for any responses that would help. 如果有一种更简单的方法,或者我需要一个插件,我将不胜感激任何有帮助的答复。

And I don't want to use any bootstrap or other frameworks - preferably just CSS or javascript, and maybe jQuery. 而且我不想使用任何引导程序或其他框架-最好仅使用CSS或javascript,甚至使用jQuery。

Thanks! 谢谢!

You can do this via css media queries, like so: 您可以通过CSS媒体查询来执行此操作,如下所示:

@media (max-width: 600px) {
  *.smhidden {
    display: none;
  }
}

Add this class to your element so that even when the .hidden class is gone, if the screen size is less than 600, the .smhidden class will still be hiding the element. 将此类添加到元素中,以便即使.hidden类消失了,如果屏幕尺寸小于600,.smhidden类仍将隐藏该元素。

EDIT 编辑

You can set defaults via JavaScript by using: 您可以使用以下方法通过JavaScript设置默认值:

var windowWidth = window.innerWidth;
if (windowWidth < 600) {
    $('#nav').addClass('hidden');
} 

I just found a way to do this, but I would rather not use two separate buttons. 我只是找到了一种方法来执行此操作,但我不想使用两个单独的按钮。

    <span class="float-right">
        <a href="#" id="toggleNavShow"><i class="fa fa-bars"></i></a>
        <a href="#" id="toggleNavHide" class="hidden"><i class="fa 
        fa-bars"></i></a>
    </span>

And the repetitive js... 还有重复的js ...

    $(document).ready(function(){
      $("#toggleNavShow").click(function(){
        $("#nav").show();
        $("#toggleNavShow").hide();
        $("#toggleNavHide").show();
      });
      $("#toggleNavHide").click(function(){
        $("#nav").hide();
        $("#toggleNavShow").show();
        $("#toggleNavHide").hide();
      });
   });

If there's a way to just use one button I'd love to know... I'm not all that experienced with js. 如果有一种方法可以使用一个按钮,我想知道...我对js的了解还不是全部。

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

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