[英]JQuery Scroll to Next Section
有人可以幫忙嗎?
我似乎有兩個問題:
.hero-banner
是全屏幻燈片。 單擊.trigger-scroll
,頁面需要滾動到sections.scroll-here。 到達最后一節時,需要滾動到頂部。
預先感謝您的回答。
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:
$(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++;
});
});
});
在試圖弄清楚為什么<scrollTop>
不能正常工作幾個小時后,我發現<body>
標記的CSS樣式為<height:100%;>
,這弄亂了<offset>
。 亞歷克斯感謝您的幫助。 對於其他人,這是一個有效的JSFiddle鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.