-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (63 loc) · 2.96 KB
/
Copy pathscript.js
File metadata and controls
70 lines (63 loc) · 2.96 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
document.addEventListener("DOMContentLoaded", () => {
const searchInput = document.getElementById("search-input");
const searchButton = document.getElementById("search-button");
const imageGallery = document.getElementById("image-gallery");
const loadMoreButton = document.getElementById("load-more");
const darkModeToggle = document.getElementById("dark-mode-toggle");
const title = document.getElementById("title");
let query = "";
let page = 1;
const API_KEY = "49262872-982a7fb81e61a1b7d15cd675d";
const API_URL = "https://pixabay.com/api/?key=" + API_KEY + "&q=";
const fetchImages = () => {
if (!query) return;
fetch(`${API_URL}${query}&page=${page}&per_page=20`)
.then(response => response.json())
.then(data => {
if (data.hits.length > 0) {
data.hits.forEach(image => {
const imgContainer = document.createElement("div");
imgContainer.classList.add("image-container");
const imgElement = document.createElement("img");
imgElement.src = image.webformatURL;
imgElement.alt = image.tags;
imgElement.addEventListener("click", () => {
window.open(image.largeImageURL, "_blank");
});
const downloadButton = document.createElement("a");
downloadButton.href = image.largeImageURL;
downloadButton.download = "image.jpg";
downloadButton.innerText = "Download";
downloadButton.classList.add("download-button");
imgContainer.appendChild(imgElement);
imgContainer.appendChild(downloadButton);
imageGallery.appendChild(imgContainer);
});
loadMoreButton.style.display = "block";
} else {
alert("No images found");
}
})
.catch(error => console.error("Error fetching images:", error));
};
searchButton.addEventListener("click", () => {
imageGallery.innerHTML = "";
query = searchInput.value;
page = 1;
fetchImages();
});
loadMoreButton.addEventListener("click", () => {
page++;
fetchImages();
});
darkModeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
if (document.body.classList.contains("dark-mode")) {
title.style.color = "white";
darkModeToggle.innerHTML = "☀️";
} else {
title.style.color = "black";
darkModeToggle.innerHTML = "🌙";
}
});
});