-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (45 loc) · 1.56 KB
/
Copy pathscript.js
File metadata and controls
57 lines (45 loc) · 1.56 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
const searchForm = document.getElementById("search-form");
const searchBox = document.getElementById("search-box");
const searchContent = document.getElementById("search-content");
const showMoreBtn = document.getElementById("show-more-btn");
let accessKey = `bVuUbciJ0HDfHukGqSzmejsVYVU6k6H3wbNy9MGCFqI`;
let keyword = "nature";
let page = 1;
async function searchImages() {
const url = `https://api.unsplash.com/search/photos?page=${page}&query=${keyword}&client_id=${accessKey}&per_page=15`;
try {
const response = await fetch(url);
const data = await response.json();
if (page === 1) {
searchContent.innerHTML = "";
}
const results = data.results;
results.forEach((result) => {
const image = document.createElement("img");
image.src = result.urls.small;
const imageLink = document.createElement("a");
imageLink.href = result.links.html;
imageLink.target = "_blank";
imageLink.appendChild(image);
searchContent.appendChild(imageLink);
});
if (data.total_pages > page) {
showMoreBtn.style.display = "block";
} else {
showMoreBtn.style.display = "none";
}
} catch (error) {
console.error('Error fetching images:', error);
}
}
searchForm.addEventListener("submit", (e) => {
e.preventDefault();
keyword = searchBox.value;
page = 1;
searchImages();
});
showMoreBtn.addEventListener("click", () => {
page++;
searchImages();
});
searchImages();