This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The VisualViewport interface of 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.
Properties
VisualViewport also inherits properties from its parent, EventTarget.
VisualViewport.offsetLeftRead only- Returns the offset of the left edge of the visual viewport from the left edge of the layout viewport in CSS pixels.
VisualViewport.offsetTopRead only- Returns the offset of the top edge of the visual viewport from the top edge of the layout viewport in CSS pixels.
VisualViewport.pageLeftRead only- Returns the x coordinate of the visual viewport relative to the initial containing block origin of the top edge in CSS pixels.
VisualViewport.pageTopRead only- Returns the y coordinate of the visual viewport relative to the initial containing block origin of the top edge in CSS pixels.
VisualViewport.widthRead only- Returns the width of the visual viewport in CSS pixels.
VisualViewport.heightRead only- Returns the height of the visual viewport in CSS pixels.
VisualViewport.scaleRead only- Returns the pinch-zoom scaling factor applied to the visual viewport.
Events
Listen to these events using addEventListener() or by assigning an event listener to the relevant oneventname property of this interface.
resize- Fired when the visual viewport is resized.
Also available via theVisualViewport.onresizeproperty. scroll- Fired when the visual viewport is scrolled.
Also available via theVisualViewport.onscrollproperty.
Examples
Hiding an overlaid box on zoom
This example, taken from the Visual Viewport README, shows how to write a simple bit of code that will hide an overlaid box (which might contain an advert, say) when the user zooms in. This is a nice way to improve the user experience when zooming in on pages. A live sample is also available.
var bottomBar = document.getElementById('bottombar');
var viewport = window.visualViewport;
function resizeHandler() {
if (viewport.scale > 1.3)
bottomBar.style.display = "none";
else
bottomBar.style.display = "block";
}
window.visualViewport.addEventListener('resize', resizeHandler);
Simulating position: device-fixed
This example, also 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);
Note: This technique should be used with care; emulating position: device-fixed in this way can result in the fixed element flickering during scrolling.
Specifications
| Specification | Status | Comment |
|---|---|---|
| Visual Viewport API The definition of 'VisualViewport' in that specification. |
Draft | Initial definition. |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
VisualViewport | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
height | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
offsetLeft | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
offsetTop | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
onresize | Chrome
Full support
62
| Edge Full support 79 | Firefox
Full support
66
| IE No support No | Opera
Full support
49
| Safari Full support 13 | WebView Android
Full support
62
| Chrome Android
Full support
62
| Firefox Android
Full support
68
| Opera Android
Full support
46
| Safari iOS Full support 13 | Samsung Internet Android
Full support
8.0
|
onscroll | Chrome
Full support
62
| Edge Full support 79 | Firefox
Full support
66
| IE No support No | Opera
Full support
49
| Safari Full support 13 | WebView Android
Full support
62
| Chrome Android
Full support
62
| Firefox Android
Full support
68
| Opera Android
Full support
46
| Safari iOS Full support 13 | Samsung Internet Android
Full support
8.0
|
pageLeft | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
pageTop | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
resize event | Chrome
Full support
62
| Edge Full support 79 | Firefox
Full support
66
| IE No support No | Opera
Full support
49
| Safari Full support 13 | WebView Android
Full support
62
| Chrome Android
Full support
62
| Firefox Android
Full support
68
| Opera Android
Full support
46
| Safari iOS Full support 13 | Samsung Internet Android
Full support
8.0
|
scale | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
scroll event | Chrome
Full support
62
| Edge Full support 79 | Firefox
Full support
66
| IE No support No | Opera
Full support
49
| Safari Full support 13 | WebView Android
Full support
62
| Chrome Android
Full support
62
| Firefox Android
Full support
68
| Opera Android
Full support
46
| Safari iOS Full support 13 | Samsung Internet Android
Full support
8.0
|
width | Chrome Full support 61 | Edge Full support 79 | Firefox
Full support
63
| IE No support No | Opera Full support 48 | Safari Full support 13 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android
Full support
68
| Opera Android Full support 45 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- User must explicitly enable this feature.
- User must explicitly enable this feature.
See also
- Web Viewports Explainer — useful explanation of web viewports concepts, including the difference between visual viewport and layout viewport.
