2017-10-12 152 views
0

我想使用jQuery動畫scrolltop的滾動效果,但它不工作,我不明白爲什麼。jQuery animate scrolltop它不工作

我的整個代碼可以在這裏找到... https://codepen.io/andresq820/project/editor/Aogyqr

我的導航欄有我接着讓每個部分上貼有以下鏈接和每個js--

<div class="menu-section"> 
    <ul class="main-nav js--main-nav"> 
     <li class="js--scroll-to-main"> <a href="#header">Inicio</a> </li> 
     <li class="js--scroll-to-about_us"> <a href="#section-about_us">Nosotros</a> </li> 
     <li class="js--scroll-to-services"> <a href="#section-services">Servicios</a> </li> 
     <li class="contact-link js--scroll-to-contact_us"> <a href="#section-contact" class="separator">Contactanos</a> </li> 
    </ul>  
</div> 

開始了jQuery類各階級,我想滾動土地如下

<section class="section-about_us js--section-about_us" id="section-about_us"> 

<section class="section-services js--section-services" id="section-services"> 

<section class="section-contact js--section-contact" id="section-contact"> 

我的jQuery代碼低於

/* scroll buttons/links */ 
$('.js--scroll-to-main').click(function(){ 
    $('html, body').animate({scrollTop: $('header').offset().top}, 1000);  
}); 

$('.js--scroll-to-about_us').click(function(){ 
    $('html, body').animate({scrollTop: $('.js--section-about_us').offset().top}, 1000);  
}); 

$('.js--scroll-to-services').click(function(){ 
    $('html, body').animate({scrollTop: $('.js--section-services').offset().top}, 1000);  
}); 

$('.js--scroll-to-contact_us').click(function(){ 
    $('html, body').animate({scrollTop: $('.js--section-contact').offset().top}, 1000);  
}); 
+0

你在你的問題包括代碼工作正常,那麼問題是另一回事。您的codepen示例在控制檯中充滿了錯誤,因此很難說出發生了什麼問題,但是如果您在實際代碼中收到任何javascript錯誤,它將阻止任何進一步的javascript工作。 – FluffyKitten

回答

0

你必須將整個jQuery代碼包裝在document.ready函數中。然後,平滑的滾動效果將起作用。希望它會幫助你。這是整個代碼。

jQuery(document).ready(function(){ 
 
    /* scroll buttons/links */ 
 
    $('.js--scroll-to-main').click(function(){ 
 
     $('html, body').animate({scrollTop: $('header').offset().top}, 1000);  
 
    }); 
 

 
    $('.js--scroll-to-about_us').click(function(){ 
 
     
 
     $('html, body').animate({scrollTop: $('.js--section-about_us').offset().top}, 1000);  
 
    }); 
 

 
    $('.js--scroll-to-services').click(function(){ 
 
     
 
     $('html, body').animate({scrollTop: $('.js--section-services').offset().top}, 1000);  
 
    }); 
 

 
    $('.js--scroll-to-contact_us').click(function(){ 
 
     
 
     $('html, body').animate({scrollTop: $('.js--section-contact').offset().top}, 1000);  
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="menu-section"> 
 
    <ul class="main-nav js--main-nav"> 
 
     <li class="js--scroll-to-main"> <a href="#header">Inicio</a> </li> 
 
     <li class="js--scroll-to-about_us"> <a href="#section-about_us">Nosotros</a> </li> 
 
     <li class="js--scroll-to-services"> <a href="#section-services">Servicios</a> </li> 
 
     <li class="contact-link js--scroll-to-contact_us"> <a href="#section-contact" class="separator">Contactanos</a> </li> 
 
    </ul>  
 
</div> 
 
    
 
<section class="section-about_us js--section-about_us" id="section-about_us">I then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as follows</section> 
 
<br> 
 
<br> 
 
<br> 
 
<br> 
 
<section class="section-services js--section-services" id="section-services">I then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as follows</section> 
 
<br> 
 
<br> 
 
<br> 
 
<br> 
 
<section class="section-contact js--section-contact" id="section-contact">I then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as followsI then have each section labeled with the respective class where I want the scroll to land as follows</section>

0

首先,該行<li class="js--scroll-to-main"> <a href="#header">Inicio</a> </li>沒有部分ID。讓我以第二個爲例,然後根據自己的用法進行調整。 嘗試

$('.js--scroll-to-about_us').click(function(){ 
//alert("alert here to see if the link is making contact"); 
$('#section-about_us').slideDown(1000); 
}); 

看看是否有效。

+0

問題中的代碼已經正常工作,所以這是不必要的,並且不會幫助解決實際問題。 – FluffyKitten