Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e2017e8

Browse files
Random Cats Generator HTML, CSS and JAVASCRIPT
1 parent cac7c70 commit e2017e8

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

‎77. Random Cat Images/app.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const url = "https://api.thecatapi.com/v1/images/search";
2+
const section = document.querySelector(".container");
3+
const button = document.querySelector(".btn");
4+
5+
button.addEventListener("click", getRandomCats);
6+
7+
randomCatPhoto = (json) => {
8+
let photo = json[0].url;
9+
section.classList.add("cats");
10+
11+
let image = document.createElement("img");
12+
image.src = photo;
13+
image.classList.add("random_cats");
14+
image.alt = photo;
15+
section.appendChild(image);
16+
};
17+
18+
async function getRandomCats() {
19+
section.innerHTML = "";
20+
try {
21+
const response = await fetch(url);
22+
const json = await response.json();
23+
console.log("JSON:", json);
24+
return randomCatPhoto(json);
25+
} catch (e) {
26+
console.log("This is an error");
27+
console.log(e);
28+
}
29+
}

‎77. Random Cat Images/index.html‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="UTF-8" />
4+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Random Cat Images</title>
7+
<link rel="stylesheet" href="style.css" />
8+
</head>
9+
<body>
10+
<section class="header-content">
11+
<button class="btn">Get Random Cat</button>
12+
</section>
13+
14+
<div class="container"></div>
15+
16+
<script src="app.js"></script>
17+
</body>
18+
</html>

‎77. Random Cat Images/style.css‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
body {
2+
background: rgb(17, 17, 17);
3+
}
4+
5+
.header-content {
6+
text-align: center;
7+
}
8+
9+
.btn {
10+
background: transparent;
11+
border: none;
12+
background-color: rgb(216, 34, 74);
13+
color: #fff;
14+
padding: 10px 20px;
15+
cursor: pointer;
16+
}
17+
18+
/* JavaScript */
19+
.cats {
20+
display: flex;
21+
justify-content: center;
22+
align-items: center;
23+
height: 100%;
24+
}
25+
26+
.random_cats {
27+
width: 40%;
28+
}
29+
30+
img {
31+
max-width: 700px;
32+
height: 30rem;
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /