Write a Android 12's overscroll animation to the container: <div class="container">…
- User
Write a Android 12's overscroll animation to the container:
- Assistant
Write a Android 12's overscroll animation to the container:
To create an Android 12-like overscroll animation in your web page, you can use CSS and JavaScript to simulate the stretchy effect when a user scrolls beyond the top or bottom of a scrollable container. Below is a step-by-step guide to implementing this effect on your container.
First, ensure you have your container with the content you want to make scrollable:
<div class="container">
<h1>Hello</h1>
<h1>Hello</h1>
<h1>Hello</h1>
<!-- Add as many items as needed -->
</div>
Apply styles to make the container scrollable and set up the initial state for the animation:
.container {
overflow-y: scroll; /* Enable vertical scrolling */
height: 400px; /* Fixed height to contain the scrollable area */
transition: transform 0.2s ease-out; /* Smooth transition for the stretchy effect */
}
.container h1 {
margin: 20px;
}
Use JavaScript to detect overscroll and apply a scaling transformation to simulate the stretch:
const container = document.querySelector('.container');
let isTouching = false;
let startY = 0;
container.addEventListener('touchstart', (e) => {
isTouching = true;
startY = e.touches[0].clientY;
}, { passive: true });
container.addEventListener('touchmove', (e) => {
if (!isTouching) return;
const currentY = e.touches[0].clientY;
const deltaY = currentY - startY;
if (container.scrollTop <= 0 && deltaY > 0) {
// Overscrolling at the top
const scale = 1 + deltaY / 600;
container.style.transformOrigin = 'top center';
container.style.transform = `scaleY(${scale})`;
e.preventDefault();
} else if (container.scrollHeight - container.scrollTop <= container.clientHeight && deltaY < 0) {
// Overscrolling at the bottom
const scale = 1 - deltaY / 600;
container.style.transformOrigin = 'bottom center';
container.style.transform = `scaleY(${scale})`;
e.preventDefault();
}
}, { passive: false });
container.addEventListener('touchend', () => {
isTouching = false;
container.style.transform = 'scaleY(1)'; // Reset the scale
}, { passive: true });
CSS:
.container class has overflow-y: scroll to enable vertical scrolling and a fixed height to create a scrollable area.transition is applied to smooth out the scaling animation when the transform property changes.JavaScript:
isTouching to track touch state, startY to record the starting Y position of the touch.touchstart: When the user touches the screen, we set isTouching to true and record the startY position.touchmove:
deltaY as the difference between the current touch position and startY.scrollTop <= 0 and deltaY > 0) or the bottom of the container.deltaY.e.preventDefault() to prevent the default scrolling behavior while overscrolling.touchend: Reset the transform to its original state when the touch ends.You can see a live demonstration of this effect by opening the following snippet on a touch-enabled device:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
overflow-y: scroll;
height: 400px;
transition: transform 0.2s ease-out;
background-color: #f0f0f0;
}
.container h1 {
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<!-- Repeat the content to make it scrollable -->
<h1>Hello</h1>
<h1>Hello</h1>
<!-- ... -->
</div>
<script>
// JavaScript code from above
</script>
</body>
</html>
Customization:
deltaY / 600 to control how stretchy the overscroll effect is. A smaller number will make it stretch more with less scrolling.transition duration in CSS for a faster or slower snap-back animation.Compatibility:
wheel event, but be cautious as it may interfere with the default scrolling experience.Enhancements:
Feel free to ask if you need assistance adapting this code or implementing additional features!
Not yet included in a published release.