引导滚动间谍不起作用,导航菜单按钮导致标题重新加载

Bootstrap scrollspy not working and nav menu buttons are causing the header to reload

本文关键字:新加载 标题 加载 按钮 间谍 滚动 不起作用 菜单 导航      更新时间:2023-09-26

基本上所有的锚点hrefs都可以工作,除了滚动间谍不工作。数据偏移也不起作用。单击时,只有第一个li"主页"正在更改其背景,所有其他内容都不起作用。向下滚动到#search-nav #obqvi #about等与它们相关的菜单按钮也没有变色,这意味着我的 scrollspy 根本不起作用。

此外,当我单击其中一个锚点时,整个标题菜单按钮会重新加载,它会发生大约 0.1 秒,但它很丑陋。

http://getbootstrap.com/javascript/#scrollspy 这是IM用作模板的链接,与我的代码相比,我看不到任何区别

.HTML:

    <body data-spy="scroll" data-target=".navbar" data-offset="0">
            <header>
                <nav class="navbar navbar-fixed-top">
                            <div class="container">
                                <div class="navbar-header">
                                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                                    <span class="icon-bar"></span>
                                    <span class="icon-bar"></span>
                                    <span class="icon-bar"></span> 
                                </button>
                                <a class="navbar-brand" href="#"><img id="logo-img" src="images/logo.png" alt="logo"></a>
                                </div>
                                <div class="collapse navbar-collapse" id="myNavbar">
                                <ul class="nav navbar-nav">
                                    <li><a href="#">Home</a></li>
                                    <li><a href="#search-nav"><span class="glyphicon glyphicon-search"></span> Search</a></li>
                                    <li><a href="#obqvi"><span class="glyphicon glyphicon-home"></span> Sales</a></li>
                                    <li><a href="#about"><span class="fa fa-cogs"></span> Services</a></li>
                                    <li><a href="#footer-contact-form"><span class="glyphicon glyphicon-comment"></span> Contact</a></li>
                                    <li><a href="#end-footer"><span class="glyphicon glyphicon-link"></span> About Us</a></li>
                                </ul>
                                </div>
                            </div>
                            </nav>
            </header>
....

使用 Jquery 脚本编辑,但当我单击"a"时它仍然不起作用,我可以平滑地滚动到没问题的位置但是当我使用滚轮滚动时,与该部分相关的按钮不会变色,并且在单击时也不会着色

.JS:

<script>
            $(document).ready(function(){
                $('.btn-information').click(function(){
                    $('#myModal').modal('show');
                });             
                 // Add scrollspy to <body>
            $('body').scrollspy({ target: ".navbar", offset: 50 });
            // Add smooth scrolling on all links inside the navbar
            $("#myNavbar a").on('click', function (event) {
                // Prevent default anchor click behavior
                event.preventDefault();
                // Store hash
                var hash = this.hash;
                // Using jQuery's animate() method to add smooth page scroll
                // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
                $('html, body').animate({
                    scrollTop: $(hash).offset().top
                }, 800, function () {
                    // Add hash (#) to URL when done scrolling (default click behavior)
                    window.location.hash = hash;
                });
            });
            });
        </script>
<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <style>
        body {
            position: relative;
        }
        #search-nav {
            padding-top: 50px;
            height: 500px;
            color: #fff;
            background-color: #1E88E5;
        }
        #obqvi {
            padding-top: 50px;
            height: 500px;
            color: #fff;
            background-color: #673ab7;
        }
        #about {
            padding-top: 50px;
            height: 500px;
            color: #fff;
            background-color: #ff9800;
        }
        #footer-contact-form {
            padding-top: 50px;
            height: 500px;
            color: #fff;
            background-color: #ff9800;
        }
        #end-footer {
            padding-top: 50px;
            height: 500px;
            color: #fff;
            background-color: #ff9800;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">WebSiteName</a>
            </div>
            <div>
                <div class="collapse navbar-collapse" id="myNavbar">
                    <ul class="nav navbar-nav">
                        <li><a href="#">Home</a></li>
                        <li><a href="#search-nav"><span class="glyphicon glyphicon-search"></span> Search</a></li>
                        <li><a href="#obqvi"><span class="glyphicon glyphicon-home"></span> Sales</a></li>
                        <li><a href="#about"><span class="fa fa-cogs"></span> Services</a></li>
                        <li><a href="#footer-contact-form"><span class="glyphicon glyphicon-comment"></span> Contact</a></li>
                        <li><a href="#end-footer"><span class="glyphicon glyphicon-link"></span> About Us</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>
    <div id="search-nav" class="container-fluid">
        <h1>Section 1</h1>
        <p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
    </div>
    <div id="obqvi" class="container-fluid">
        <h1>Section 2</h1>
        <p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
    </div>
    <div id="about" class="container-fluid">
        <h1>Section 3</h1>
        <p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
    </div>
    <div id="footer-contact-form" class="container-fluid">
        <h1>Section 3</h1>
        <p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
    </div>
    <div id="end-footer" class="container-fluid">
        <h1>Section 3</h1>
        <p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
    </div>
    <script>
        $(document).ready(function () {
            // Add scrollspy to <body>
            $('body').scrollspy({ target: ".navbar", offset: 50 });
            // Add smooth scrolling on all links inside the navbar
            $("#myNavbar a").on('click', function (event) {
                // Prevent default anchor click behavior
                event.preventDefault();
                // Store hash
                var hash = this.hash;
                // Using jQuery's animate() method to add smooth page scroll
                // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
                $('html, body').animate({
                    scrollTop: $(hash).offset().top
                }, 800, function () {
                    // Add hash (#) to URL when done scrolling (default click behavior)
                    window.location.hash = hash;
                });
            });
        });
    </script>
</body>
</html>
我不知道

它是否有帮助,但是当我使用滚动间谍功能时,我不得不在我的js中包含以下内容。另外 - 检查您是否具有滚动间谍工作所需的文件,并且 jquery 是否已加载并首先调用到页面中。

// jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1500, 'easeInOutExpo');
        event.preventDefault();
    });
});
// Highlight the top nav as scrolling occurs
$('body').scrollspy({
    target: '.navbar-fixed-top'
})