-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.html
More file actions
186 lines (161 loc) · 5.1 KB
/
search.html
File metadata and controls
186 lines (161 loc) · 5.1 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Results - PINSPIRE</title>
<link rel="stylesheet" href="pin.css">
<style>
.search-header {
display: flex;
align-items: center;
padding: 20px;
background-color: #fff;
border-bottom: 1px solid #ddd;
}
.search-header .logo img {
height: 50px;
width: 50px;
}
.search-header .search-input {
flex: 1;
margin: 0 20px;
}
#container {
display: flex;
flex-wrap: wrap;
margin-top: 100px;
}
.col {
height: auto;
width: calc(100% / 6);
display: flex;
flex-direction: column;
padding: 20px;
gap: 20px;
}
.card {
margin: 0;
width: 100%;
}
.card img {
width: 100%;
height: auto;
object-fit: contain;
border-radius: 20px;
}
#loader {
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
margin: 20px auto;
margin-top: 50px;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="search-header">
<div class="logo">
<a href="index.html">
<img src="./images/download.png" alt="Logo">
</a>
</div>
<div class="search-container" style="flex: 1;">
<input type="text" class="search-input" placeholder="Searched for dogs" id="search">
<button class="search-button" onclick="performSearch()">
<img src="./images/search.png" alt="Search Icon" class="search-icon">
</button>
</div>
</div>
<div id="loader"></div>
<div id="container">
<div class="col">
</div>
<div class="col">
</div>
<div class="col">
</div>
<div class="col">
</div>
<div class="col">
</div>
</div>
<script>
let page = 1;
let fetching = false;
const container = document.getElementById('container');
const cols = Array.from(container.getElementsByClassName('col'));
console.log(cols)
const fetchImageData = async () => {
try {
fetching = true;
document.getElementById('loader').style.display = 'block';
const response = await fetch('https://dog.ceo/api/breeds/image/random/30');
const data = await response.json();
fetching = false;
return data.message;
} catch (error) {
console.error("Error fetching data:", error);
fetching = false;
throw error;
}
};
fetchImageData().then((images) => {
if (images.length > 0) {
images.forEach((image, index) => {
createCard(image, cols[index % cols.length]);
console.log(index % cols.length)
});
}
}).catch((error) => {
console.error("Error initial fetch:", error);
});
const createCard = (image, col) => {
const card = document.createElement('div');
card.classList.add('card');
const img = document.createElement('img');
img.src = image;
img.alt = "Random Dog Image";
img.style.width = "100%";
img.onerror = function () {
this.parentElement.style.display = "none";
};
img.onload = function () {
document.getElementById('loader').style.display = 'none';
};
card.appendChild(img);
col.appendChild(card);
};
const handleScroll = () => {
if (fetching) return;
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const windowHeight = window.innerHeight;
const bodyHeight = document.documentElement.scrollHeight;
if (bodyHeight - scrollTop - windowHeight < 800) {
page++;
fetchImageData().then((images) => {
if (images.length > 0) {
images.forEach((image, index) => {
createCard(image, cols[index % cols.length]);
});
}
}).catch((error) => {
console.error("Error handling scroll:", error);
});
}
};
window.addEventListener('scroll', handleScroll);
</script>
</body>
</html>