2017-05-25 139 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