-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrolltrigger.html
221 lines (185 loc) · 8.78 KB
/
scrolltrigger.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ScrollTrigger Background Change with Pinning</title>
<!-- bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="row">
<div class="col-12">
<h1>heading</h1>
<div class="scrolly">
<style>
.scrolly {
width: 100%;
position: relative;
font-family: Arial, sans-serif;
}
.scrolly * {
box-sizing: border-box;
}
.scrolly-bg {
position: absolute;
top: 0;
left: 0;
z-index: -1;
width: 100%;
height: 100%;
background-color: #f8f8f8;
}
.scrolly .section {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 2em;
transition: background-color 0.5s ease;
position: relative;
}
.scrolly .background {
position: absolute;
top: 50vh;
left: 50%;
transform: translate(-50%, -50%);
width: 1000px;
max-width: 100%;
aspect-ratio: 16 / 9;
z-index: -1;
overflow: hidden;
}
.scrolly .background.fixed {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.scrolly #bgImage {
width: 100%;
height: 100%;
object-fit: cover;
transform-origin: center center;
}
.scrolly .slide-inner {
display: inline-block;
background-color: #000;
}
</style>
<div class="scrolly-bg"></div>
<div class="background" id="background">
<img src="img/toilets2.jpg" alt="Background Image" id="bgImage">
</div>
<div class="sections-container">
<div class="section section1" data-background="img/toilets2.jpg">
<div class="slide-inner">Slide 1</div>
</div>
<div class="section section2" data-background="img/toilets1.jpg">
<div class="slide-inner">Slide 2</div>
</div>
<div class="section section4" data-background="img/map-base.jpg">
<div class="slide-inner">Slide 3</div>
</div>
<div class="section section5" data-background="img/map.jpg"></div>
<div class="section section6" data-background="img/map-base.jpg">
<div class="slide-inner">Slide 4</div>
</div>
<div class="section section7" data-background="img/pit-latrine1.jpg">
<div class="slide-inner">Slide 5</div>
</div>
<div class="section section8" data-background="img/toilets1.jpg">
<div class="slide-inner">Slide 6</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/ScrollTrigger.min.js"></script>
<script>
function updateBackgroundImage(src) {
const bgImage = document.getElementById('bgImage');
bgImage.src = src;
bgImage.style.transform = 'scale(1)';
}
window.addEventListener('load', () => {
const scrolly = document.querySelector('.scrolly');
const sectionsContainer = document.querySelector('.sections-container');
const sections = document.querySelectorAll('.section');
let contentHeight = 0;
sections.forEach(section => {
contentHeight += section.offsetHeight;
});
const scrolly_after = document.querySelector('.scrolly::after');
scrolly_after.style.height = `${contentHeight}px`;
});
gsap.registerPlugin(ScrollTrigger);
document.querySelectorAll('.section').forEach((section, index) => {
const background = section.getAttribute('data-background');
ScrollTrigger.create({
trigger: section,
start: 'top 50%',
onEnter: () => {
updateBackgroundImage(background);
},
onLeaveBack: () => {
const prevSection = document.querySelectorAll('.section')[index - 1];
const prevBackground = prevSection ? prevSection.getAttribute('data-background') : background;
updateBackgroundImage(prevBackground);
}
});
});
gsap.registerPlugin(ScrollTrigger);
const background = document.querySelector('.background');
ScrollTrigger.create({
trigger: '.section1',
start: 'top top',
endTrigger: '.section8',
end: 'bottom bottom',
toggleClass: { targets: background, className: 'fixed' },
});
// Pin Element
ScrollTrigger.create({
trigger: '.section4 .slide-inner',
pin: true,
start: 'center center',
endTrigger: '.section5',
end: 'top top',
pinSpacing: false
});
gsap.fromTo(
"#bgImage",
{ scale: 1 },
{
scale: 1.5,
ease: "none",
scrollTrigger: {
trigger: ".section4",
start: "top center",
end: "bottom center",
scrub: true,
},
}
);
gsap.fromTo(
"#bgImage",
{ scale: 1.5 },
{
scale: 1,
ease: "none",
scrollTrigger: {
trigger: ".section5",
start: "top bottom",
end: "bottom top",
scrub: true,
},
}
);
</script>
</div>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus laudantium impedit mollitia nostrum debitis, optio minima culpa tempora iusto, pariatur, unde harum? Asperiores officiis ex ad assumenda, eos dolorem aperiam?</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus laudantium impedit mollitia nostrum debitis, optio minima culpa tempora iusto, pariatur, unde harum? Asperiores officiis ex ad assumenda, eos dolorem aperiam?</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus laudantium impedit mollitia nostrum debitis, optio minima culpa tempora iusto, pariatur, unde harum? Asperiores officiis ex ad assumenda, eos dolorem aperiam?</p>
</div>
</div>
</body>
</html>