这是一个完全响应、新手友好、易于使用的滚动导航模板,专为单页滚动网站设计。
1.在Bootstrap 5(或4)项目中加载必要的资源。
<!-- Required --> <link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" /> <script src="/path/to/cdn/bootstrap.bundle.min.js"></script> <!-- Required For Bootstrap 4 --> <script src="/path/to/cdn/jquery.min.js"></script> <script src="/path/to/cdn/anime.min.js"></script>
2.在页面上创建一个包含锚链接的Bootstrap导航栏。
<!-- Bootstrap 5 --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top" id="mainNav"> <div class="container px-4"> <a class="navbar-brand" href="#page-top">Start Bootstrap</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ms-auto"> <li class="nav-item"><a class="nav-link" href="#about">About</a></li> <li class="nav-item"><a class="nav-link" href="#services">Services</a></li> <li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li> </ul> </div> </div> </nav> <!-- Bootstrap 4 --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top" id="mainNav"> <div class="container"> <a class="navbar-brand js-scroll-trigger" href="#page-top">Start Bootstrap</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#about">About</a></li> <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#services">Services</a></li> <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#contact">Contact</a></li> </ul> </div> </div> </nav>
3.用于激活滚动导航的主脚本。
/* * Bootstrap 5 */ window.addEventListener('DOMContentLoaded', event => { // Activate Bootstrap scrollspy on the main nav element const mainNav = document.body.querySelector('#mainNav'); if (mainNav) { new bootstrap.ScrollSpy(document.body, { target: '#mainNav', offset: 74, }); }; // Collapse responsive navbar when toggler is visible const navbarToggler = document.body.querySelector('.navbar-toggler'); const responsiveNavItems = [].slice.call( document.querySelectorAll('#navbarResponsive .nav-link') ); responsiveNavItems.map(function (responsiveNavItem) { responsiveNavItem.addEventListener('click', () => { if (window.getComputedStyle(navbarToggler).display !== 'none') { navbarToggler.click(); } }); }); }); /* * Bootstrap 4 */ (function ($) { // Smooth scrolling using anime.js $('a.js-scroll-trigger[href*="#"]:not([href="#"])').on('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) { anime({ targets: 'html, body', scrollTop: target.offset().top - 56, duration: 1000, easing: 'easeInOutExpo' }); return false; } } }); // Closes responsive menu when a scroll trigger link is clicked $(".js-scroll-trigger").on('click', function () { $(".navbar-collapse").collapse("hide"); }); // Activate scrollspy to add active class to navbar items on scroll $("body").scrollspy({ target: "#mainNav", offset: 56, }); })(jQuery);
v5.0.5版本(2022-03-24)