-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
59 lines (48 loc) · 1.74 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
const searchForm = document.getElementById("search-form");
const searchBox = document.getElementById("search-box");
const searchResult = document.getElementById("search-result");
const showMoreBtn = document.getElementById("show-more-btn");
const accessKey = "bBpf_oygynpa0Hf-8X0hzGU8DgV_u0Q6sid_7XLb_ZA"; //"Add you own access key given by unspalsh or any website you use"
let keyboard = "";
let page = 1;
async function searchImages() {
keyboard = searchBox.value;
const url = `https://api.unsplash.com/search/photos?page=${page}&query=${keyboard}&client_id=${accessKey}&per_page=12`;
const response = await fetch(url);
const data = await response.json();
const results = data.results;
results.map((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);
searchResult.appendChild(imageLink);
});
loader.style.display = "block";
}
searchForm.addEventListener("submit", (e) => {
e.preventDefault();
page = 1;
searchResult.innerHTML = ""; // Clear previous search results
searchImages();
});
showMoreBtn.addEventListener("click", (e) => {
page++;
searchImages();
});
// Add the 'fade-in' class to the search result images
function fadeInImages() {
const images = document.querySelectorAll("#search-result img");
images.forEach((image) => {
image.classList.add("fade-in");
});
}
// Add the 'fade-in' class to the body element
function fadeInPage() {
const body = document.body;
body.classList.add("fade-in");
}
// Call the 'fadeInPage' function to trigger the fade-in animation
window.addEventListener("load", fadeInPage);