I want the “+01” element to follow the image div while someone is scrolling, but I don’t know how to make it so that it starts moving downwards before it hits the top of the page. Is there a simple solution to this?
Advertisement
Answer
If I understand your question correctly, I think you may be aiming for sticky
position. More info
I have made this example based in your image for you. Hope it helps:
JavaScript
x
23
23
1
body {margin:0; padding:0; background-color:orange;}
2
.container {
3
position:relative;
4
margin:0 auto;
5
width:400px;
6
margin-top:20px;
7
margin-bottom:40px;
8
}
9
img {
10
width:100%;
11
display:block;
12
margin-top:-40px;
13
}
14
.number {
15
position:sticky;
16
top:30px;
17
margin-left:-40px;
18
font-size:40px;
19
color:#fff;
20
text-shadow: 2px 2px 3px rgba(0,0,0,0.8);
21
font-weight:bold;
22
23
}
JavaScript
1
28
28
1
<div class="container">
2
<div class="number">
3
+01
4
</div>
5
<img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">
6
7
</div>
8
<div class="container">
9
<div class="number">
10
+02
11
</div>
12
<img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">
13
14
</div>
15
<div class="container">
16
<div class="number">
17
+03
18
</div>
19
<img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">
20
21
</div>
22
<div class="container">
23
<div class="number">
24
+04
25
</div>
26
<img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">
27
28
</div>