-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathabsolute-fixed-to-viewport.html
55 lines (50 loc) · 1.58 KB
/
absolute-fixed-to-viewport.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
<!DOCTYPE html>
<!--
This example shows how the ViewportAPI can be used to keep an absolute
positioned element fixed to the viewport. Note that this example assumes that
the window properties are relative to the layout viewport (see
crbug.com/489206). This can be tested by enabling the "Inert Visual Viewport"
experiment from chrome:flags.
Without the flag, the ViewportAPI is not needed since properties like
window.pageYOffset already take the visual viewport's position into account.
-->
<html>
<head>
<meta name="description" content="Stick position:absolute to visual viewport">
<meta name="viewport" content="width=device-width, initial-scale=2">
<style>
body {
height: 2500px;
}
#fixed {
position:absolute;
left: 50px;
height: 200px;
width: 200px;
top: 150px;
background-color: #fcc;
}
</style>
</head>
<body>
<div id="fixed"></div>
<script>
function viewport() {
return window.visualViewport;
}
var fixed = document.getElementById("fixed");
var initialTop = fixed.getBoundingClientRect().top + viewport().offsetTop;
var fixedAt = 10;
function viewportHandler() {
var scrollTop = window.pageYOffset + viewport().offsetTop;
if (scrollTop > initialTop)
fixed.style.top = (scrollTop + fixedAt) + 'px';
else
fixed.style.top = '';
}
window.onscroll = viewportHandler;
viewport().onscroll = viewportHandler;
viewport().onresize = viewportHandler;
</script>
</body>
</html>