2017-05-25 143 views
1

之间上下不知道是否有人能帮助我,我试图建立了连接向下滚动平滑效果porfolio,这是我不会做的效果:https://studiorotate.com/case-study/seedlip平滑滚动的div

+0

你有没有考虑使用一个库,如[fullPage.js(https://alvarotrigo.com/fullPage/)。这将节省您大量的关于浏览器的兼容性,触摸刷卡,响应性,回调头痛的... – Alvaro

回答

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> 

然后用脚本一起从W3Schools的添加jQuery的文件,像这样:

<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