I have a single page navigation site and all sections/navigation elements have min-height
set to 100vh
. With that I have a smooth scroll snippet to navigate.
If I manual scroll and the section is not centered (like it would if I click on a menu item) I'd like it to be centered after a given amount of time. Note I don't want to disable scroll to just navigate via menu.
I was thinking about some js to be added with the smoothscroll code. Something to check section position offset and if it is not centered, smoothscroll it with some easing function.
https://jsfiddle.net/9ta3yh52/ take this as a reference, If a color is in more than 75% of view port, scroll to that element.
Thx for your help :)
EDIT / SOLUTION:
The closest approach has been given so far by @Hastig Zusammenstellen
https://stackoverflow.com/a/39188110/6717849
You have to modify it to your needs in order to match the number of sections you have. The logic is easy when sections are set to height: 100vh
:
if (scrollTop <= sectionHeightHalf) {
$('html, body').animate({
scrollTop: $("#section1").offset().top
}, 300);
} else if (scrollTop >= sectionHeightHalf && scrollTop <= ($(window).height() * 2 - sectionHeightHalf)) {
$('html, body').animate({
scrollTop: $("#section2").offset().top
}, 300);
} else if (scrollTop >= sectionHeightHalf && scrollTop <= ($(window).height() * 3 - sectionHeightHalf)) {
$('html, body').animate({
scrollTop: $("#section3").offset().top
}, 300);
} else if (scrollTop >= sectionHeightHalf && scrollTop <= ($(window).height() * 4 - sectionHeightHalf)) {
$('html, body').animate({
scrollTop: $("#section4").offset().top
}, 300);
} else if (scrollTop >= sectionHeightHalf && scrollTop <= ($(window).height() * 5 - sectionHeightHalf)) {
$('html, body').animate({
scrollTop: $("#section5").offset().top
}, 300);
// etc etc
} else if (scrollTop >= ($(window).height() * 2 - sectionHeightHalf)) {
$('html, body').animate({
scrollTop: $("#lastsection").offset().top
}, 300);
}