jQuery 动画 scrolltop 它不工作

jQuery animate scrolltop it's not working

我正在尝试使用 jQuery animate scrolltop 来使用滚动效果,但它不起作用,我也不知道为什么。

我的全部代码可以在这里找到...https://codepen.io/andresq820/project/editor/Aogyqr

我的导航栏有以下链接和每个以 js 开头的 jQuery class--

 <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> 

然后我让每个部分都标有相应的 class 我希望卷轴着陆的位置

 <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);    
});

首先,这一行<li class="js--scroll-to-main"> <a href="#header">Inicio</a> </li>没有section id。让我以第二个为例,然后将其调整为您自己的用法。 尝试

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

看看是否可行。

您必须将整个 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>