在div之间平滑向下滚动

时间:2017-05-25 06:58:13

标签: jquery scroll

不知道是否有人可以帮助我,我试图建立一个具有平滑效果的向下滚动,这是我不会做的效果:https://studiorotate.com/case-study/seedlip < / p>

1 个答案:

答案 0 :(得分:1)

查看此tutorial by w3schools.com

您需要做什么: 将ID添加到页面的各个部分:

<a href="#section2">Click Me to Smooth Scroll to Section 2 Below</a>

<div class="main">
  <section></section>
</div>

<div class="main" id="section2">
  <section style="background-color:blue"></section>
</div>

然后添加jquery文件以及来自w3schools的脚本,如下所示:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>

$(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
      }, 800, function(){

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

然后点击<a href="#section2"></a>标记,您就可以顺利滚动到#section2

这里有一个jsfiddle