Sunday, 30 June 2019

Full screen toggle for jQuery video player does not assign class-name correctly

I am working on a JavaScript/jQuery video player. It has a bug whose cause I was unable to find.

The players has, among others, an enter/exit full-screen button (it can be seen at the bottom of the HTML snippet):

(function($) {

  /* Helper functions */
  /* 1) full screen */
  function toggleFullScreen(elem) {
    if ((document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen)) {
      if (elem.requestFullScreen) {
        elem.requestFullScreen();
      } else if (elem.mozRequestFullScreen) {
        elem.mozRequestFullScreen();
      } else if (elem.webkitRequestFullScreen) {
        elem.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
      } else if (elem.msRequestFullscreen) {
        elem.msRequestFullscreen();
      }
    } else {
      if (document.cancelFullScreen) {
        document.cancelFullScreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      }
    }
  }

  $('video').each(function() {
    var video = $(this)[0],
      videoContainer = video.closest('div'),
      $playToggleBtn = $(videoContainer).find('input[name="play-pause"]'),
      $progressBar = $(videoContainer).find('.progress-bar'),
      $progress = $(videoContainer).find('.progress'),
      $current_time = $(videoContainer).find('.current-time'),
      $durationDisplay = $(videoContainer).find('.duration'),
      $volumeSlider = $(videoContainer).find('.volume-slider'),
      $mute_toggle = $(videoContainer).find('.mute-toggle'),
      $muteBtn = $mute_toggle.find('input[type="checkbox"]'),
      $rate_display = $(videoContainer).find('.rate_display'),
      $fullScreenToggler = $(videoContainer).find('input[name="screen-toggler"]'),
      $playSpeed = $(videoContainer).find('.playback-rate ul li');

    $(document).bind('webkitfullscreenchange mozfullscreenchange fullscreenchange', function(e) {
      var state = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
      var event = state ? 'FullscreenOn' : 'FullscreenOff';

      if (event === 'FullscreenOff') {
        $(videoContainer).removeClass('fullscreen');
        $fullScreenToggler.removeClass('exit');
      } else {
        $(videoContainer).addClass('fullscreen');
        $fullScreenToggler.addClass('exit');
      }
    });

  });

})(jQuery);

<div class="video-container">
  <video poster="posters/poster.jpg">
            <source src="videos/mymovie.mp4" type="video/mp4" />
        </video>
  <div class="controls-wrapper">
    <div class="progress-bar">
      <div class="progress"></div>
    </div>
    <ul class="video-controls">
      <li><input type="button" name="play-pause" value="Play" class="play" /></li>
      <li><a href="#" class="previous">Previous</a></li>
      <li><a href="#" class="next">Next</a></li>
      <li class="mute-toggle unmuted"><input type="checkbox" name="mute" /></li>
      <li><input type="range" min="0" max="1" step="0.01" class="volume-slider" /></li>
      <li><span class="current-time"></span><span>/</span><span class="duration"></span></li>
      <li class="playback-rate">
        <span class="rate_display">Normal</span>
        <div class="piker">
          <ul class="dropdown-content" id="rate_selector">
            <li data-rate="0.5">0.5x</li>
            <li data-rate="0.75">0.75x</li>
            <li data-rate="1">Normal</li>
            <li data-rate="1.125">1.125x</li>
            <li data-rate="1.5">1.5x</li>
            <li data-rate="2">2x</li>
          </ul>
        </div>
      </li>
      <li class="fullscreen-container">
        <input type="button" name="screen-toggler" value="Fullscreen" class="toggle-fullscreen" />
      </li>
    </ul>
  </div>
</div>

The problem: If I have two videos (or more), even if I click the full-screen button of the first clip, the fullscreen class-name will be added to all the video-container elements on the page.

Why does that happen?



from Full screen toggle for jQuery video player does not assign class-name correctly

No comments:

Post a Comment