Entering the full screen in iframe makes the page scroll up

Asked

Viewed 242 times

2

I’m creating a page that will be used as an iframe in other applications, to test how it behaved in different sizes, created a page and added several iframes to the page I was making, but with different sizes.

On this page there is an option "Fullscreen" that goes into full screen, when entering full screen and then leaving, even without navigating the iframe, the page that embeds the iframe scrolls up a little, this behavior occurs just testing directly on the mobile, on the desktop, even emulating a mobile, this does not occur

To understand what could be doing this, I did a test with the minimum to start fullscreen, created a page with only one iframe:

<iframe
  src="data:text/html;base64,PGJ1dHRvbiBvbmNsaWNrPSJkb2N1bWVudC5kb2N1bWVudEVsZW1lbnQucmVxdWVzdEZ1bGxzY3JlZW4oKSI+ZnM8L2J1dHRvbj4="
  frameborder="0"
  allowfullscreen
  style="margin: 50vh 10%; width: 80%; height: 35vh;"
></iframe>

Contents of iframe in Base64:

<button onclick="document.documentElement.requestFullscreen()">fs</button>

I did the same tests, reloaded the page, rolled down, clicked on the button and left the fullscreen ("Esc" key on the desktop and the back of Android), this made, always (mobile, desktop and desktop emulating mobile) scrolled the page up

I did some more tests to check if the code that starts fullscreen, some meta tag or the style of html and body could affect this behavior, however, the results remained the same

I tested on Android with Chrome 83 and Linux with Chromium 83

The page I’m creating always occupies 100% of the size of the device, no pixel more or less, so it has no scroll, all elements have the position: fixed or your container has it. The code that controls fullscreen is this:

const requestFullscreen =
  document.documentElement.requestFullscreen ||
  document.documentElement[
    Object.keys(document.documentElement).find(key =>
      key.endsWith("RequestFullscreen")
    )
  ]

function force() {
  if (document.fullscreenElement === null) setTimeout(() => enter(), 500)
}

function enter() {
  if (document.fullscreenEnabled && requestFullscreen)
    requestFullscreen
      .call(document.documentElement)
      .then(() => force())
      .catch(() => force())
}

const exitFullscreen =
  document.exitFullscreen ||
  document[
    Object.keys(document).find(
      key => key.endsWith("ExitFullscreen") || key.endsWith("CancelFullscreen")
    )
  ]

function exit() {
  if (exitFullscreen) exitFullscreen.call(document)
}

function toggle() {
  document.fullscreenElement !== null ? exit() : enter()
}

What causes and how to prevent this bearing on the page that embeds the iframe?

  • But when I did the test, creating a page and placing only its iframe, this behavior did not occur: https://pasteboard.co/JhYVNyT.gif

  • Here is the page I created to test your iframe: https://pasteboard.co/JhYWSLO.png

  • Maybe you need to add some more information so I can reproduce this behavior, such as HTML and CSS.

  • @Cahianr.Freire probably because of the browser or OS version, I tested it on Chromium 83 on Linux Mint 19.3. Which is odd, since it would make this behavior unstable/uncontrollable

  • I believe that it is very unlikely that the browser version or operating system is causing the behavior of scrolling the page up does not occur, as I did this same test on 4 more browsers (Chrome, firefox, opera and Brave) and 2 operating systems (linux and windows). I believe that the problem is not in the iframe but in the page where you are putting this iframe.

  • @Cahianr.Freire only with iframe, no doctype, html, body, etc, and also tested with the snippet "doc" of VS Code, which comes with this basic structure and tag <meta name="viewport" content="width=device-width, initial-scale=1.0">. I tested both forms on the page that embeds the iframe and on the iframe page (base 64)

Show 1 more comment

1 answer

-1

A possible solution will be:

<iframe src="" scrolling="no"></iframe>

and in css:

iframe {
  overflow: hidden;
}
  • The attribute scrolling is obsolete and iframe does not have overflow

  • @Costamilam you can test for a return false; after the call of onclick="document.documentElement.requestFullscreen()" onclick="document.documentElement.requestFullscreen(); return false;", if you could for an example in jsfiddle for example, I cannot reproduce the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.