-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.txt
177 lines (144 loc) · 4.99 KB
/
scroll.txt
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Change Color on Scroll</title>
<style>
body {
background-color:white;
transition: all 1s ease-in;
padding: 50px;
color: grey;
font-family: "Century Gothic", sans-serif;
line-height: 32px;
font-size: 1.5em;
}
h1 {
font-family: Elephant;
}
.colorOne {
background-color: #9BC53D;
color: #000;
}
.colorTwo {
background-color: #FFF;
color: #000;
}
#mainContent {
width: 420px;
margin: 0 auto;
}
#mainContent p {
padding: 20px;
}
#mainContent #firstBox {
font-weight: bold;
transform: translate3d(-30px, 0, 0);
transition: all .5s ease-out;
opacity: 0;
}
#mainContent #firstBox.active {
background-color: #333;
color: #FFF;
transform: translate3d(0, 0, 0);
opacity: 1;
}
#mainContent #secondBox {
transition: all .2s ease-in-out;
transform: translate3d(0, 30px, 0);
opacity: 0;
}
#mainContent #secondBox.active {
background-color: #1581AF;
color: #FFF;
transform: translate3d(0, 0, 0);
opacity: 1;
}
#mainContent ol li {
padding-left: 7px;
margin-bottom: 15px;
transition: all .2s ease-in-out;
transform: translate3d(20px, 0, 0);
opacity: 0;
}
#mainContent ol li.active {
transform: translate3d(0px, 0, 0);
opacity: 1;
}
</style>
</head>
<body>
<div id="mainContent">
<h1>Scroll Down</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis massa a arcu efficitur suscipit vehicula et risus.</p>
<ol id="myList">
<li>Nam sagittis est non enim ultrices elementum. </li>
<li>Sed id ligula sed mi tempor ornare.</li>
<li>Aenean feugiat risus eget sagittis volutpat. Proin quis orci a metus lacinia auctor eget id nisi.</li>
<li>Donec pulvinar nunc feugiat semper consequat.</li>
<li>Etiam cursus justo eget libero gravida, nec faucibus mauris posuere.</li>
<li>In nec sem id libero egestas cursus vel a urna.</li>
<li>Fusce pulvinar arcu eu lobortis egestas. Maecenas eleifend felis ut urna consectetur, et pellentesque mi molestie.</li>
<li>Aliquam ut felis venenatis, dapibus ante non, gravida nulla.</li>
<li>Donec consectetur quam in urna commodo, sed aliquet metus vehicula.</li>
<li>Mauris eget est sit amet felis eleifend sagittis non id nulla.</li>
</ol>
<p id="firstBox"><img src ="/images/2.jpg" alt="img"> </p>
<p>Nunc blandit varius sapien quis ultrices. Vestibulum et consequat augue. Pellentesque et maximus nisl, sit amet dictum ante.</p>
<p id="secondBox">Nullam magna augue, consequat eu augue ut, volutpat fringilla est. Ut commodo ac magna vulputate dictum.</p>
</div>
<script>
var isScrolling = false;
window.addEventListener("scroll", throttleScroll, false);
function throttleScroll(e) {
if (isScrolling == false) {
window.requestAnimationFrame(function() {
scrolling(e);
isScrolling = false;
});
}
isScrolling = true;
}
document.addEventListener("DOMContentLoaded", scrolling, false);
var listItems = document.querySelectorAll("#mainContent ol li");
var firstBox = document.querySelector("#firstBox");
var secondBox = document.querySelector("#secondBox");
function scrolling(e) {
if (isPartiallyVisible(firstBox)) {
firstBox.classList.add("active");
document.body.classList.add("colorOne");
document.body.classList.remove("colorTwo");
} else {
document.body.classList.remove("colorOne");
document.body.classList.remove("colorTwo");
}
if (isFullyVisible(secondBox)) {
secondBox.classList.add("active");
document.body.classList.add("colorTwo");
document.body.classList.remove("colorOne");
}
for (var i = 0; i < listItems.length; i++) {
var listItem = listItems[i];
if (isPartiallyVisible(listItem)) {
listItem.classList.add("active");
} else {
listItem.classList.remove("active");
}
}
}
function isPartiallyVisible(el) {
var elementBoundary = el.getBoundingClientRect();
var top = elementBoundary.top;
var bottom = elementBoundary.bottom;
var height = elementBoundary.height;
return ((top + height >= 0) && (height + window.innerHeight >= bottom));
}
function isFullyVisible(el) {
var elementBoundary = el.getBoundingClientRect();
var top = elementBoundary.top;
var bottom = elementBoundary.bottom;
return ((top >= 0) && (bottom <= window.innerHeight));
}
</script>
</body>
</html>