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.
Here's an enhanced approach to scroll animation with multiple improvements:
function smoothScrollToTop(duration = 800) {
const start = window.pageYOffset;
const startTime = performance.now();
function easeInOutQuad(t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}
function animation(currentTime) {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
const easedProgress = easeInOutQuad(progress);
window.scrollTo(0, start * (1 - easedProgress));
if (elapsed < duration) {
requestAnimationFrame(animation);
}
}
requestAnimationFrame(animation);
}
class ScrollAnimationController {
constructor(selector, options = {}) {
this.element = document.querySelector(selector);
this.options = {
scrollDuration: 800,
scrollDelay: 100,
...options
};
this.init();
}
init() {
if (!this.element) return;
this.element.addEventListener('animationend', this.handleAnimationEnd.bind(this));
}
handleAnimationEnd() {
// Optional: Add a slight delay to ensure animation is fully complete
setTimeout(() => {
smoothScrollToTop(this.options.scrollDuration);
// Optional: Additional actions after scroll
this.postScrollActions();
}, this.options.scrollDelay);
}
postScrollActions() {
// You can add additional actions here, like:
// - Removing temporary classes
// - Triggering next animation
// - Updating UI state
this.element.classList.add('animation-completed');
}
}
.animation-container {
perspective: 1000px;
overflow: hidden;
position: relative;
width: 100%;
min-height: 100vh;
}
.animated-element {
transform-style: preserve-3d;
animation: unfold 1s cubic-bezier(0.25, 0.1, 0.25, 1) forwards;
opacity: 0;
transform:
perspective(1000px)
rotateX(90deg)
translateZ(-100px);
}
@keyframes unfold {
0% {
opacity: 0;
transform:
perspective(1000px)
rotateX(90deg)
translateZ(-100px);
}
70% {
opacity: 0.7;
transform:
perspective(1000px)
rotateX(-10deg)
translateZ(50px);
}
100% {
opacity: 1;
transform:
perspective(1000px)
rotateX(0deg)
translateZ(0);
}
}
<!DOCTYPE html>
<html>
<head>
<title>Enhanced Scroll Animation</title>
<style>
/* CSS from previous example */
</style>
</head>
<body>
<div class="animation-container">
<div class="animated-element">
Animated Content
</div>
</div>
<script>
// Paste the JavaScript functions from above
// Initialize the scroll animation controller
document.addEventListener('DOMContentLoaded', () => {
new ScrollAnimationController('.animated-element', {
scrollDuration: 1000,
scrollDelay: 200
});
});
</script>
</body>
</html>
Performance Optimization:
requestAnimationFrame for smoother animationsFlexibility:
postScrollActions methodEnhanced CSS Animation:
Robust Event Handling:
// Example of more advanced configuration
new ScrollAnimationController('.animated-element', {
scrollDuration: 1200, // Scroll animation duration
scrollDelay: 300, // Delay before scrolling
easing: 'easeInOutCubic', // Custom easing type
onScrollComplete: () => {
// Custom callback when scroll is complete
console.log('Scroll finished');
}
});
will-change: transform, opacity; in CSS for better renderingThe provided code uses modern JavaScript features. For older browsers, you might need to:
requestAnimationFrameThis approach provides a robust, performant, and customizable solution for scroll animations with 3D transformations.