twitter-bootstrap 使用 .hide() 隐藏导航栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29242074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:28:40  来源:igfitidea点击:

Hiding a navigation bar with .hide()

javascriptjqueryhtmlcsstwitter-bootstrap

提问by Ryan

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.

我搜索并搜索试图找出我的代码有什么问题。我想在某个屏幕宽度处完全隐藏导航栏,但我什么也没得到。JS fiddle 和 Code Pen 找不到任何东西,safari 也没有显示语法错误。我只在这里待了一个月,在这里我感谢任何帮助。

<!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;

回答by AndrewL64

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;
    }
}

回答by David

You are attempting to hide a child with the class .navbar-defaultof your nav, when you are actually wanting to target the nav itself:

.navbar-default当您实际上想要定位导航本身时,您正试图用您的导航类隐藏一个孩子:

<nav class="navbar-default">

This is targeted as:

其目标是:

nav.navbar-default

not:

不是:

nav .navbar-default

That one space character is extremely important here.

那个空格符在这里极其重要。

回答by TbgUser

I have found two ways of finding the width of the user's screen in JS:

我找到了两种在 JS 中查找用户屏幕宽度的方法:

  1. screen.width

  2. window.innderWidth

  1. 屏幕宽度

  2. window.inderWidth

I hope they may be of assistance to you.

我希望他们能对你有所帮助。