-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
98 lines (88 loc) · 2.1 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.wp-animate-block {
opacity: 0;
transform: translateY(20px);
transition: opacity 1s ease, transform 1s ease;
}
.wp-animate-block.visible {
opacity: 1;
transform: translateY(0);
}
/* Additional styling for demonstration purposes */
body {
height: 200vh;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.wp-animate-block {
width: 100%;
height: 200px;
margin-bottom: 20px;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
font-weight: bold;
color: white;
}
.wp-animate-block:nth-child(odd) {
background-color: #3498db;
}
.wp-animate-block:nth-child(even) {
background-color: #e74c3c;
}
</style>
<title>Scroll Animation Example</title>
</head>
<body>
<div class="wp-animate-block">
Block 1
</div>
<div class="wp-animate-block">
Block 2
</div>
<div class="wp-animate-block">
Block 3
</div>
<div class="wp-animate-block">
Block 4
</div>
<div class="wp-animate-block">
Block 5
</div>
<script>
// Function to check if an element is in the viewport
function isElementInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
// Function to handle scroll event
function handleScroll() {
const elementsToAnimate = document.querySelectorAll('.wp-animate-block');
elementsToAnimate.forEach(element => {
if (isElementInViewport(element)) {
element.classList.add('visible');
}
});
}
// Attach scroll event listener
window.addEventListener('scroll', handleScroll);
// Trigger initial check in case some elements are already in viewport on page load
handleScroll();
</script>
</body>
</html>