jQuery animate scrolltop 它不工作

标签 jquery html css jquery-animate

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

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

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

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

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

最佳答案

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

关于jQuery animate scrolltop 它不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46699912/

相关文章:

c# - colorbox + asp.net 按钮不会回发

javascript - 在 Angular Material 中选择 mat-option 时调用 ngmodelchange

javascript - 如何更改特定标签的内容?

html - 无序列表元素内容器 div 的垂直对齐

css - 是否可以将具有相同根的 CSS 选择器聚集在一起?

javascript - 在 DOM 中双击了哪个 HTML 元素

javascript - codemirror 选定的文本 css 不起作用(div 背景颜色不显示在文本区域中)

javascript - 实现 Jquery 日期时间选择器

html - 在以下场景中,如何在 Bootstrap 框架中并排设置图标图像和文本标题?

javascript - 折叠菜单,高度问题