/
index.html
78 lines (74 loc) · 2.92 KB
/
index.html
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
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>View Transitions with Vanilla JS</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<h1>Animal Kingdom</h1>
<div class="animals__list">
<div class="animal__container">
<img src="./dog.jpg" alt="Dog" />
<div class="animal__text">
<p>
<strong> Dogs:</strong> Dogs are known for their strong sense of
smell, and they have up to 300 million olfactory receptors in their
noses, compared to about 5-6 million in humans. This remarkable
sense of smell allows them to detect scents, track trails, and even
identify medical conditions in some cases.
</p>
<button class="show__cat">Show Cat</button>
</div>
</div>
<div class="animal__container hidden">
<img src="./cat.jpg" alt="Cat" />
<div class="animal__text">
<p>
<strong> Cats:</strong> Cats have retractable claws, which is a
unique feature that sets them apart from many other animals. These
sharp claws are useful for hunting, climbing, and defending
themselves. When a cat is at rest or walking, their claws are
retracted to keep them sharp, but they can extend them when needed
for various tasks.
</p>
<button class="show__dog">Show Dog</button>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const showCatButton = document.querySelector(".show__cat");
const showDogButton = document.querySelector(".show__dog");
const dogContainer = document.querySelector(
".animal__container:not(.hidden)"
);
const catContainer = document.querySelector(
".animal__container.hidden"
);
// Function to handle the transition
function switchAnimal(showElement, hideElement) {
if (document.startViewTransition) {
document.startViewTransition(() => {
hideElement.classList.add("hidden");
showElement.classList.remove("hidden");
});
} else {
// Fallback if View Transitions API is not supported
hideElement.classList.add("hidden");
showElement.classList.remove("hidden");
}
}
// Event listener for the 'Show Cat' button
showCatButton.addEventListener("click", () => {
switchAnimal(catContainer, dogContainer);
});
// Event listener for the 'Show Dog' button
showDogButton.addEventListener("click", () => {
switchAnimal(dogContainer, catContainer);
});
});
</script>
</body>
</html>