首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery动画滚动顶--它不工作

问jQuery动画滚动顶--它不工作
EN

Stack Overflow用户
提问于 2017-10-12 01:19:40
回答 2查看 1.6K关注 0票数 0

我试图使用jQuery动画滚动顶滚动效果,但它不起作用,我不知道为什么。

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

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

代码语言:javascript
复制
 <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> 

然后,我将每个部分标记为相应的类,在其中,我希望卷轴按如下方式登陆

代码语言:javascript
复制
 <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代码如下

代码语言:javascript
复制
/* 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);    
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-12 05:55:24

您必须将整个jQuery代码包装在document.ready函数中。那么平滑的滚动效果就会起作用。希望能帮到你。这是整个密码。

​

代码语言:javascript
复制
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);    
  });
});
代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2017-10-12 01:34:51

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

代码语言:javascript
复制
$('.js--scroll-to-about_us').click(function(){ 
//alert("alert here to see if the link is making contact");
$('#section-about_us').slideDown(1000);
});

看看能不能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46699912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档