-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (55 loc) · 1.93 KB
/
script.js
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
const accesskey = "bjh6hJYaJVF6X0s5QZAXC5gbUtHCQSfjnppYui8atSA";
const formEl = document.querySelector("form");
const input = document.getElementById("search-input");
const searchreslts = document.querySelector(".search-results"); // Correct variable name
const showmore = document.getElementById("show-more");
let inputdata = "";
let page = 1;
async function searchimages() {
inputdata = input.value;
const url = `https://api.unsplash.com/search/photos?page=${page}&query=${inputdata}&client_id=${accesskey}`;
const response = await fetch(url);
const data = await response.json();
const scrolltop = document.getElementById("scroll-up");
const results = data.results;
console.log(results);
if (page === 1) {
searchreslts.innerHTML = "";
}
scrolltop.addEventListener("click", () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
});
results.map((result) => {
const imagewrapper = document.createElement("div");
imagewrapper.classList.add("search-result");
const image = document.createElement("img");
image.src = result.urls.small;
image.alt = result.alt_description;
const imagelink = document.createElement("a");
imagelink.href = result.links.html;
imagelink.target = "_blank";
imagelink.textContent = result.alt_description;
imagelink.style.color = "white"; // Set the text color to white
imagelink.style.textAlign = "center"; // Align the text in the center
imagewrapper.appendChild(image);
imagewrapper.appendChild(imagelink);
searchreslts.appendChild(imagewrapper); // Append imagewrapper to the container
});
page++;
if (page > 1) {
showmore.style.display = "block";
// showmore.style.background-color = rgb(20, 19, 19);
// showmore.style.color = "red";
}
}
formEl.addEventListener("submit", (event) => {
event.preventDefault();
page = 1;
searchimages();
});
showmore.addEventListener("click", (event) => {
searchimages();
});