gpt4 book ai didi

jQuery animate scrolltop 它不工作

转载 作者:行者123 更新时间:2023-11-28 02:50:11 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com