Introduction
In this tutorial, you'll learn how to create a simple image generation loading screen that displays product carousels using HTML, CSS, and JavaScript. This mimics the new ad format that ChatGPT is implementing, where users see advertisements while waiting for image generation to complete. We'll build a basic carousel that simulates the loading experience.
Prerequisites
To follow this tutorial, you'll need:
- A basic understanding of HTML, CSS, and JavaScript
- A code editor (like VS Code or any text editor)
- A web browser to test your code
Step-by-step Instructions
Step 1: Create the HTML Structure
We'll start by creating the basic HTML structure for our loading screen with a product carousel. This will include a container for the loading animation and a carousel section.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Generation Loading Screen</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="loading-container" id="loadingContainer">
<div class="loading-animation">
<div class="spinner"></div>
<p>Generating your image...</p>
</div>
<div class="carousel-container" id="carouselContainer">
<div class="carousel" id="carousel">
<!-- Product items will be added here dynamically -->
</div>
<button class="carousel-btn prev" id="prevBtn"><</button>
<button class="carousel-btn next" id="nextBtn">></button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Step 2: Add CSS Styling
Now we'll create the CSS to style our loading screen and carousel. This will include the spinner animation, carousel layout, and responsive design.
/* style.css */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.loading-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
padding: 20px;
}
.loading-animation {
text-align: center;
margin-bottom: 30px;
}
.spinner {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 0 auto 15px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.carousel-container {
width: 100%;
max-width: 600px;
position: relative;
overflow: hidden;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.carousel {
display: flex;
transition: transform 0.5s ease-in-out;
}
.carousel-item {
min-width: 100%;
padding: 20px;
text-align: center;
background-color: white;
}
.carousel-item img {
max-width: 100%;
height: auto;
border-radius: 5px;
}
.carousel-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0,0,0,0.5);
color: white;
border: none;
padding: 10px 15px;
cursor: pointer;
border-radius: 50%;
font-size: 18px;
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
.carousel-btn:hover {
background-color: rgba(0,0,0,0.8);
}
Step 3: Implement JavaScript Functionality
Next, we'll add the JavaScript to handle the carousel functionality and simulate the loading process.
// script.js
const products = [
{
id: 1,
name: "Wireless Headphones",
price: "$99.99",
image: "https://via.placeholder.com/300x200/3498db/ffffff?text=Headphones"
},
{
id: 2,
name: "Smart Watch",
price: "$199.99",
image: "https://via.placeholder.com/300x200/e74c3c/ffffff?text=Smart+Watch"
},
{
id: 3,
name: "Bluetooth Speaker",
price: "$79.99",
image: "https://via.placeholder.com/300x200/2ecc71/ffffff?text=Speaker"
},
{
id: 4,
name: "Laptop Stand",
price: "$39.99",
image: "https://via.placeholder.com/300x200/f39c12/ffffff?text=Laptop+Stand"
}
];
let currentIndex = 0;
const carousel = document.getElementById('carousel');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const loadingContainer = document.getElementById('loadingContainer');
// Create carousel items
function createCarouselItems() {
carousel.innerHTML = '';
products.forEach(product => {
const item = document.createElement('div');
item.className = 'carousel-item';
item.innerHTML = `
<img src="${product.image}" alt="${product.name}">
<h3>${product.name}</h3>
<p>${product.price}</p>
`;
carousel.appendChild(item);
});
}
// Show current item
function showItem(index) {
const carouselWidth = carousel.offsetWidth;
carousel.style.transform = `translateX(-${index * carouselWidth}px)`;
}
// Next item
function nextItem() {
currentIndex = (currentIndex + 1) % products.length;
showItem(currentIndex);
}
// Previous item
function prevItem() {
currentIndex = (currentIndex - 1 + products.length) % products.length;
showItem(currentIndex);
}
// Auto-rotate carousel
let autoRotate = setInterval(nextItem, 3000);
// Pause rotation when user interacts
carousel.addEventListener('mouseenter', () => {
clearInterval(autoRotate);
});
carousel.addEventListener('mouseleave', () => {
autoRotate = setInterval(nextItem, 3000);
});
// Event listeners for buttons
prevBtn.addEventListener('click', prevItem);
nextBtn.addEventListener('click', nextItem);
// Initialize
createCarouselItems();
showItem(currentIndex);
// Simulate loading process
setTimeout(() => {
// In a real app, this would be when the image generation is complete
loadingContainer.innerHTML = 'Image Generated Successfully!
Your image is ready to view.
';
}, 10000); // 10 seconds to simulate loading
Step 4: Test Your Implementation
Save all your files (index.html, style.css, and script.js) in the same folder. Open index.html in your web browser to see the loading screen in action. You should see:
- A spinning loading animation
- A product carousel that automatically rotates
- Navigation buttons to manually control the carousel
- After 10 seconds, the loading screen changes to a success message
Step 5: Customize Your Carousel
You can customize the product information, styling, and behavior by modifying the JavaScript array and CSS styles. For example, you can add more products or change the auto-rotation speed:
// Change auto-rotation speed (in milliseconds)
let autoRotate = setInterval(nextItem, 5000); // 5 seconds instead of 3
Step 6: Add More Features (Optional)
For a more advanced version, consider adding:
- Pause/resume functionality when users interact with the carousel
- Indicator dots to show current position in the carousel
- Responsive design adjustments for mobile devices
- Real API integration to fetch product data
Summary
In this tutorial, you've learned how to create a loading screen similar to ChatGPT's new ad format. You built a complete HTML page with CSS styling and JavaScript functionality that includes:
- A spinning loading animation
- A responsive product carousel with navigation buttons
- Auto-rotation functionality
- Interactive controls
This implementation demonstrates how to handle loading states and display advertisements or product information while waiting for a process to complete. The skills you've learned can be applied to various web applications that need to show loading states or promotional content during processing.