Skip to content

Commit b8c75ee

Browse files
ayush-pandey047sumn2u
authored andcommitted
feat: Add Random Photo Viewer with attractive header, fixed image area, loader, and responsive button
1 parent b7e201f commit b8c75ee

File tree

10 files changed

+216
-0
lines changed

10 files changed

+216
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 📸 Random Photo Viewer
2+
3+
A simple and fun JavaScript project that displays a random image every time you click a button.
4+
5+
## 🔍 Concepts Demonstrated
6+
- Arrays and random selection in JavaScript
7+
- DOM manipulation and event handling
8+
- CSS transitions for smooth effects
9+
10+
## 🚀 How It Works
11+
1. Images are stored in an array.
12+
2. Each time the button is clicked, a random index is chosen.
13+
3. The image source is updated, and a fade transition is applied.
14+
15+
## 🧩 Bonus Idea
16+
You can replace the local array with an API like Unsplash to load new random images dynamically.
30.4 KB
Loading
104 KB
Loading
11 KB
Loading
8.15 KB
Loading
965 KB
Loading
1.65 MB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Random Photo Viewer</title>
7+
<link rel="stylesheet" href="style.css" />
8+
</head>
9+
<body>
10+
<div class="container">
11+
<div class="header">
12+
<h1>Random Photo Viewer</h1>
13+
<button id="btn">Show Random Photo</button>
14+
</div>
15+
16+
17+
<div class="image-wrapper">
18+
<div id="loader"></div>
19+
<img id="photo" src="" alt="Random Photo" />
20+
</div>
21+
</div>
22+
23+
24+
<script src="script.js"></script>
25+
</body>
26+
</html>
27+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const btn = document.getElementById("btn");
2+
const img = document.getElementById("photo");
3+
const loader = document.getElementById("loader");
4+
5+
const photos = [
6+
"images/photo1.jpg",
7+
"images/photo2.jpg",
8+
"images/photo3.jpg",
9+
"images/photo4.jpg",
10+
"images/photo5.jpg"
11+
];
12+
13+
function showRandomPhoto() {
14+
if (!btn || !img || !loader) return; // safety check
15+
16+
loader.style.display = "block";
17+
img.style.opacity = 0;
18+
19+
const randomIndex = Math.floor(Math.random() * photos.length);
20+
img.src = photos[randomIndex];
21+
22+
img.onload = () => {
23+
loader.style.display = "none";
24+
img.style.opacity = 1;
25+
};
26+
}
27+
28+
// Load first image on page load
29+
window.addEventListener("load", showRandomPhoto);
30+
31+
// Button click
32+
btn.addEventListener("click", showRandomPhoto);
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* 🌟 Body and General Layout */
2+
body {
3+
display: flex;
4+
justify-content: center;
5+
align-items: center;
6+
flex-direction: column;
7+
height: 100vh;
8+
margin: 0;
9+
background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
10+
font-family: 'Poppins', sans-serif;
11+
}
12+
13+
/* 🌟 Container */
14+
.container {
15+
text-align: center;
16+
width: 720px;
17+
max-width: 90%;
18+
}
19+
20+
/* 🌟 Header: Title + Button side by side */
21+
.header {
22+
display: flex;
23+
justify-content: space-between;
24+
align-items: center;
25+
margin-bottom: 20px;
26+
}
27+
28+
/* 🌟 Attractive Header */
29+
.header h1 {
30+
font-size: 2.2rem;
31+
background: linear-gradient(90deg, #ff8c00, #ff3c00);
32+
-webkit-background-clip: text;
33+
-webkit-text-fill-color: transparent;
34+
font-weight: 700;
35+
letter-spacing: 1px;
36+
text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.2);
37+
transition: transform 0.3s ease, text-shadow 0.3s ease;
38+
display: inline-block;
39+
cursor: default;
40+
}
41+
42+
.header h1:hover {
43+
transform: scale(1.05) rotate(-2deg);
44+
text-shadow: 3px 3px 10px rgba(255, 140, 0, 0.6);
45+
}
46+
47+
/* 🌟 Header Button */
48+
.header button {
49+
padding: 10px 22px;
50+
font-size: 16px;
51+
background: #ff8c00;
52+
color: #fff;
53+
border: none;
54+
border-radius: 10px;
55+
cursor: pointer;
56+
font-weight: 500;
57+
box-shadow: 0 5px 15px rgba(255, 140, 0, 0.4);
58+
transition: all 0.3s ease;
59+
}
60+
61+
.header button:hover {
62+
background: #ff6600;
63+
transform: scale(1.05);
64+
box-shadow: 0 8px 20px rgba(255, 102, 0, 0.5);
65+
}
66+
67+
/* 🌟 Image Wrapper / Card */
68+
.image-wrapper {
69+
width: 100%;
70+
height: 500px;
71+
border-radius: 20px;
72+
overflow: hidden;
73+
position: relative;
74+
background-color: #e0e0e0;
75+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
76+
display: flex;
77+
justify-content: center;
78+
align-items: center;
79+
transition: transform 0.3s ease, box-shadow 0.3s ease;
80+
}
81+
82+
.image-wrapper:hover {
83+
transform: translateY(-5px);
84+
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
85+
}
86+
87+
/* 🌟 Image Styling */
88+
.image-wrapper img {
89+
width: 100%;
90+
height: 100%;
91+
object-fit: contain; /* scales large images, keeps small ones visible */
92+
object-position: center;
93+
opacity: 0;
94+
transition: opacity 0.6s ease-in-out;
95+
}
96+
97+
/* 🌟 Loader Spinner */
98+
#loader {
99+
position: absolute;
100+
top: 50%;
101+
left: 50%;
102+
transform: translate(-50%, -50%);
103+
border: 6px solid #f3f3f3;
104+
border-top: 6px solid #ff8c00;
105+
border-radius: 50%;
106+
width: 50px;
107+
height: 50px;
108+
animation: spin 1s linear infinite;
109+
display: none;
110+
}
111+
112+
@keyframes spin {
113+
0% { transform: translate(-50%, -50%) rotate(0deg); }
114+
100% { transform: translate(-50%, -50%) rotate(360deg); }
115+
}
116+
117+
/* 🌟 Responsive Design */
118+
@media (max-width: 768px) {
119+
.container {
120+
width: 90%;
121+
}
122+
123+
.header {
124+
flex-direction: column;
125+
gap: 10px;
126+
}
127+
128+
.header h1 {
129+
font-size: 1.5rem;
130+
text-align: center;
131+
}
132+
133+
.header button {
134+
font-size: 16px;
135+
padding: 10px 16px;
136+
}
137+
138+
.image-wrapper {
139+
height: 300px;
140+
}
141+
}

0 commit comments

Comments
 (0)