I want to scroll 100vh in a web page. I want it to be as if a slide show. However if…
- User
I want to scroll 100vh in a web page. I want it to be as if a slide show. However if user scrolls twice in middle of scrolling the scroll will not be aligned and will get scrolled to unwanted location. Help me implement a thing for this so the scrolling will only, and always, stop at an element. Here is my code: " // Listen for the "wheel" event on the document and prevent the default behavior const section = document.querySelector(".scroll-section"); section.addEventListener( "wheel", (event) => { event.preventDefault();
// Check if the target element is a scroll section const scrollSection = section; // Determine the direction of the scroll const direction = event.deltaY > 0 ? 1 : -1; // Calculate the distance to the next scroll position (100vh) // Calculate the distance to the next scroll position (100vh) const scrollOffset = document.documentElement.scrollTop || document.body.scrollTop; const windowHeight = window.innerHeight; const nextScrollPosition = scrollOffset + direction * windowHeight; // Animate the scrolling with requestAnimationFrame() const start = window.pageYOffset; const distance = nextScrollPosition - start; const duration = 1000; // 1 second let startTime = null; function animate(currentTime) { if (startTime === null) { startTime = currentTime; } const elapsedTime = currentTime - startTime; const scrollPosition = easeInOut( elapsedTime, start, distance, duration ); window.scrollTo({ top: scrollPosition, behavior: "smooth" }); if (elapsedTime < duration) { requestAnimationFrame(animate); } } requestAnimationFrame(animate); // Add a shake effect at the end of the scroll setTimeout(() => { scrollSection.classList.add("shake"); setTimeout(() => { scrollSection.classList.remove("shake"); }, 500); }, 1500); // Easing function for smooth scrolling animation function easeInOut(t, b, c, d) { t /= d / 2; if (t < 1) return (c / 2) * t * t + b; t--; return (-c / 2) * (t * (t - 2) - 1) + b; } // Animate the scrolling with a delay // Determine the direction of the scroll // Calculate the distance to the next scroll position (100vh) const scrollTop = section.scrollTop; const scrollHeight = section.scrollHeight; const clientHeight = section.clientHeight; const scrollPosition = Math.round( (scrollTop + direction * clientHeight) / clientHeight ) * clientHeight; // const distance = scrollPosition - scrollTop; // Animate the scrolling with a delay }, { passive: false } );"
- Assistant
- User
I mean that is good but I don't want to lock scrolling. I want user to be able to cancel their scrolling by scrolling opposite direction mid way. Instead can we calculate distance in a way that it will account how much distance remained to the next element with a class, for example .scrolling-anchor class. So that scrolling will always stop at these anchors. Also in order to account for user scrolling just as the anchor is about to land, we can disable scrolling with scroll lock, however only just before getting to .scrolling-anchor class. I mean we can scroll to may be 10vh until the .scrolling-anchor class freely, with out a scrolling lock. Then the last 10vh is animated and scroll locked. This way user can cancel their scrolling with in the range of 90vh but the last 10vh is animated and scroll locked. Also scroll always lands on to a .scrolling-anchor class element. I am not sure what element to use, also should it have a height of its own or just be a line. Regardless if this element is a line or not just to indicate its position there might be a colorful and page wide, 5 or 10vh bar. Also I want to actually animate last 10vh. The animation I am thinking can be like slowing down, as if the brakes are hit.
- Assistant