【问题标题】:jQuery animate scrolltop it's not workingjQuery动画滚动顶部它不工作
【发布时间】:2017-10-12 01:19:40
【问题描述】:

我正在尝试使用 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);    
});

【问题讨论】:

  • 您在问题中包含的代码可以正常工作,所以问题是别的。您的 codepen 示例在控制台中充满了错误,因此很难判断出了什么问题,但是如果您在实际代码中遇到任何 javascript 错误,它将阻止任何进一步的 javascript 工作。

标签: jquery html css jquery-animate


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    首先,这一行&lt;li class="js--scroll-to-main"&gt; &lt;a href="#header"&gt;Inicio&lt;/a&gt; &lt;/li&gt; 没有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);
    });
    

    看看这是否有效。

    【讨论】:

    • 问题中的代码已经可以工作,所以这是不必要的,对实际问题没有帮助。
    猜你喜欢
    • 2012-03-03
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多