This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The VisualViewport
interface of the the Visual Viewport API represents the visual viewport for a given window. For a page containing iframes, each iframe, as well as the containing page, will have a unique window object. Each window on a page will have a unique VisualViewport
representing the properties associated with that window.
You can get a window's visual viewport using Window.visualViewport
.
Note: Only the top-level window has a visual viewport that's distinct from the layout viewport. Therefore, it's generally only the VisualViewport
object of the top-level window that's useful. For an <iframe>
, visual viewport metrics like VisualViewport.width
always correspond to layout viewport metrics like document.documentElement.clientWidth
.
VisualViewport
also inherits properties from its parent, EventTarget
.
VisualViewport.offsetleft
Read only
VisualViewport.offsetTop
Read only
VisualViewport.pageLeft
Read only
VisualViewport.pageTop
Read only
VisualViewport.width
Read only
VisualViewport.height
Read only
VisualViewport.scale
Read only
VisualViewport.onresize
VisualViewport.onscroll
This example, taken from the Visual Viewport README, shows how to use this API to simulate position: device-fixed
, which fixes elements to the visual viewport. A live sample is also available.
var bottomBar = document.getElementById('bottombar'); var viewport = window.visualViewport; function viewportHandler() { var layoutViewport = document.getElementById('layoutViewport'); // Since the bar is position: fixed we need to offset it by the visual // viewport's offset from the layout viewport origin. var offsetLeft = viewport.offsetLeft; var offsetTop = viewport.height - layoutViewport.getBoundingClientRect().height + viewport.offsetTop; // You could also do this by setting style.left and style.top if you // use width: 100% instead. bottomBar.style.transform = 'translate(' + offsetLeft + 'px,' + offsetTop + 'px) ' + 'scale(' + 1/viewport.scale + ')' } window.visualViewport.addEventListener('scroll', viewportHandler); window.visualViewport.addEventListener('resize', viewportHandler);
Specification | Status | Comment |
---|---|---|
Visual Viewport API The definition of 'VisualViewport' in that specification. | Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 61 | No | 63
|
No | 48 | No |
height
|
61 | No | 63
|
No | 48 | No |
offsetLeft
|
61 | No | 63
|
No | 48 | No |
offsetTop
|
61 | No | 63
|
No | 48 | No |
onresize
|
62
|
No | No | No | 49
|
No |
onscroll
|
62
|
No | No | No | 49
|
No |
pageLeft
|
61 | No | 63
|
No | 48 | No |
pageTop
|
61 | No | 63
|
No | 48 | No |
scale
|
61 | No | 63
|
No | 48 | No |
width
|
61 | No | 63
|
No | 48 | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 61 | 61 | No | 63
|
48 | No | ? |
height
|
61 | 61 | No | 63
|
48 | No | ? |
offsetLeft
|
61 | 61 | No | 63
|
48 | No | ? |
offsetTop
|
61 | 61 | No | 63
|
48 | No | ? |
onresize
|
62
|
62
|
No | No | 49
|
No | ? |
onscroll
|
62
|
62
|
No | No | 49
|
No | ? |
pageLeft
|
61 | 61 | No | 63
|
48 | No | ? |
pageTop
|
61 | 61 | No | 63
|
48 | No | ? |
scale
|
61 | 61 | No | 63
|
48 | No | ? |
width
|
61 | 61 | No | 63
|
48 | No | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport