Jquery - 平滑滚动偏移

时间:2018-03-02 23:08:58

标签: javascript jquery offset smooth-scrolling

我有以下js,但偏移值(-140)似乎不起作用。它识别跳转到浏览器顶部的偏移量。

请帮忙:)



// smooth scroll
$(document).ready(function(){
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // 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 - 140
      }, 500, function(){
        

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="header"> 
   <canvas class="fireworks"></canvas> 
   <h1 class="logo"><a href="../index.html"><img src="images/quantum-logo.svg"></a></h1> 
   <ul class="menu"> 
      <div class="menu__item toggle"><span></span></div> 
      <li class="menu__item"><a href="#overview">Overview</a></li> 
      <li class="menu__item"><a href="#structre">Structre</a></li> 
      <li class="menu__item"><a href="#timing-easing">Timing & Easing</a></li> 
      <li class="menu__item"><a href="#tension-friction">Tension & Friction</a></li> 
   </ul> 
</nav>
&#13;
&#13;
&#13;

0 个答案:

没有答案