Advertisement
danrim

Scrollspy + jquery

Oct 24th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.74 KB | None | 0 0
  1. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  2.  
  3. <!-- add to the body tag scroll spy AND main-nav id to nav bar AND the id tags to the links AND those id's to the section -->
  4.  
  5. <body id="home" data-spy="scroll" data-target="#main-nav">
  6.   <nav class="navbar navbar-expand-md navbar-light fixed-top py-4" id="main-nav">
  7.     <div class="container">
  8.       <a href="#home" class="navbar-brand">
  9.         <img src="img/mlogo.png" width="50" height="50" alt="logo">
  10.         <h3 class="d-inline align-middle">Bootstrap-Sass</h3>
  11.       </a>
  12.       <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
  13.         <span class="navbar-toggler-icon"></span>
  14.       </button>
  15.       <div class="collapse navbar-collapse" id="navbarCollapse">
  16.         <ul class="navbar-nav ml-auto"><!-- ml-auto nustumia li i desine puse -->
  17.           <li class="nav-item">
  18.             <a href="#home" class="nav-link">Home</a>
  19.           </li>
  20.           <li class="nav-item">
  21.             <a href="#about" class="nav-link">About</a>
  22.           </li>
  23.           <li class="nav-item">
  24.             <a href="#authors" class="nav-link">Meet The Authors</a>
  25.           </li>
  26.           <li class="nav-item">
  27.             <a href="#contact" class="nav-link">Contact</a>
  28.           </li>
  29.         </ul>
  30.       </div><!-- end og collapse navbar-collapse -->
  31.     </div><!-- end of container -->
  32.   </nav>
  33.  
  34. <section id="about" class="py-5 text-center bg-light"> ....
  35.  
  36.  
  37. <!-- under the body -->
  38.  
  39.   <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  40.  crossorigin="anonymous"></script>
  41.   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
  42.  crossorigin="anonymous"></script>
  43.   <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T"
  44.  crossorigin="anonymous"></script>
  45.  
  46.  
  47.   <script>
  48.     // Get the current year for the copyright
  49.     $('#year').text(new Date().getFullYear());
  50.  
  51.     // Init Scrollspy
  52.     $('body').scrollspy({target: '#main-nav'});
  53.  
  54.     // Smooth Scrolling
  55.     $("#main-nav a").on('click', function (event) {
  56.       if (this.hash !== "") {
  57.         event.preventDefault();
  58.  
  59.         const hash = this.hash;
  60.  
  61.         $('html, body').animate({
  62.           scrollTop: $(hash).offset().top
  63.         }, 800, function () {
  64.  
  65.           window.location.hash = hash;
  66.         });
  67.       }
  68.     });
  69.  
  70.   </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement