我试图使用jQuery动画滚动顶滚动效果,但它不起作用,我不知道为什么。
我的全部密码都可以在这里找到..。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);
});发布于 2017-10-12 05:55:24
您必须将整个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>
发布于 2017-10-12 01:34:51
首先,这一行<li class="js--scroll-to-main"> <a href="#header">Inicio</a> </li>没有分区id。让我以第二个为例,然后将其调整为您自己的用法。试一试
$('.js--scroll-to-about_us').click(function(){
//alert("alert here to see if the link is making contact");
$('#section-about_us').slideDown(1000);
});看看能不能。
https://stackoverflow.com/questions/46699912
复制相似问题