<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">

  <title>Prevent Firefox from Moving After .exitFullscreen()</title>
  <style>
    button {
      display: block;
      padding: 0;
      width: 32px;
      height: 32px;
      text-align: center;
      cursor: pointer;
      background-size: contain;
    }
    
    .expand {
      background: url(http://imgh.us/expand_2.svg)no-repeat;
    }
  </style>
</head>

<body>



  <!--I tried using the ~a~nchors, -id-, -name-, and -tabindex- 
FF was ignoring my attempts to keep or get focus. Using named or 
id ~a~nchors failed since the distance between desired spot
and the spot FF ends up at is short.-->
  <div id='A' class='media'>A
    <video id="vid1" class="vid fs" src="http://html5demos.com/assets/dizzy.mp4" controls></video>

  </div>
  <div id='B' class='media'>B
    <img id='img1' class='img fs' src='http://imgh.us/Lenna.png'>
    <button class='expand'></button>
  </div>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
  <script>
    /* Several attempts to use the .focus() method
    || and focus events did not work for me, neither
    || has tabindex and anchors. If it appears that
    || my implementation is wrong (a strong possibility)
    || please inform me.
    */
    $('button').click(function(e) {
      e.preventDefault();
      var tgt = $(this).prev();
      fs(tgt[0]);

    });
    /* This function is for the MCVE 
    || It enables the ~select~ to remove and re-insert
    || the test elements. By doing so, we can see
    || how the test elements behave in different
    || combinations. What I found out about FF is
    || that when exiting a full screened ~img~ that's
    || positioned last is that it will lose focus 
    || and the viewport is scrolled up to the element 
    || above it.
    */

    $('#sel1').on('change', function(e) {
      var V = $(this).val();
      var first = $('#' + V).find(':first').attr('id');
      if ($('#' + V).hasClass('media')) {
        $('#' + V).fadeOut('#' + first);
      } else {
        $('#' + V).fadeIn('#' + first);
      }
      $('#' + V).toggleClass('media');
    });

    /* These 2 functions are responsible for 
    || full screen. Please inform me if there's a 
    || better way, or if anything is outdated. I
    || have researched the Fullscreen API and I
    || haven't found any updates of any use. I've
    || used these functions for the last 3 years
    || so maybe I might've missed something 
    || critical.
    */ // There's no ms prefixes because I'm not concerned about IE.

    var isFullScreen = function() {
      return !!(document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement);
    }
    // SOLUTION XXXXXXXXXX]START[XXXXXXXXXXXXXXX
    var yOffset;
        document.onmozfullscreenchange = function() {
          if(!isFullScreen()){
            window.scrollTo(0, yOffset);
          }
          
        };
    // SOLUTION XXXXXXXXXXX]END[XXXXXXXXXXXXXXXX
    function fs(target) {

      if (!isFullScreen()) {
        yOffset = pageYOffset;
        if (target.requestFullscreen) {
          target.requestFullscreen();
        } else if (target.webkitRequestFullscreen) {
          target.webkitRequestFullscreen();
        } else if (target.mozRequestFullScreen) {
          target.mozRequestFullScreen();
        }
      } else {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        }

      }
    }
  </script>
</body>

</html>
     

Objective
=========
>Prevent Firefox from Moving After `.exitFullscreen()`

Behavior
-----------
>###Expected

>When exiting fullscreen mode, we should be at the same position that we were at before.
 

>###Experienced

>In FF, when exiting fullscreen mode, we are scrolled up as if the element above has a higher tab priority or more than likely is that tab index and focus are being ignored by FF.

Question
--------
> **How can I prevent Firefox from scrolling up after exiting fullscreen mode?**

 1. To reproduce issue, use the `<select>` to remove items C, D, E, and F.

 2. Next, fullscreen item B by clicking the icon below it.

 3. Then exit full screen mode by hitting `ESC`.

 4. Notice we have jumped up the page.