Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a navigation bar with .hide()

I have searched and searched trying to figure out what is wrong with my code. I would like to hide the navigation bar completely at a certain screen width but I am getting absolutely nothing. JS fiddle and Code Pen can't find anything and safari isn't showing syntax errors. I have only been at this a month, here it is I appreciate any help.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>No clue what I am doing</title>
    <link href="css/bootstrap.min.css" rel="stylesheet"/> 
    <link href="stylesheet.css" rel="stylesheet" type="text/css" />     
  </head>
  <body> 
 <nav class="navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <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>     
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">     
        <li><a href="#">Link</a></li>    
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav> 

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> 
    <script src="js/bootstrap.min.js"></script>
    <script src="java.js" type="text/javascript"></script>
  </body>
</html>

JAVASCRIPT

$(document).ready() 
var width = function(checkWidth) {
if(window.width>768) {
$("nav .navbar-default").hide()
} else {
$("nav .navbar-default").show()
}
};
width;
like image 793
Ryan Avatar asked Mar 17 '23 15:03

Ryan


1 Answers

You need to load the script on page load as well as on screen resize. This may help:

function toggleDiv(){

    if ($(window).width() < 768) {

            $("nav.navbar-default").hide();

    }else{

        $("nav.navbar-default").show();

    }

}

$(document).ready(function () {
    toggleDiv();

    $(window).resize(function(){
        toggleDiv();
    });

});

OR you can just use media queries like this:

@media (max-width: 768px) {
    .navbar-default {
        display: none !important;
    }
}
@media (min-width: 769px) {
    .navbar-default {
        display: block !important;
    }
}
like image 155
AndrewL64 Avatar answered Mar 26 '23 02:03

AndrewL64