[英]JQuery Scroll to Next Section
Can someone please help? 有人可以帮忙吗?
I seem to have two issues: 我似乎有两个问题:
.hero-banner
is a full screen slideshow. .hero-banner
是全屏幻灯片。 When .trigger-scroll
is clicked, page needs to scroll to sections.scroll-here. 单击
.trigger-scroll
,页面需要滚动到sections.scroll-here。 When reached last section needs to scroll to top. 到达最后一节时,需要滚动到顶部。
Thanks in advance for your answers. 预先感谢您的回答。
HTML: HTML:
<section class="hero-banner">full screen section</section>
<div class="trigger-scroll"></div>
<section class="scroll-here" id="first-section">some content</section>
<section class="scroll-here" id="second-section">some content</section>
<section class="scroll-here" id="third-section">some content</section>
JS: JS:
$(document).ready(function() {
$('.page-scroller', function() {
var $scrollSection = $('.scroll-here')
var $scrollTrigger = $('.trigger-scroll')
var nextSection = 0;
$scrollTrigger.on( 'click', function() {
$(this).removeClass('go-to-top');
// If at last section, scroll back to top on next click:
if (nextSection >= $scrollSection.length) {
$('html, body').animate({ scrollTop: 0 }, 1000);
nextSection = 0;
return;
}
// If you've already scrolled down and then click button, run a check
// to find next section position so you don't go backwards:
while ( $('body').scrollTop() > $($scrollSection[nextSection]).offset().top ) {
nextSection++;
}
// If next section is the last, add class to rotate arrow graphic:
if (nextSection === ($scrollSection.length - 1)) {
$(this).addClass('go-to-top');
}
// Move to next section and increment counter check:
$( 'html, body' ).animate({ scrollTop: $($scrollSection[nextSection]).offset().top }, 1000);
nextSection++;
});
});
});
After a few hours trying to figure out why <scrollTop>
wasn't working, I found that <body>
tag had a css style of <height:100%;>
which was messing up the <offset>
. 在试图弄清楚为什么
<scrollTop>
不能正常工作几个小时后,我发现<body>
标记的CSS样式为<height:100%;>
,这弄乱了<offset>
。 Alex thanks for your help. 亚历克斯感谢您的帮助。 For others, here is a working JSFiddle Link
对于其他人,这是一个有效的JSFiddle链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.