Improve the scroll animation
- User
Improve the scroll animation
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Improve the scroll animation
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You can enhance the scroll animation for a smoother and more customizable experience by either relying on native CSS smooth scrolling (if browser support is sufficient) or by implementing a custom JavaScript function with your own easing function. Below are two approaches:
If the native smooth scroll is acceptable, ensure your HTML tag uses the CSS property:
html {
scroll-behavior: smooth;
}
Then your existing JavaScript code becomes:
const animatedElement = document.querySelector('.animated-element');
animatedElement.addEventListener('animationend', () => {
window.scrollTo({ top: 0 });
});
Because the browser handles the smooth animation (assuming support in the target browsers), this solution is simple and effective.
If you need more control over the easing function or timing, you can implement a custom scroll function. For example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Smooth Scroll</title>
<style>
html {
/* Fallback: could also add smooth behavior here */
scroll-behavior: smooth;
}
body {
height: 200vh; /* To allow scroll */
margin: 0;
padding: 0;
}
.animation-container {
overflow: hidden;
position: relative;
width: 100%;
height: 100vh;
}
.animated-element {
transform-style: preserve-3d;
animation: unfold 1s ease-out forwards;
width: 200px;
height: 200px;
background-color: lightblue;
margin: 100px auto;
}
@keyframes unfold {
0% {
transform: rotateX(90deg);
opacity: 0;
}
100% {
transform: rotateX(0deg);
opacity: 1;
}
}
</style>
</head>
<body>
<div class="animation-container">
<div class="animated-element">Animated Element</div>
</div>
<script>
// Custom smooth scroll to top function
function smoothScrollToTop(duration) {
const startY = window.pageYOffset;
const startTime = performance.now();
// Custom easing function: easeOutQuad
function easeOutQuad(t) {
return t * (2 - t);
}
function scrollStep(currentTime) {
// Calculate progress (0 to 1)
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
const ease = easeOutQuad(progress);
// Calculate the new scroll position
window.scrollTo(0, startY * (1 - ease));
if (progress < 1) {
requestAnimationFrame(scrollStep);
}
}
requestAnimationFrame(scrollStep);
}
const animatedElement = document.querySelector('.animated-element');
animatedElement.addEventListener('animationend', () => {
// Scroll to top over 600 milliseconds
smoothScrollToTop(600);
});
</script>
</body>
</html>
Setup:
The function smoothScrollToTop(duration) gets the current Y offset (startY) and uses requestAnimationFrame to animate the scroll.
Easing:
The function easeOutQuad provides a smoother deceleration effect. You can replace it with any easing function you prefer.
Animation Loop:
The internal scrollStep function calculates the progress of the animation, applies the easing function, recalculates the scroll position, and continues until the animation is complete.
Trigger:
The event listener waits for the animation to end and then calls the custom scrolling function.
Choose the approach that best fits your requirements and browser support constraints. The custom JavaScript method gives you precise control over the timing and easing, while the native CSS solution is straightforward and leverages browser optimizations.