I want to make a smooth scroll to the anchor which will be activated not by clicking, but just by an event scroll.
Moving the scroll itself will cause the scroll to go down to the section, and moving the scroll up will give the same return effect to the first section.
I'm trying to do it in jQuery, but I miss something. I manage to pull down to the bottom section, but the function blocks me. Sure there is some simple tip for it, but I do not know what to look for.
function transitionScroll() {
if ($(window).width() <= 767) {
let scrollWatch = window.pageYOffset;
let positionOfElement = $("#small-carousel").offset().top;
if ((scrollWatch <= positionOfElement) && (scrollWatch != 0)) {
$([document.documentElement, document.body]).animate({ scrollTop: $("#small-carousel").offset().top }, 1000);
}
}
$(window).resize(function () {
if ($(window).width() <= 767) {
let scrollWatch = window.pageYOffset;
let positionOfElement = $("#small-carousel").offset().top;
if (scrollWatch <= positionOfElement) {
$([document.documentElement, document.body]).animate({ scrollTop: $("#small-carousel").offset().top }, 1000);
}
}
});
};
transitionScroll();
window.addEventListener('scroll', transitionScroll);