javascript - Bootstrap 默认导航栏无法正常工作

标签 javascript html css

Bootstrap DEFAULT-navbar 跳转到我部分#point 下的几个像素,无论如何 AFFIX-navbar 工作得很好。

您可以在这里查看:http://kresyproduction.cz/

问题是当我点击默认导航栏中的任何链接时,它不会以正确的方式滚动。就像你在我的页面上看到的一样。该部分下方的像素很少,它隐藏了一些内容。但是当你稍微滚动一下,默认的导航栏更改为附加链接时,效果很好。

<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
    <title>Kresy Production</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="img/favicon.png">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <script src="js/jquery-1.12.4.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.0.4/popper.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js">
</script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js">
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
&& location.hostname == this.hostname) {
  var target = $(this.hash);
  target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
  if (target.length) {
    $('html,body').animate({
      scrollTop: target.offset().top
    }, 1000);
    return false;
  }
}
});
});
</script>

</head>
<body>
<nav class="navbar navbar navbar-default navbar-static-top" data-spy="affix" data-offset-top="50">
<div class="container">
<!-- 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" 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>
            <a class="navbar-brand" href="#domu"><img src="img/kp_white.png" alt="logo" title="Kresy Production" style="width auto;height: 30px;"></a>
            </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 navbar-right">
                <li><a href="#domu">Domů</a></li>
                <li><a href="#sluzby">Služby</a></li>
                <li><a href="#showreel">Showreel</a></li>
                <li><a href="#onas">O nás</a></li>
                <li><a href="#kontakt">Kontakt</a></li>
            </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
</div>
.
.
</body>

enter image description here

enter image description here

最佳答案

您的导航栏具有 data-offset-top 值。这就是导致问题的原因。改变这个

    <nav class="navbar navbar-default nav-fixed-top" data-spy="affix" data-offset-top="50">

为此-

    <nav class="navbar navbar-default nav-fixed-top" data-spy="affix" >

希望这有助于...

编辑:1 不删除 data-offset-top 值,将其编辑为此

    <nav class="navbar navbar-default navbar-fixed-top" data-spy="affix" data-offset-top="xx">  -- xx could be the data offset that works for you.

关于javascript - Bootstrap 默认导航栏无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48681770/

相关文章:

javascript - 设置 css 属性的最快方法?

javascript - 鼠标悬停工具提示在 JQuery Accordion 中不起作用

javascript - knockout : How to run script after inserting data in observable array

jquery - 将鼠标悬停在区域上时图像映射中的错误

html - 将 <select> <option> 的文本右对齐

php - 链接 CSS 根据 URL 变化并保持子 URL 的样式

javascript - 有没有一种方法可以在不使用SQL的情况下用PHP在页面之间交换数据?

html - 更改 ios 上多选输入的默认文本

html - XPath 适用于 HTML5 格式吗?

css - 在同一浏览器中刷新时页面呈现不同