Skip to content

Commit df26cd3

Browse files
committed
image search using unsplash api
1 parent 87fcfa6 commit df26cd3

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

image-search/index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<link rel="stylesheet" href="styles.css">
6+
<meta charset="UTF-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Image Search</title>
9+
</head>
10+
11+
<body>
12+
<main>
13+
<div class="search">
14+
<input type="text" name="search" id="search-input" placeholder="Search for images">
15+
<button type="submit" id="search-btn">Search</button>
16+
</div>
17+
<div class="search-result">
18+
<div class="image-cards" id="image-cards">
19+
20+
</div>
21+
</div>
22+
</main>
23+
<script src="script.js"></script>
24+
</body>
25+
26+
</html>

image-search/script.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const searchText = document.getElementById("search-input");
2+
const searchBtn = document.getElementById("search-btn");
3+
const imageCards = document.getElementById("image-cards");
4+
const imageApi = "https://api.unsplash.com/search/photos?query=";
5+
const accessKey = "cZkBLec2VWlXBAgs785p7uw9nagFu_G0Zgb8e5lK400";
6+
const fetchImages = async (query) => {
7+
if (!query) {
8+
console.log("Query is empty");
9+
return;
10+
}
11+
try {
12+
const response = await fetch(`${imageApi}${query}&client_id=${accessKey}`);
13+
const data = await response.json();
14+
console.log(data);
15+
16+
displayImages(data.results);
17+
} catch (error) {
18+
console.log("Error fetching images:", error);
19+
}
20+
};
21+
22+
const displayImages = (images) => {
23+
imageCards.innerHTML = "";
24+
images.forEach((image) => {
25+
const imageBlock = document.createElement("div");
26+
const imgElement = document.createElement("img");
27+
const imgCaption = document.createElement("p");
28+
imgCaption.innerText = image.description;
29+
imgElement.src = image.urls.small;
30+
imgElement.alt = image.alt_description;
31+
imageBlock.className = "image-card";
32+
imageBlock.appendChild(imgElement);
33+
imageBlock.appendChild(imgCaption);
34+
35+
imageCards.appendChild(imageBlock);
36+
});
37+
};
38+
39+
const searchImage = () => {
40+
const query = searchText.value.trim();
41+
fetchImages(query);
42+
};
43+
44+
searchBtn.addEventListener("click", searchImage);

image-search/styles.css

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
* {
2+
padding: 0;
3+
margin: 0;
4+
box-sizing: border-box;
5+
}
6+
7+
main {
8+
display: flex;
9+
flex-direction: column;
10+
justify-content: center;
11+
align-items: center;
12+
gap: 5rem;
13+
}
14+
15+
.search {
16+
margin-top: 3rem;
17+
padding: 1rem;
18+
}
19+
20+
input {
21+
font-size: 1.2rem;
22+
padding: 10px;
23+
border-radius: 10px;
24+
outline: none;
25+
border-color: blue;
26+
}
27+
button {
28+
font-size: 1.2rem;
29+
padding: 10px;
30+
background-color: green;
31+
border: none;
32+
border-radius: 10px;
33+
color: azure;
34+
font-weight: 600;
35+
cursor: pointer;
36+
}
37+
38+
.search-results {
39+
40+
}
41+
42+
.image-cards {
43+
display: flex;
44+
flex-wrap: wrap;
45+
justify-content: center;
46+
gap: 2rem;
47+
}
48+
49+
.image-card {
50+
border-radius: 10px;
51+
padding: 10px;
52+
display: flex;
53+
flex-direction: column;
54+
background-color: aqua;
55+
height: 350px;
56+
width: 350px;
57+
}
58+
59+
img {
60+
object-fit: cover;
61+
height: 300px;
62+
width: 300px;
63+
64+
}
65+
66+
p {
67+
text-justify: auto;
68+
}
69+
70+
img:hover {
71+
transform: scale(1.1);
72+
cursor: pointer;
73+
}

0 commit comments

Comments
 (0)