简体   繁体   English

用户单击栏图标时如何隐藏/显示导航栏?

[英]How to hide/ show navbar when user clicks on the bar icon?

How do I hide my navbar when the user clicks on the "bar-icon"? 当用户单击“ bar-icon”时,如何隐藏导航栏?

I want to create a navbar which begins hidden and when the user clicks on the bar icon, the navbar displays 我想创建一个导航栏,该导航栏开始hidden ,当用户单击栏图标时,导航栏displays

. Can you do that with JavaScript ? 您可以使用JavaScript做到这一点吗? Or do you need jQuery for that? 还是您需要jQuery

HTML 的HTML

<div class="banner">
    <header class="header">
        <i class="fa fa-bars"></i>
      <nav class="nav">
      </nav>
    </header>
    <div class="bannerContainer">
        <h1>Heading 1</h1>
    </div>
</div>

 nav { height: 60px; width: 100%; background-color: #ccc; } 
 <div class="banner"> <header class="header"> <i class="fa fa-bars"></i> <nav class="nav"> </nav> </header> <div class="bannerContainer"> <h1>Display like that above!</h1> </div> </div> 
I'm just trying to figure out how I can hide the navbar, so no fancy-looking 3D effects when it displays is needed, (for now at least). 我只是想弄清楚如何隐藏导航栏,因此(至少现在)不需要显示时看起来精美的3D效果。 If I haven't provided enough information, please tell me so that I can provide the right amount of information needed to solve this. 如果我没有提供足够的信息,请告诉我,以便我能提供解决此问题所需的适量信息。

Here is how to control that with a simple show and hide with basic javascript: 这是通过基本的javascript进行简单的显示和隐藏控制的方法:

function openMenu(e){
    document.getElementById('menu').style.display = 'block';
}

function closeMenu(e){
    document.getElementById('menu').style.display = 'none';
}

https://jsfiddle.net/7xo87kz6/ https://jsfiddle.net/7xo87kz6/

You may use jQuery for this purpose. 您可以为此目的使用jQuery。 Make sure to include jQuery in your project. 确保在项目中包含jQuery。

Jquery Part jQuery部分

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#bar").click(function(){ $(".bannerContainer").toggle(); }); }); </script> 

HTML Part HTML部分

 nav { height: 60px; width: 100%; background-color: #ccc; } 
 <div class="banner"> <div id = "bar"> <i class="fa fa-bars"></i> <nav class="nav"> </nav> </div> </div> <div class="bannerContainer"> <h1>Display like that above!</h1> </div> 

I dont 100% understand your question but, you could use something like this, 我不是100%理解您的问题,但是,您可以使用类似的方法,

 //JS var button = document.querySelector("button"); var body = document.querySelector("body"); button.addEventListener("click",function() { body.style.background = "red"; }); 
 <!--HTML--> <button>Try Me!</button> 

basically it uses addEventListener to run a function on a click. 基本上,它使用addEventListener在单击时运行一个函数。 And you can add if statements around that to test css attributes as well 您也可以添加if语句来测试css属性

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

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