Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,320 --> 00:00:03,480
Hello there, and welcome back
2
00:00:03,480 --> 00:00:06,180
to implementing our last effect,
3
00:00:06,180 --> 00:00:08,930
which has also the coolest effect of all.
4
00:00:08,930 --> 00:00:13,073
And this one is going to be a so-called sticky navigation.
5
00:00:14,520 --> 00:00:17,770
So as I had mentioned, by the end of the last video,
6
00:00:17,770 --> 00:00:20,307
a sticky navigation bar is basically that
7
00:00:20,307 --> 00:00:24,480
this navigation here stays on top as we scroll
8
00:00:24,480 --> 00:00:26,123
across our entire page.
9
00:00:26,970 --> 00:00:31,000
So let's say we are here or really anywhere on the page,
10
00:00:31,000 --> 00:00:33,470
and then we always have our navigation bar here,
11
00:00:33,470 --> 00:00:35,560
nicely sitting at the top.
12
00:00:35,560 --> 00:00:37,660
And once more, I'm pretty sure
13
00:00:37,660 --> 00:00:40,280
that you have seen a navigation bar like this
14
00:00:40,280 --> 00:00:42,130
many, many times before
15
00:00:42,130 --> 00:00:44,670
because it is actually really helpful
16
00:00:44,670 --> 00:00:46,533
to have that on our page.
17
00:00:47,960 --> 00:00:50,880
All right, now let's start by actually creating
18
00:00:50,880 --> 00:00:52,530
the style for that
19
00:00:52,530 --> 00:00:55,804
because the JavaScript part is basically only then
20
00:00:55,804 --> 00:00:58,300
adding and removing that style.
21
00:00:58,300 --> 00:01:00,675
So basically similar to what we did before,
22
00:01:00,675 --> 00:01:03,323
also with the mobile navigation.
23
00:01:04,840 --> 00:01:06,110
All right.
24
00:01:06,110 --> 00:01:10,030
So again, I'm going to start by creating a new class,
25
00:01:10,030 --> 00:01:13,303
which we will then style to make the navigation sticky.
26
00:01:14,860 --> 00:01:19,860
So let's do that in the style.css file,
27
00:01:20,690 --> 00:01:22,913
which is where our navigation should be.
28
00:01:25,290 --> 00:01:27,610
So here we have the mobile navigation,
29
00:01:27,610 --> 00:01:31,783
and now let's add the sticky navigation.
30
00:01:37,130 --> 00:01:41,160
So let's call it sticky, which makes sense.
31
00:01:41,160 --> 00:01:44,410
And so now there is a new type of value
32
00:01:44,410 --> 00:01:47,430
that we can specify for position.
33
00:01:47,430 --> 00:01:50,490
So that's something that we hadn't done before.
34
00:01:50,490 --> 00:01:54,300
So all we did until this point was position relative
35
00:01:54,300 --> 00:01:56,300
and position absolute,
36
00:01:56,300 --> 00:02:00,330
but now what we actually want is position fixed.
37
00:02:00,330 --> 00:02:02,660
And so this will then fix this element,
38
00:02:02,660 --> 00:02:04,410
basically in the viewport,
39
00:02:04,410 --> 00:02:06,743
and we'll not move it as we scroll.
40
00:02:07,870 --> 00:02:12,870
So let's put it at top zero and bottom zero,
41
00:02:14,440 --> 00:02:17,614
and also let's make sure that it will fill
42
00:02:17,614 --> 00:02:20,190
the entire viewport.
43
00:02:20,190 --> 00:02:21,840
And just so we see it,
44
00:02:21,840 --> 00:02:23,850
let's give it a white background color
45
00:02:28,450 --> 00:02:29,943
and to let see what happens.
46
00:02:32,800 --> 00:02:34,740
Well, of course nothing happened
47
00:02:34,740 --> 00:02:38,150
because we didn't even add this here anywhere.
48
00:02:38,150 --> 00:02:40,610
So let's add this actually to the entire header
49
00:02:43,920 --> 00:02:47,320
because of course the header is the entire part,
50
00:02:47,320 --> 00:02:51,320
which contains the logo and also the navigation.
51
00:02:51,320 --> 00:02:53,090
So right now all of that,
52
00:02:53,090 --> 00:02:56,373
so that entire bar should stay at the top.
53
00:02:58,550 --> 00:03:01,680
Right, and there we can already see it.
54
00:03:01,680 --> 00:03:03,150
Let me just reload.
55
00:03:03,150 --> 00:03:05,093
And so there it is.
56
00:03:06,350 --> 00:03:10,013
So this is kind of already what we are looking for.
57
00:03:10,986 --> 00:03:15,010
There is just as problem that we gave these images here
58
00:03:15,010 --> 00:03:16,803
a higher Z index value.
59
00:03:18,160 --> 00:03:21,223
So let's inspect that.
60
00:03:22,970 --> 00:03:27,970
So, well, maybe this entire thing,
61
00:03:30,500 --> 00:03:32,293
or maybe just to one of these.
62
00:03:33,590 --> 00:03:36,530
Well, in any way, what we need to fix here
63
00:03:36,530 --> 00:03:40,027
is the Z index value of this entire header,
64
00:03:40,027 --> 00:03:42,893
so off the sticky class, right?
65
00:03:43,760 --> 00:03:47,463
So let's again add some really high value here,
66
00:03:48,590 --> 00:03:50,540
so Z index.
67
00:03:50,540 --> 00:03:53,994
And remember how we used this one here for the button.
68
00:03:53,994 --> 00:03:57,419
Now, the button, we will want to always stay on top
69
00:03:57,419 --> 00:04:02,419
of everything, and so let's just do 999 for this one here.
70
00:04:02,887 --> 00:04:04,050
All right.
71
00:04:04,050 --> 00:04:06,720
Also we can use the same background color
72
00:04:06,720 --> 00:04:08,910
with some transparency that we used
73
00:04:08,910 --> 00:04:10,443
for the mobile navigation.
74
00:04:11,444 --> 00:04:13,323
So that's in the queries.
75
00:04:15,320 --> 00:04:17,093
So yeah, just this one here.
76
00:04:20,732 --> 00:04:21,913
Okay.
77
00:04:23,390 --> 00:04:26,363
Give it a save, and beautiful.
78
00:04:27,890 --> 00:04:29,890
So that looks really, really nice,
79
00:04:29,890 --> 00:04:34,860
but we can see that some space has been lost here before.
80
00:04:34,860 --> 00:04:37,220
So that is because the header,
81
00:04:37,220 --> 00:04:40,910
so this entire element, has been taken out of the flow,
82
00:04:40,910 --> 00:04:43,083
just like with position absolute.
83
00:04:43,940 --> 00:04:46,690
So remember that when we position an element
84
00:04:46,690 --> 00:04:48,200
with position absolute,
85
00:04:48,200 --> 00:04:51,130
it is taken out of the flow of the page,
86
00:04:51,130 --> 00:04:54,490
and therefore, it is as if it doesn't even exist.
87
00:04:54,490 --> 00:04:56,290
And so then the rest of the element
88
00:04:56,290 --> 00:04:58,620
fill the remaining space.
89
00:04:58,620 --> 00:05:02,107
And so with position fixed, the exact same thing happens.
90
00:05:02,107 --> 00:05:07,107
And so now basically, the page starts here at this element.
91
00:05:08,730 --> 00:05:11,003
So at the hero section.
92
00:05:13,800 --> 00:05:17,620
So you'll see that it now starts right there at the top.
93
00:05:17,620 --> 00:05:20,370
So we will have to fix that at some point,
94
00:05:20,370 --> 00:05:23,190
but for now, let's actually leave it like this
95
00:05:23,190 --> 00:05:26,513
so that you can later see the problem that it causes.
96
00:05:27,370 --> 00:05:30,760
Now, I also want to add some shadow here,
97
00:05:30,760 --> 00:05:34,174
and we also need to make this a bit smaller.
98
00:05:34,174 --> 00:05:37,596
So like this, it takes up a little bit too big
99
00:05:37,596 --> 00:05:39,670
of the screen, I would say.
100
00:05:39,670 --> 00:05:41,863
So we need to make all of this smaller.
101
00:05:43,190 --> 00:05:45,590
And actually I wanted to give this navigation bar
102
00:05:45,590 --> 00:05:46,763
a fixed height.
103
00:05:47,660 --> 00:05:48,803
So right here.
104
00:05:50,690 --> 00:05:53,313
So let's say 80 pixels, so 8rem,
105
00:05:54,150 --> 00:05:55,900
and then we need to remove the padding
106
00:05:55,900 --> 00:05:57,643
from the top and the bottom.
107
00:05:58,880 --> 00:06:03,170
So we can say padding-top zero
108
00:06:04,200 --> 00:06:07,460
and padding-bottom zero as well.
109
00:06:07,460 --> 00:06:09,220
And so with this, the left and right,
110
00:06:09,220 --> 00:06:11,023
they simply stay unchanged.
111
00:06:12,290 --> 00:06:13,300
Okay.
112
00:06:13,300 --> 00:06:16,040
And I will tell you a bit later in the JavaScript part,
113
00:06:16,040 --> 00:06:19,263
why it is important to have this fixed height.
114
00:06:19,263 --> 00:06:20,420
Okay.
115
00:06:20,420 --> 00:06:24,760
But for now let's add that box shadow
116
00:06:24,760 --> 00:06:26,033
that I was talking about.
117
00:06:26,930 --> 00:06:29,780
And something just very, very subtle.
118
00:06:29,780 --> 00:06:32,610
So horizontally, as always, nothing.
119
00:06:32,610 --> 00:06:36,702
Then just 1.2 REM and then the spread.
120
00:06:36,702 --> 00:06:39,163
Let's give it like 3.2.
121
00:06:41,550 --> 00:06:44,160
And then here, our black color can be something
122
00:06:44,160 --> 00:06:47,700
really subtle, and even less than this.
123
00:06:47,700 --> 00:06:49,373
Let's say just 3%.
124
00:06:54,662 --> 00:06:56,090
Okay.
125
00:06:56,090 --> 00:06:57,625
That looks really nice.
126
00:06:57,625 --> 00:07:00,757
So now it's not too high anymore I would say.
127
00:07:00,757 --> 00:07:04,126
And it still is everything vertically centered here
128
00:07:04,126 --> 00:07:06,123
because of flexbox.
129
00:07:08,020 --> 00:07:10,850
But of course, we don't always want this navigation
130
00:07:10,850 --> 00:07:13,033
to be sticky like this.
131
00:07:13,033 --> 00:07:18,033
So we want us to start basically after this hero section.
132
00:07:19,770 --> 00:07:22,900
Right, so basically, when we start the content itself,
133
00:07:22,900 --> 00:07:25,650
then that's a good time to make it sticky.
134
00:07:25,650 --> 00:07:27,400
So basically right here,
135
00:07:27,400 --> 00:07:30,118
after we leave the hero section behind.
136
00:07:30,118 --> 00:07:34,990
So once that scrolls out of the viewport, right.
137
00:07:34,990 --> 00:07:39,170
And so now we can use JavaScript to do exactly that.
138
00:07:39,170 --> 00:07:42,110
So basically, to add the sticky class,
139
00:07:42,110 --> 00:07:46,080
as soon as the hero section is no longer visible.
140
00:07:46,080 --> 00:07:46,930
All right.
141
00:07:46,930 --> 00:07:50,620
So let's do that, remove it from here.
142
00:07:50,620 --> 00:07:54,730
And does this going to be the most tricky part of all
143
00:07:54,730 --> 00:07:56,410
that we're going to do here.
144
00:07:56,410 --> 00:07:58,230
And so from now on, if you want,
145
00:07:58,230 --> 00:08:01,810
you can just skip the rest of this video
146
00:08:01,810 --> 00:08:04,940
and simply grab the code from the final files
147
00:08:04,940 --> 00:08:06,053
of the section.
148
00:08:07,050 --> 00:08:07,883
All right.
149
00:08:07,883 --> 00:08:10,932
But again, if you're interested in how this works,
150
00:08:10,932 --> 00:08:14,633
then just stick around just like the sticky navigation.
151
00:08:16,758 --> 00:08:17,780
Okay.
152
00:08:17,780 --> 00:08:22,780
And the way we will do this is by using a very modern way,
153
00:08:23,210 --> 00:08:25,742
which is called the intersection observer.
154
00:08:25,742 --> 00:08:28,780
So there would be multiple ways of doing this,
155
00:08:28,780 --> 00:08:31,300
but this is the most performant way,
156
00:08:31,300 --> 00:08:34,823
and also the most modern and the best way I would say.
157
00:08:35,850 --> 00:08:37,793
So intersection observer.
158
00:08:39,440 --> 00:08:42,320
And so here, we now need to specify two things.
159
00:08:42,320 --> 00:08:44,583
First, what we want to happen.
160
00:08:45,490 --> 00:08:47,773
So here we will define a function,
161
00:08:49,330 --> 00:08:52,800
and then second, some options here.
162
00:08:52,800 --> 00:08:55,850
But I will not yet specify these two things.
163
00:08:55,850 --> 00:08:58,480
So not the function and not the options,
164
00:08:58,480 --> 00:09:01,450
but instead I will start by saving
165
00:09:01,450 --> 00:09:04,973
this new intersection observer into a variable.
166
00:09:06,670 --> 00:09:10,970
So calling this one here, the observer, simply
167
00:09:11,940 --> 00:09:16,500
because now we need to actually do observer.observe.
168
00:09:19,210 --> 00:09:21,900
So this sounds very confusing, I know,
169
00:09:21,900 --> 00:09:24,160
but just bear with me here.
170
00:09:24,160 --> 00:09:26,370
So we just need to write a little bit of code
171
00:09:26,370 --> 00:09:28,443
and then things will make more sense.
172
00:09:30,150 --> 00:09:31,210
Okay.
173
00:09:31,210 --> 00:09:34,280
So this one here could have any other name.
174
00:09:34,280 --> 00:09:37,610
Let me call it OBS maybe as an abbreviation,
175
00:09:37,610 --> 00:09:39,833
just to make it less confusing with the OBS.
176
00:09:41,949 --> 00:09:43,410
Okay.
177
00:09:43,410 --> 00:09:47,410
So here we now need to basically observe some element
178
00:09:47,410 --> 00:09:52,030
in the HTML using this observer that we specify here.
179
00:09:52,030 --> 00:09:55,470
And so the element that we want to observe is, in this case,
180
00:09:55,470 --> 00:10:00,470
the hero section, so section hero,
181
00:10:00,470 --> 00:10:03,380
and this is the section that we want to observe
182
00:10:03,380 --> 00:10:06,550
because we will want to make the navigation sticky
183
00:10:06,550 --> 00:10:11,070
as soon as this section here moves out of the viewport.
184
00:10:11,070 --> 00:10:14,213
So that's what we also said before, right.
185
00:10:15,070 --> 00:10:17,908
So basically as soon as this hero section here
186
00:10:17,908 --> 00:10:21,345
moves out of the viewport is to when we want to make
187
00:10:21,345 --> 00:10:23,440
the navigation sticky.
188
00:10:23,440 --> 00:10:25,660
And so that's why that is the element
189
00:10:25,660 --> 00:10:27,060
that we're going to observe.
190
00:10:28,800 --> 00:10:31,270
All right, so let me select that
191
00:10:33,750 --> 00:10:36,843
and create that here first maybe.
192
00:10:38,910 --> 00:10:43,910
So section hero element and then document.
193
00:10:45,520 --> 00:10:47,930
So as always, just selecting it
194
00:10:47,930 --> 00:10:50,283
using document.query selector.
195
00:10:52,550 --> 00:10:57,090
Okay, and so this is the element that we want to observe.
196
00:10:57,090 --> 00:10:58,503
So section hero.
197
00:11:00,570 --> 00:11:02,600
So this was the first part.
198
00:11:02,600 --> 00:11:06,124
So using this observer here to observe a certain element,
199
00:11:06,124 --> 00:11:09,770
which in this case is the section,
200
00:11:09,770 --> 00:11:12,800
and now we need to define the options here.
201
00:11:12,800 --> 00:11:16,193
So for that, we have this so-called object here.
202
00:11:17,177 --> 00:11:18,960
And the first thing is the root.
203
00:11:18,960 --> 00:11:22,800
And the root is basically simply where this element
204
00:11:22,800 --> 00:11:25,600
should be appearing or not.
205
00:11:25,600 --> 00:11:29,970
So that sounds confusing, but it's really not.
206
00:11:29,970 --> 00:11:32,000
So we're just setting it to null here.
207
00:11:32,000 --> 00:11:34,770
And what that means is that we will observe
208
00:11:34,770 --> 00:11:38,270
this hero section inside of the viewport.
209
00:11:38,270 --> 00:11:42,750
So basically, as it moves through the viewport, all right.
210
00:11:42,750 --> 00:11:45,890
So usually that's always what we do here,
211
00:11:45,890 --> 00:11:48,530
but we could also observe some elements
212
00:11:48,530 --> 00:11:51,450
basically scrolling inside of something else,
213
00:11:51,450 --> 00:11:54,363
but this is most helpful just in the viewport.
214
00:11:55,620 --> 00:11:59,163
So basically, inside of the entire browser window.
215
00:12:00,530 --> 00:12:04,320
Okay, next we need to set a threshold.
216
00:12:04,320 --> 00:12:06,700
and this one we set at zero.
217
00:12:06,700 --> 00:12:10,910
And zero basically means that this will fire.
218
00:12:10,910 --> 00:12:14,750
So basically, it means that we will have like an event
219
00:12:14,750 --> 00:12:19,750
as soon as 0% of the hero section is inside of the viewport.
220
00:12:23,610 --> 00:12:26,610
So again, a little bit confusing, I know.
221
00:12:26,610 --> 00:12:30,420
And here we could actually specify multiple values.
222
00:12:30,420 --> 00:12:32,380
So it could also be one,
223
00:12:32,380 --> 00:12:33,840
and then we would get the event
224
00:12:33,840 --> 00:12:36,726
whenever the hero section is completely
225
00:12:36,726 --> 00:12:39,713
inside of the viewport.
226
00:12:40,700 --> 00:12:43,230
And so that's why we set to null here.
227
00:12:43,230 --> 00:12:45,073
So null means the viewport.
228
00:12:46,530 --> 00:12:48,750
But again, we want it to be zero
229
00:12:48,750 --> 00:12:52,715
because we want to get an event as soon as the hero section
230
00:12:52,715 --> 00:12:56,530
moves out completely of the viewport.
231
00:12:56,530 --> 00:13:01,083
So again, that is this event right here, so this situation.
232
00:13:03,440 --> 00:13:06,125
Okay, and for now that is it.
233
00:13:06,125 --> 00:13:08,468
And so now we need to actually specify
234
00:13:08,468 --> 00:13:12,400
what we want to happen when this is the case,
235
00:13:12,400 --> 00:13:17,400
so whenever we have the threshold of zero in the viewport.
236
00:13:19,230 --> 00:13:20,800
Okay.
237
00:13:20,800 --> 00:13:23,210
Let's move this into another line here.
238
00:13:23,210 --> 00:13:25,200
And this one as well.
239
00:13:25,200 --> 00:13:28,150
And here we get access to basically
240
00:13:28,150 --> 00:13:30,853
something called an array of entries,
241
00:13:33,530 --> 00:13:35,070
and there's going to be one entries
242
00:13:35,070 --> 00:13:37,200
for each threshold value.
243
00:13:37,200 --> 00:13:39,260
But here we just have one,
244
00:13:39,260 --> 00:13:44,103
and so we can just say that const ent is equal
245
00:13:47,350 --> 00:13:49,350
the very first entry,
246
00:13:49,350 --> 00:13:50,713
and so that is zero.
247
00:13:51,710 --> 00:13:54,160
So this is, again, something called an array,
248
00:13:54,160 --> 00:13:56,640
which has a collection of multiple elements,
249
00:13:56,640 --> 00:13:58,380
but this one has just one.
250
00:13:58,380 --> 00:14:02,630
And so the element number one is called the number zero,
251
00:14:02,630 --> 00:14:04,310
in fact, in JavaScript.
252
00:14:04,310 --> 00:14:06,360
And so here we are taking that out
253
00:14:06,360 --> 00:14:08,763
and storing it into a new variable.
254
00:14:10,770 --> 00:14:15,560
And so now to start working in this function,
255
00:14:15,560 --> 00:14:19,243
let's take a look actually at this entry here.
256
00:14:20,770 --> 00:14:22,460
So in the console, basically.
257
00:14:22,460 --> 00:14:25,240
And so if we can see something in the console,
258
00:14:25,240 --> 00:14:28,940
then it means that or main code is already working.
259
00:14:28,940 --> 00:14:32,023
So especially these options that we specified here.
260
00:14:33,037 --> 00:14:34,363
Okay.
261
00:14:35,330 --> 00:14:38,630
So let's come to the console, and let's reload,
262
00:14:38,630 --> 00:14:41,323
but I can already see something happening here.
263
00:14:45,050 --> 00:14:47,730
So here we get to now the intersection ratio,
264
00:14:47,730 --> 00:14:52,730
which is basically 62%, all right,
265
00:14:52,880 --> 00:14:55,510
and we get is intersecting.
266
00:14:55,510 --> 00:14:58,033
So these are the two important values.
267
00:15:01,510 --> 00:15:04,680
So this first event here always happens, no matter what,
268
00:15:04,680 --> 00:15:07,010
but now let's wait for it,
269
00:15:07,010 --> 00:15:09,388
and I will actually clear the output here.
270
00:15:09,388 --> 00:15:12,360
And so now, as soon as we move out,
271
00:15:12,360 --> 00:15:16,200
so as soon as this section here moves out of the viewport,
272
00:15:16,200 --> 00:15:18,850
we should get a new event there, right.
273
00:15:18,850 --> 00:15:21,703
So let's wait for it, and there it is.
274
00:15:23,280 --> 00:15:25,870
So now the intersection ratio is zero
275
00:15:25,870 --> 00:15:28,123
and is intersecting is false.
276
00:15:29,340 --> 00:15:33,883
All right, and so we now need to test for this, basically.
277
00:15:34,920 --> 00:15:39,143
So we will want, this is intersecting to be equal to false.
278
00:15:40,830 --> 00:15:42,883
OkaY, so let's write that.
279
00:15:44,384 --> 00:15:49,100
So if ent, which is that event that we just saw,
280
00:15:49,100 --> 00:15:52,220
so which stands here for entry.isintersecting
281
00:15:57,380 --> 00:15:59,820
is equal to false,
282
00:15:59,820 --> 00:16:02,713
then we want to add our sticky class.
283
00:16:05,220 --> 00:16:07,800
So let's select the header here,
284
00:16:07,800 --> 00:16:10,510
and I will do it here all in one go this time
285
00:16:10,510 --> 00:16:13,863
because actually we will have to change this in a minute.
286
00:16:16,264 --> 00:16:18,190
Oh yeah, right here.
287
00:16:18,190 --> 00:16:21,073
So document.querySelector.header.
288
00:16:26,730 --> 00:16:29,816
And so before we always did it in a separate variable,
289
00:16:29,816 --> 00:16:32,480
so we stored this into a variable,
290
00:16:32,480 --> 00:16:36,920
and then on that variable, we did the dot, right.
291
00:16:36,920 --> 00:16:40,440
But we can do it all in one go, so all directly.
292
00:16:40,440 --> 00:16:43,849
And so let's grab the class list
293
00:16:43,849 --> 00:16:48,849
and then we want to add the sticky class here.
294
00:16:51,150 --> 00:16:53,160
So this time we are not using toggle,
295
00:16:53,160 --> 00:16:55,113
but instead simply adding it.
296
00:16:56,620 --> 00:16:58,113
So just called sticky.
297
00:16:59,670 --> 00:17:02,350
Now there is a better way of writing this.
298
00:17:02,350 --> 00:17:06,880
So instead of this, we would usually just this, all right.
299
00:17:07,970 --> 00:17:10,140
But I'll actually leave it like this
300
00:17:10,140 --> 00:17:13,090
to make the code a little bit easier for you to understand.
301
00:17:14,530 --> 00:17:17,533
All right, and so this would actually,
302
00:17:18,580 --> 00:17:20,313
let's reload here once again,
303
00:17:21,890 --> 00:17:25,873
and well, nothing happened.
304
00:17:28,180 --> 00:17:31,253
So, well, let's,
305
00:17:34,995 --> 00:17:37,040
so let's add these curly braces here
306
00:17:37,040 --> 00:17:41,140
to create a block of code so we can have multiple lines.
307
00:17:41,140 --> 00:17:44,730
And so I will only lock this event here
308
00:17:44,730 --> 00:17:48,790
so this entry, when this condition here is true.
309
00:17:48,790 --> 00:17:52,090
So in other words, when ent.intersection
310
00:17:52,090 --> 00:17:55,200
or isintersecting is false.
311
00:17:55,200 --> 00:17:58,572
And actually, I think that I did this wrong here,
312
00:17:58,572 --> 00:17:59,822
isIntersecting.
313
00:18:01,970 --> 00:18:03,770
So I think this is the correct name.
314
00:18:07,720 --> 00:18:10,163
Oh, and now it is actually already working.
315
00:18:12,790 --> 00:18:14,210
So let's see.
316
00:18:14,210 --> 00:18:16,493
So right now, nothing is happening.
317
00:18:17,920 --> 00:18:19,820
And actually let's move this back out.
318
00:18:21,725 --> 00:18:23,510
Okay, so I did this here
319
00:18:23,510 --> 00:18:26,193
because I thought there was a bigger problem.
320
00:18:28,090 --> 00:18:33,090
All right, so here we already have our first entry,
321
00:18:33,130 --> 00:18:36,720
but it has this percentage and it is intersecting.
322
00:18:36,720 --> 00:18:40,368
And so now the sticky class is not yet being added,
323
00:18:40,368 --> 00:18:42,183
but let's wait for it,
324
00:18:43,690 --> 00:18:46,893
and there it is, beautiful.
325
00:18:47,990 --> 00:18:51,765
Okay, now of course, we don't have yet a way of removing it,
326
00:18:51,765 --> 00:18:55,093
but we will take care of that in a minute.
327
00:18:56,420 --> 00:19:00,523
Now, what matters here is that we have this big jump.
328
00:19:02,560 --> 00:19:07,560
So watch closely what happens as the sticky class is added.
329
00:19:07,710 --> 00:19:10,223
So you will notice a big jump here in the page.
330
00:19:12,920 --> 00:19:15,010
So you saw that.
331
00:19:15,010 --> 00:19:17,980
So we basically skipped this entire part here.
332
00:19:17,980 --> 00:19:20,900
And the reason for that is basically
333
00:19:20,900 --> 00:19:22,520
what I explained to you before,
334
00:19:22,520 --> 00:19:25,850
which is the fact that this entire header here
335
00:19:25,850 --> 00:19:28,010
has now been taken out of the flow
336
00:19:28,010 --> 00:19:31,320
because it is now absolutely positioned.
337
00:19:31,320 --> 00:19:34,957
Okay, and so we need to fix that by basically compensating
338
00:19:34,957 --> 00:19:38,770
for this lost height and add some margin
339
00:19:38,770 --> 00:19:41,090
to the top of this header,
340
00:19:41,090 --> 00:19:43,188
so of this hero section, actually.
341
00:19:43,188 --> 00:19:45,870
So here, we now basically need to add
342
00:19:45,870 --> 00:19:49,520
exactly the same margin as is the original height
343
00:19:49,520 --> 00:19:51,330
of the header.
344
00:19:51,330 --> 00:19:54,583
So let's reload, and let's get that height.
345
00:19:56,010 --> 00:19:59,820
So I'll inspect it here, and you see that by default,
346
00:19:59,820 --> 00:20:01,625
it is 96 pixels.
347
00:20:01,625 --> 00:20:03,623
So that's 9.6rem.
348
00:20:04,960 --> 00:20:07,700
All right, and so now whenever the header class
349
00:20:07,700 --> 00:20:12,500
has been added, we want to add some margin to this one here,
350
00:20:12,500 --> 00:20:15,313
so a margin top to the section hero.
351
00:20:16,150 --> 00:20:16,983
All right.
352
00:20:18,590 --> 00:20:20,303
So let's do that here,
353
00:20:21,880 --> 00:20:23,420
right here.
354
00:20:23,420 --> 00:20:25,533
And so how can we actually do that?
355
00:20:27,060 --> 00:20:30,510
So let's first write to code here,
356
00:20:30,510 --> 00:20:32,323
but of course it will not work yet.
357
00:20:33,710 --> 00:20:36,903
So margin top of 9.6rem,
358
00:20:37,825 --> 00:20:42,540
but how does this relate now to the sticky class?
359
00:20:42,540 --> 00:20:45,290
I mean, we only want this to happen whenever
360
00:20:45,290 --> 00:20:49,593
basically the header has the sticky class, right.
361
00:20:51,170 --> 00:20:56,170
So let's make this here a smaller, right.
362
00:20:59,350 --> 00:21:03,080
So again, this style here should basically only apply
363
00:21:03,080 --> 00:21:07,550
whenever sticky is right here on the header, right.
364
00:21:07,550 --> 00:21:09,390
However, that is not possible
365
00:21:09,390 --> 00:21:13,053
because the header is not a parent of section hero.
366
00:21:14,100 --> 00:21:14,933
All right.
367
00:21:14,933 --> 00:21:18,300
And so instead, what we will have to do is to always add
368
00:21:18,300 --> 00:21:22,770
the sticky class, not to the header, but to the body.
369
00:21:22,770 --> 00:21:25,780
All right, and so then we can do this.
370
00:21:25,780 --> 00:21:28,840
So we will then apply all these styles
371
00:21:28,840 --> 00:21:30,180
actually to the header,
372
00:21:30,180 --> 00:21:33,663
but only if the sticky class is a parent of it.
373
00:21:35,270 --> 00:21:37,047
So just like this.
374
00:21:37,047 --> 00:21:39,370
And so then this is exactly what we did
375
00:21:39,370 --> 00:21:41,107
multiple times before,
376
00:21:41,107 --> 00:21:45,669
and in particular with the mobile navigation, right.
377
00:21:45,669 --> 00:21:50,669
So here we had this code where this style only applied
378
00:21:50,897 --> 00:21:54,927
to the main nav if enough open class was present
379
00:21:54,927 --> 00:21:57,340
on one of the parent elements,
380
00:21:57,340 --> 00:21:59,630
and so here we are doing the same.
381
00:21:59,630 --> 00:22:02,830
So we will apply all these styles here to the header
382
00:22:02,830 --> 00:22:06,910
if sticky is a one of its parent elements.
383
00:22:06,910 --> 00:22:09,550
And so that will then be the body.
384
00:22:09,550 --> 00:22:12,213
And so here we can then do the same.
385
00:22:15,100 --> 00:22:16,340
All right.
386
00:22:16,340 --> 00:22:19,950
And finally, then in our JavaScript,
387
00:22:19,950 --> 00:22:23,500
we also need to add the sticky, not to the header,
388
00:22:23,500 --> 00:22:24,893
but to the body.
389
00:22:27,300 --> 00:22:31,780
So that we do by saying document.body
390
00:22:31,780 --> 00:22:33,013
and yeah, that's it.
391
00:22:33,910 --> 00:22:38,910
So just like this, and let's reload manually here.
392
00:22:43,270 --> 00:22:45,870
And let's see if there is the same jump now,
393
00:22:45,870 --> 00:22:46,720
but it shouldn't.
394
00:22:47,600 --> 00:22:49,773
And indeed, we fixed that.
395
00:22:51,460 --> 00:22:55,930
So beautiful, you saw the sticky class being added here,
396
00:22:55,930 --> 00:22:58,313
and so it then should have added.
397
00:22:59,409 --> 00:23:02,580
Well, it's added all these tiles here to the header
398
00:23:02,580 --> 00:23:07,580
and then here to the section hero
399
00:23:08,290 --> 00:23:12,510
it will have added that margin, right,
400
00:23:12,510 --> 00:23:14,403
so that margin top of 9.6.
401
00:23:17,410 --> 00:23:18,590
All right.
402
00:23:18,590 --> 00:23:19,958
So, beautiful.
403
00:23:19,958 --> 00:23:23,453
And of course, if we take that sticky class away,
404
00:23:25,150 --> 00:23:26,853
then we go back to normal.
405
00:23:28,590 --> 00:23:30,120
So we are almost done here.
406
00:23:30,120 --> 00:23:32,390
Just two more things.
407
00:23:32,390 --> 00:23:36,403
So first, let's take a look again at our effect.
408
00:23:38,030 --> 00:23:41,033
So you see, or you will see,
409
00:23:42,600 --> 00:23:45,208
now that, of course, this sticky
410
00:23:45,208 --> 00:23:50,208
exactly as we moved out of the hero section, right.
411
00:23:51,250 --> 00:23:52,743
now, the problem that is that
412
00:23:52,743 --> 00:23:56,460
it will then overlap this first section here.
413
00:23:56,460 --> 00:23:58,910
And so that means basically that this happens
414
00:23:58,910 --> 00:24:00,333
a little bit too late.
415
00:24:01,230 --> 00:24:04,563
So actually we want it to happen kind of like here.
416
00:24:05,450 --> 00:24:07,403
So exactly here.
417
00:24:08,420 --> 00:24:09,253
All right.
418
00:24:09,253 --> 00:24:11,393
And so we can do that in our code.
419
00:24:13,030 --> 00:24:17,517
So here we can add a third option, which is the root margin.
420
00:24:19,180 --> 00:24:21,810
And so that margin is then basically applied
421
00:24:21,810 --> 00:24:24,470
outside of this root.
422
00:24:24,470 --> 00:24:28,143
So here we can say minus 80 pixels.
423
00:24:29,210 --> 00:24:31,034
So here it has to be pixels.
424
00:24:31,034 --> 00:24:35,283
It doesn't work to use percentages or REM.
425
00:24:36,880 --> 00:24:40,293
And it needs to be a string like this, so inside of quotes.
426
00:24:41,350 --> 00:24:43,677
And so here we use these 80 pixels
427
00:24:43,677 --> 00:24:46,435
because that's exactly what we set
428
00:24:46,435 --> 00:24:49,033
the height of the navigation bar to.
429
00:24:49,930 --> 00:24:53,270
So these eight REM here are basically 80 pixels.
430
00:24:53,270 --> 00:24:55,940
And so this is the reason why it was important
431
00:24:55,940 --> 00:24:58,410
to set the height here manually.
432
00:24:58,410 --> 00:25:02,423
So that then right here, we could use that 80 pixels.
433
00:25:03,370 --> 00:25:07,150
And so let's reload just to make sure.
434
00:25:07,150 --> 00:25:09,633
With JavaScript, sometimes it's better to reload.
435
00:25:10,560 --> 00:25:12,063
And so now it should happen,
436
00:25:12,940 --> 00:25:16,120
yeah, exactly as we reach that.
437
00:25:16,120 --> 00:25:16,963
You saw it?
438
00:25:18,820 --> 00:25:21,980
So that is now almost it.
439
00:25:21,980 --> 00:25:25,180
And so now the only missing part is to remove it
440
00:25:25,180 --> 00:25:27,943
as soon as the hero is still visible.
441
00:25:29,350 --> 00:25:33,113
So once we enter this again, we should get another event.
442
00:25:35,310 --> 00:25:39,050
So I now exited the hero again,
443
00:25:39,050 --> 00:25:43,140
but now as we enter it, we will get a new event.
444
00:25:43,140 --> 00:25:47,483
And so now is intersecting is back to being true.
445
00:25:48,930 --> 00:25:52,903
Okay, and so we can use that to then remove it.
446
00:25:57,800 --> 00:26:02,800
So we can say if true, then remove, okay.
447
00:26:03,950 --> 00:26:06,850
And now here, this true is not necessary at all
448
00:26:06,850 --> 00:26:09,720
because basically this condition here,
449
00:26:09,720 --> 00:26:13,573
as soon as it is true, then this here will get executed.
450
00:26:15,620 --> 00:26:17,050
So this is not necessary.
451
00:26:17,050 --> 00:26:18,770
And that's also the reason why here,
452
00:26:18,770 --> 00:26:20,680
we could set this
453
00:26:20,680 --> 00:26:22,590
because if this is false,
454
00:26:22,590 --> 00:26:27,000
then this one here will basically switch from false to true,
455
00:26:27,000 --> 00:26:29,593
and so therefore, then this code here can execute.
456
00:26:31,060 --> 00:26:33,930
All right, but again, I will leave it here like this
457
00:26:33,930 --> 00:26:36,480
for you to understand the code a little bit better.
458
00:26:37,800 --> 00:26:41,700
But anyway, again, reloading here.
459
00:26:43,530 --> 00:26:46,720
There it is, and there it leaves.
460
00:26:46,720 --> 00:26:48,473
And of course, now it is back up.
461
00:26:50,730 --> 00:26:54,720
So let's see that also in the code.
462
00:26:54,720 --> 00:26:57,350
So here, so now it is sticky,
463
00:26:57,350 --> 00:27:01,143
and now it's gone, and it's back, and it's gone.
464
00:27:02,320 --> 00:27:04,275
So, working really nice.
465
00:27:04,275 --> 00:27:07,440
Let's just try it also in Safari
466
00:27:07,440 --> 00:27:12,110
because things in Safari tends to work a little bit worse,
467
00:27:12,110 --> 00:27:14,423
but still, it is working perfectly.
468
00:27:17,260 --> 00:27:19,470
Very, very nice.
469
00:27:19,470 --> 00:27:22,160
So, as I said in the beginning,
470
00:27:22,160 --> 00:27:25,323
I know that this code here is quite tricky,
471
00:27:25,323 --> 00:27:29,150
but hopefully you were at least able to follow the logic
472
00:27:29,150 --> 00:27:32,990
of what I explained to you here throughout this lecture.
473
00:27:32,990 --> 00:27:35,440
Because if you did that, then you might be able
474
00:27:35,440 --> 00:27:38,370
to adapt this in the future to your own page.
475
00:27:38,370 --> 00:27:40,120
And that is really the main goal
476
00:27:40,120 --> 00:27:43,200
that I have with these few videos here.
477
00:27:43,200 --> 00:27:47,190
All right, but enough talk because it works great.
478
00:27:47,190 --> 00:27:49,580
And so in the next video we will create
479
00:27:49,580 --> 00:27:52,200
our last piece of JavaScript,
480
00:27:52,200 --> 00:27:55,180
or actually we will just use what we already have here
481
00:27:55,180 --> 00:27:56,720
because we now need to fix
482
00:27:56,720 --> 00:27:59,873
the flexbox gap property in Safari.
35375
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.