Hide Navbar on mobile after a link is clicked - javascript

I'm building a small website and I'd like to hide the mobile navbar after a link is clicked.
I've found some similar questions in this forum: tried hide() and slideup() but it works for the very first link clicked, then I can't re-open my navbar.
The idea is to automatically close the navbar once a link is clicked to simplify and make the website more pleasant.
Here the html of my navbar:
<nav class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav ">
<li><a class="click" href="./index.html" title="">01 : Home</a></li>
<li><a class="click" href="#thedavid" title="">02 : The Project</a></li>
<li><a class="click" href="#aboutus" title="">03 : About us</a></li>
<li><a class="click" href="#roadmap" title="">04 : Our Goal</a></li>
<li><a class="click" href="#faq" title="">05 : Faq</a></li>
</ul>
</div>
</div>
</nav>

document.getElementById('sr-only').addEventListener('click',myFunction)
function myFunction(){
let nav=document.getElementById('navbar-collapse')
if(nav.style.display=="" || nav.style.display=='block')
nav.style.display = 'none'
else nav.style.display='block'
}
<nav class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
<span id="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav ">
<li><a class="click" href="./index.html" title="">01 : Home</a></li>
<li><a class="click" href="#thedavid" title="">02 : The Project</a></li>
<li><a class="click" href="#aboutus" title="">03 : About us</a></li>
<li><a class="click" href="#roadmap" title="">04 : Our Goal</a></li>
<li><a class="click" href="#faq" title="">05 : Faq</a></li>
</ul>
</div>
</div>
</nav>

Related

is there any way to make push-down content navbar with bootstrap 4

please refer "https://codepen.io/anon/pen/XbBPPY" i want same result but instead of using bootstrap-3, i want to use bootstrap-4.
U can see the code in the reference link "https://codepen.io/anon/pen/XbBPPY"
HTML
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav" id="nav">
<li class="nav-link active">Link</li>
<li class="nav-link">Link</li>
<li class="nav-link">Link</li>
<li class="dropdown">
Drop-Down Menu<i class="fa fa-angle-down flipped"></i>
<ul class="dropdown-menu" id="menu">
<li class="nav-link">
<i class="fa fa-microphone fa-2x"></i>Option
</li>
<li class="nav-link">
<i class="fa fa-newspaper-o fa-2x"></i>Option
</li>
</ul>
</li>
<li class="nav-link">Link</li>
<li class="nav-link">Link</li>
</ul>
</div>
</div>
</nav>
<div class="container" style="background: red;height:100px; width: 100%;">
</div>```

How to create Multiple Dropdowns into the same dropdown.

I've been trying to replicate the icons part in the navigation bar on this website https://www.vannuysmaserati.com/
This is what I have so far. I'm not using icons yet. I would like the dropdowns to use one dropdown menu not have one for each.
https://codepen.io/dmads2355/pen/bQPgbJ
<br>
<br>
<div class="container example2">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar2">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://disputebills.com"><img src="https://res.cloudinary.com/candidbusiness/image/upload/v1455406304/dispute-bills-chicago.png" alt="Dispute Bills">
</a>
</div>
<div id="navbar2" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">Home</li>
<li class="dropdown">
P <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<p>phone</p>
</ul>
</li>
<li class="dropdown">
H <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<p>Hours</p>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container-fluid -->
</nav>
</div>
Thanks

Bootstrap Menu Collapse Disappear

Hello all,
I am trying to make a navbar disappear after being clicked (It also has a sub menu dropdown). I tried using this (in fact it worked for a while but now does not). Any help would be appreciated, David
<script type="text/javascript">
<!--Make the menu collapse on option click in mobile mode-->
jQuery(document).ready(function () {
jQuery(".nav .dropdownmenu, .navbar-header .dropdownmenu").click(function(event) {
// check if window is small enough so dropdown is created
jQuery(".navbar-collapse").removeClass("in").addClass("collapse");
});
});
</script>
This is the dropdown menu
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top" >
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<img src = "images/logo.png" height="30px" style="margin-top: 10px; margin-left: 5px;">
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdownmenu active">Home</li>
<li class="dropdownmenu">Sales</li>
<li class="dropdownmenu">Lettings</li>
<li class="dropdownmenu">Financial Services</li>
<li class="dropdown">
Branches <span class="caret"></span>
<ul class="dropdown-menu">
<li class="dropdownmenu" data-toggle="modal" data-target="#northwichModal"><a >Northwich</a></li>
<li class="dropdownmenu" data-toggle="modal" data-target="#middlewichModal">Middlewich</li>
<li class="dropdownmenu" data-toggle="modal" data-target="#winsfordModal">Winsford</li>
<li class="dropdownmenu" data-toggle="modal" data-target="#lettingsModal">Northwich Lettings</li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>

How to stick notification icon with menu toggle on small screens and right on large screens?

I'm trying to stick notification submenu to toggle menu in small screens and stick to right on small screens like the following
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<nav role="navigation" class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" data-toggle="collapse" data-target=".navbar-collapse" class="navbar-toggle collapsed pull-left">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>Home</li>
<li>Abount</li>
<li>Contact</li>
</ul>
</div>
</div>
<ul class="nav">
<li class="dropdown dropdown-list">
<a href="javascript:;" data-toggle="dropdown" dropdown-animate="" aria-haspopup="true" class="dropdown-toggle">
<i class="glyphicon glyphicon-bell"></i>
<span class="label label-danger">11</span>
</a>
</li>
</ul>
</nav>
But unfortunately it does not work. Please help me how could I do it?
To keep the notification always visible, you could add them to the nav-header like this:
<div class="navbar-header">
<button class="navbar-toggle collapsed">...</button>
<div class="nav navbar-brand pull-right">
<a href="#" >
<i class="glyphicon glyphicon-bell"></i>
<span class="label label-danger">11</span>
</a>
</div>
</div>
.notifications.navbar-brand {
font-size: 14px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" >
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse"
data-target=".navbar-collapse" >
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="nav navbar-brand pull-right notifications">
<a href="#" >
<i class="glyphicon glyphicon-bell"></i>
<span class="label label-danger">11</span>
</a>
</div>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>Home</li>
<li>Abount</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>

js doesnt recognize click on navbar collapse button

I have problem, with navbar toggle in twitter bootstrap 3 on small screens (when collapse button appears),
everything looks fine i think, when i execute my code on jsfiddle collapse button works, but in my asp net mvc project menu dropdown doesnt appear when i click on button,
heres code:
<nav class="navbar navbar-default scrollMenu" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href=""></a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse" id="scrollTarget">
<ul class="nav navbar-nav pull-right">
<li class="active"><i class="fa fa-home"></i>1</li>
<li class=""><i class="glyphicon glyphicon-star">2</i> </li>
<li class=""><i class="glyphicon glyphicon-stats">3</i> </li>
<li class=""><i class="glyphicon glyphicon-ok"></i>4 </li>
<li class=""><i class="fa fa-bullhorn fa-1"></i>5</li>
<li class=""><i class="glyphicon glyphicon-globe"></i>6 </li>
</ul>
</div>
</nav>

Categories

Resources