-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgallery.html
More file actions
145 lines (121 loc) · 4.15 KB
/
gallery.html
File metadata and controls
145 lines (121 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Gallery</title>
<style>
* { box-sizing: border-box; }
body { font-family: sans-serif; margin: 0; }
nav {
background-color: blueviolet;
padding: 10px;
display: flex;
align-items: center;
gap: 15px;
}
nav a { color: white; text-decoration: none; border: 2px solid white; padding: 5px 14px; text-align: center; }
nav h1 { color: white; margin: 0; flex: 1; text-align: center; }
nav input { padding: 5px; }
.filters { padding: 10px; display: flex; gap: 8px; flex-wrap: wrap; }
.filter-btn { padding: 5px 12px; cursor: pointer; border: 1px solid blueviolet; background: white; color: blueviolet; }
.filter-btn.active { background: blueviolet; color: white; }
main {
display: flex;
flex-wrap: wrap;
gap: 15px;
padding: 15px;
}
.card {
border: 1px solid #ccc;
padding: 10px;
width: 150px;
text-align: center;
background-color: white;
}
.card img { width: 100px; height: 100px; }
.card p { margin: 4px 0; text-transform: capitalize; font-size: 0.9rem; }
</style>
</head>
<body>
<nav>
<a href="index.html">Home</a>
<h1>Gallery</h1>
<input type="text" id="search" placeholder="Search..." oninput="handleSearch(this.value)">
</div>
</nav>
<div class="filters">
<button class="filter-btn active" data-type="all" onclick="setFilter('all')">All</button>
<button class="filter-btn" data-type="fire" onclick="setFilter('fire')">Fire</button>
<button class="filter-btn" data-type="water" onclick="setFilter('water')">Water</button>
<button class="filter-btn" data-type="grass" onclick="setFilter('grass')">Grass</button>
<button class="filter-btn" data-type="electric" onclick="setFilter('electric')">Electric</button>
<button class="filter-btn" data-type="psychic" onclick="setFilter('psychic')">Psychic</button>
<button class="filter-btn" data-type="normal" onclick="setFilter('normal')">Normal</button>
<button class="filter-btn" data-type="poison" onclick="setFilter('poison')">Poison</button>
</div>
<main id="gallery">
<div class="loading">Loading Pokemon...</div>
</main>
<script>
let allPokemon = [];
let currentFilter = 'all';
let searchQuery = '';
function renderGallery(data) {
const gallery = document.querySelector('#gallery');
if (data.length === 0) {
gallery.innerHTML = '<div class="no-results">No Pokemon found</div>';
return;
}
let html = '';
for (let pokemon of data) {
html += `<div class="card">
<img src="${pokemon.image}" alt="${pokemon.name}">
<p><strong>${pokemon.name}</strong></p>
<p>${pokemon.type1}${pokemon.type2 ? ' / ' + pokemon.type2 : ''}</p>
</div>`;
}
gallery.innerHTML = html;
}
function applyFilters() {
let filtered = allPokemon;
if (currentFilter !== 'all') {
filtered = filtered.filter(p =>
p.type1 === currentFilter || p.type2 === currentFilter
);
}
if (searchQuery) {
filtered = filtered.filter(p =>
p.name.toLowerCase().includes(searchQuery.toLowerCase())
);
}
renderGallery(filtered);
}
function setFilter(type) {
currentFilter = type;
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.type === type);
});
applyFilters();
}
function handleSearch(query) {
searchQuery = query;
applyFilters();
}
async function loadPokemon() {
const gallery = document.querySelector('#gallery');
gallery.innerHTML = '<div class="loading">Loading Pokemon...</div>';
for (let i = 1; i <= 50; i++) {
const response = await fetch(`https://pokedextr.uwista.dev/pokemon/${i}`);
const data = await response.json();
allPokemon.push(data);
if (i % 10 === 0) {
renderGallery(allPokemon);
}
}
renderGallery(allPokemon);
}
loadPokemon();
</script>
</body>
</html>