2017-10-12 118 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