-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add visual viewport API demo * Simplify example code * Couple of small tweaks
- Loading branch information
1 parent
08dadaa
commit aa3dd78
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Visual Viewport API example</title> | ||
<script async src="main.js"></script> | ||
<link href="style.css" rel="stylesheet" type="text/css"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
</head> | ||
|
||
<body> | ||
<p id="instructions">Try scrolling around on a desktop and a mobile browser to see how the reported values change. | ||
Also try pinch/zooming on a mobile browser to see the effect.</p> | ||
<div id="output"> | ||
<p id="visual-info"></p> | ||
<hr> | ||
<p id="window-info"></p> | ||
</div> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const output = document.getElementById("output"); | ||
const visualInfo = document.getElementById("visual-info"); | ||
const windowInfo = document.getElementById("window-info"); | ||
|
||
const scrollUpdater = () => { | ||
output.style.top = `${visualViewport.offsetTop + 10}px`; | ||
output.style.left = `${visualViewport.offsetLeft + 10}px`; | ||
output.style.background = "yellow"; | ||
updateText(); | ||
}; | ||
|
||
const scrollendUpdater = () => { | ||
output.style.background = "lime"; | ||
updateText(); | ||
}; | ||
|
||
function updateText() { | ||
visualInfo.innerText = `Visual viewport left: ${visualViewport.offsetLeft.toFixed(2)} top: ${visualViewport.offsetTop.toFixed(2)}`; | ||
windowInfo.innerText = `Window scrollX: ${window.scrollX.toFixed(2)} scrollY: ${window.scrollY.toFixed(2)}`; | ||
} | ||
|
||
updateText(); | ||
|
||
visualViewport.onresize = scrollUpdater; | ||
visualViewport.onscroll = scrollUpdater; | ||
visualViewport.onscrollend = scrollendUpdater; | ||
window.onresize = scrollUpdater; | ||
window.onscroll = scrollUpdater; | ||
window.onscrollend = scrollendUpdater; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
html { | ||
font-family: sans-serif; | ||
height: 2000px; | ||
width: 4000px; | ||
} | ||
|
||
p { | ||
margin: 0; | ||
} | ||
|
||
#instructions { | ||
position: relative; | ||
top: 90px; | ||
} | ||
|
||
#output { | ||
position: fixed; | ||
top: 10px; | ||
left: 10px; | ||
border: solid 1px green; | ||
background: lime; | ||
padding: 5px; | ||
} |