W3cubDocs

/DOM

Visual Viewport API

Draft
This page is not complete.

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The Visual Viewport API provides an explicit mechanism for querying and modifying the properties of the visual viewport. The visual viewport is the visual portion of a screen excluding on-screen keyboards, areas outside of a pinch-zoom area, or any other on-screen artifact that doesn't scale with the dimensions of a page.

Visual Viewport concepts and usage

The mobile web contains two viewports, the layout viewport and the visual viewport. The layout viewport covers all the elements on a page and the visual viewport is what is actually visible on the screen. When the user pinch-zooms into the page, the visual viewport shrinks but the layout viewport is unchanged. User-interface features like the on-screen keyboard (OSK) can shrink the visual viewport without affecting the layout viewport.

What happens when a web page element needs to be visible on screen regardless of the visible portion of a web page? For example, what if you need a set of image controls to remain on screen regardless of the pinch zoom level of the device? Current browsers vary in how they handle this. Visual viewport lets web developers solve this by positioning elements relative to what's shown on screen.

The visual viewport API adds two events, onresize and onscroll, that fire whenever the visual viewport changes. These events allow you to position elements relative to the visual viewport that would normally be anchored to the layout viewport.

Example

The code below is based on the sample the specification, though it adds a few things that make it function better. It shows a function called viewportHandler(). When called it queries the offsetLeft and height properties for values it uses in a CSS translate() method. You invoke this function by passing it to both event calls.

One thing that may not be clear in this example is the use of the pendingUpdate flag and the call to requestAnimationFrame(). The pendingUpdate flag serves to prevent multiple invocations of the transfrom that can occur when onresize and onscroll fire at the same time. Using requestAnimationFrame() ensures that the transform ocurrs before the next render.

let pendingUpdate = false;

function viewportHandler() {
  if (pendingUpdate) return;
  pendingUpdate = true;

  requestAnimationFrame(() => {
    pendingUpdate = false;
    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);

Interfaces

VisualViewport
Represents the visual viewport for a given window.

Specifications

Specification Status Comment
Visual Viewport API
The definition of 'VisualViewport' in that specification.
Draft Initial definition.

Browser CompatibilityUpdate compatibility data on GitHub

VisualViewport interface

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 48 No
height 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 48 No
offsetLeft 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 48 No
offsetTop 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 48 No
onresize 62
62
61
No No No 49
49
48
No
onscroll 62
62
61
No No No 49
49
48
No
pageLeft 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 48 No
pageTop 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 48 No
scale 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 48 No
width 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
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
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
48 No ?
height 61 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
48 No ?
offsetLeft 61 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
48 No ?
offsetTop 61 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
48 No ?
onresize 62
62
61
62
62
61
No No 49
49
48
No ?
onscroll 62
62
61
62
62
61
No No 49
49
48
No ?
pageLeft 61 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
48 No ?
pageTop 61 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
48 No ?
scale 61 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
48 No ?
width 61 61 No 63
Disabled
63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
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/Visual_Viewport_API