I'll create a parallax scrolling effect with an image that shrinks from fullscreen to a fixed size, then expands back when scrolling up. Here's the complete implementation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Parallax Shrinking Effect</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
overflow-x: hidden;
}
.hero-section {
height: 200vh; /* Extra height for scroll effect */
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
.parallax-image {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100vw;
height: 100vh;
object-fit: cover;
z-index: 1;
transition: all 0.1s ease-out;
}
.content-section {
position: relative;
z-index: 2;
background: white;
min-height: 100vh;
padding: 50px 20px;
margin-top: 100vh; /* Start after the hero section */
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
.content-block {
margin-bottom: 50px;
padding: 30px;
background: #f8f9fa;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.content-block h2 {
color: #333;
margin-bottom: 20px;
font-size: 2.5em;
}
.content-block p {
color: #666;
line-height: 1.6;
font-size: 1.1em;
margin-bottom: 15px;
}
.static-image-container {
display: flex;
justify-content: center;
margin: 50px 0;
}
.static-image {
width: 1000px;
height: 500px;
object-fit: cover;
border-radius: 10px;
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
display: none; /* Hidden initially */
}
.footer {
background: #333;
color: white;
padding: 50px 20px;
text-align: center;
}
/* Smooth scrolling */
html {
scroll-behavior: smooth;
}
</style>
</head>
<body>
<div class="hero-section">
<img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2070&q=80"
alt="Parallax Image"
class="parallax-image"
id="parallaxImage">
</div>
<div class="content-section">
<div class="container">
<div class="content-block">
<h2>Welcome to Our Website</h2>
<p>This is where the magic happens! As you scrolled down, you witnessed our hero image gracefully shrinking from fullscreen to a perfect 1000x500px rectangle. This creates an engaging parallax effect that draws users into the content.</p>
<p>The image transformation is smooth and responsive, adapting to different screen sizes while maintaining its aspect ratio and visual appeal.</p>
</div>
<div class="static-image-container">
<img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2070&q=80"
alt="Static Image"
class="static-image"
id="staticImage">
</div>
<div class="content-block">
<h2>About Our Parallax Effect</h2>
<p>The parallax scrolling effect you just experienced is created using a combination of CSS transforms and JavaScript scroll event listeners. As you scroll, the image dynamically resizes and repositions itself.</p>
<p>When you scroll back up, the effect reverses seamlessly, expanding the image back to fullscreen. This creates an immersive user experience that feels modern and engaging.</p>
</div>
<div class="content-block">
<h2>Technical Implementation</h2>
<p>The effect uses fixed positioning for the parallax image, allowing it to stay in place while the content scrolls beneath it. JavaScript calculates the scroll progress and applies appropriate transforms to achieve the shrinking and expanding effect.</p>
<p>The transition between the parallax image and the static image is handled smoothly, ensuring a seamless user experience throughout the scroll journey.</p>
</div>
<div class="content-block">
<h2>Responsive Design</h2>
<p>Our implementation is fully responsive and works across different screen sizes. The image scaling adapts to maintain proper proportions on mobile devices, tablets, and desktop computers.</p>
<p>Try scrolling back up to see the reverse effect in action! The image will gradually expand back to fullscreen as you scroll towards the top of the page.</p>
</div>
</div>
</div>
<div class="footer">
<div class="container">
<h3>Thank You for Scrolling!</h3>
<p>You've experienced our custom parallax effect. Scroll back up to see it in reverse!</p>
</div>
</div>
<script>
const parallaxImage = document.getElementById('parallaxImage');
const staticImage = document.getElementById('staticImage');
const heroSection = document.querySelector('.hero-section');
// Target dimensions for the shrunk image
const targetWidth = 1000;
const targetHeight = 500;
function updateParallaxEffect() {
const scrollY = window.scrollY;
const windowHeight = window.innerHeight;
const heroHeight = heroSection.offsetHeight;
// Calculate scroll progress (0 to 1)
const scrollProgress = Math.min(scrollY / (heroHeight - windowHeight), 1);
if (scrollProgress < 1) {
// Show parallax image, hide static image
parallaxImage.style.display = 'block';
staticImage.style.display = 'none';
// Calculate current dimensions
const currentWidth = window.innerWidth - (window.innerWidth - targetWidth) * scrollProgress;
const currentHeight = windowHeight - (windowHeight - targetHeight) * scrollProgress;
// Apply transforms
parallaxImage.style.width = currentWidth + 'px';
parallaxImage.style.height = currentHeight + 'px';
// Adjust opacity for smooth transition
const opacity = 1 - Math.max(0, (scrollProgress - 0.8) * 5);
parallaxImage.style.opacity = opacity;
} else {
// Hide parallax image, show static image
parallaxImage.style.display = 'none';
staticImage.style.display = 'block';
}
}
// Throttle scroll events for better performance
let ticking = false;
function requestTick() {
if (!ticking) {
requestAnimationFrame(updateParallaxEffect);
ticking = true;
}
}
function handleScroll() {
ticking = false;
requestTick();
}
// Add scroll event listener
window.addEventListener('scroll', handleScroll);
// Initialize on page load
window.addEventListener('load', updateParallaxEffect);
// Handle window resize
window.addEventListener('resize', updateParallaxEffect);
</script>
</body>
</html>
This implementation creates exactly what you requested:
Key Features:
- Fullscreen Start: The image begins covering the entire viewport
- Gradual Shrinking: As you scroll down, the image smoothly shrinks to 1000x500px
- Content Appearance: Once shrunk, regular website content appears below
- Static Image: The image becomes a normal static element in the content flow
- Reverse Effect: When scrolling back up, the image gradually expands back to fullscreen
How it Works:
- Uses
position: fixed for the parallax image to keep it in place during scrolling
- JavaScript calculates scroll progress and applies real-time transforms
- Smooth transitions between parallax and static states
- Responsive design that adapts to different screen sizes
- Performance optimized with
requestAnimationFrame
Technical Details:
- The hero section has extra height (200vh) to provide scroll space for the effect
- The image transforms from 100vw×100vh to 1000px×500px based on scroll progress
- Opacity transitions help create smooth visual changes
- The static image appears once the parallax effect completes
You can replace the image URL with your own image. The effect works smoothly in both directions and provides an engaging user experience!