Open In App

Web API Visual Viewport

Last Updated : 06 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The visual Viewport is the actual screen size of the device after removing keyboard space, the pinch zoom area, or any other kind of space that takes up screen size. The Visual Viewport API can be understood as an interface that exposes information about what is currently visible on the screen and provides us with various methods to modify the properties of the Visual Viewport.

Types of Visual View Port:

  • Layout Viewport: The entire layout of the web-page, including the content that is off-screen due to scrolling.
  • Visual Viewport: It refers to the currently visible portion of the web page within the user device.
file

Understanding Visual and Layout Viewport

Accessing the API

Accessing the Visual View port API involves interacting with the Visual Viewport object from the ‘window’ object in JavaScript. This object gives information about the position, width, height, and scale of the visual viewport.

Example:

Javascript




// Script.js
 
// window.visualViewport gives access
// to the Visual Viewport object.
const visualViewport =
    window.visualViewport;
 
// Returns width and height of the
// visual viewport in CSS pixels.
const width = visualViewport.width;
const height = visualViewport.height;
 
// Returns the offset of the visual
// viewport from the layout viewport
const offsetX =
    visualViewport.offsetLeft;
const offsetY =
    visualViewport.offsetTop;
 
console.log(
    "Width: " +
        width +
        ", Height: " +
        height);
console.log(
    "OffsetLeft: " +
        offsetX +
        ", OffsetTop: " +
        offsetY);


HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>
      Visual Viewport API Exploration
  </title>
    <link rel="stylesheet" href="style.css">
</head>
 
<body>
    <h1>Hello, World!</h1>
</body>
<script src="./script.js"></script>
 
</html>


CSS




/* Style.css */
body {
    margin: 0;
    padding: 0;
 
    height: 200vh;
}
 
h1 {
    font-size: 2rem;
    transition: font-size 0.3s ease;
     
    /* Smooth transition for font size change */
    text-align: center;
    margin-top: 50vh;
     
    /* Center the heading vertically */
    padding: 20px;
}


Output:

file

Output of the above HTML page

Events/ Interface

The Visual Viewport API includes two events that can be listened to for changes in the Visual Viewport. The changes can be listened to via addEventListener() in JavaScript.

  • Resize: Event is fired when the visual viewport is changed.
  • Scroll: Event is fired when the visual viewport is scrolled.

Example:

Javascript




//Script.js
 
// Access the Visual Viewport API
const visualViewport =
    window.visualViewport;
 
// Listen for resize events on the visual viewport
window.addEventListener(
    "resize",
    function () {
         
        // Handle the resize event
        window.alert(
            "Visual viewport resized!"
);});
 
// Listen for scroll events on the visual viewport
window.addEventListener(
    "scroll",
    function () {
         
        // Handle the scroll event
        window.alert(
            "User is scrolling!"
);});


HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Visual Viewport API Exploration</title>
     <style>
          body{
              height:100vh;
              }
      </style>
</head>
 
<body>
    <h1>Please Scroll or Resize Screen</h1>
</body>
<script src="./script.js"></script>
 
</html>


Output:

Animation



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads