我正在使用JavaScript/jQuery视频播放器。它有一个我找不到原因的错误。

除其他外,播放器还具有一个进入/退出全屏按钮(可以在HTML代码段的底部看到):

(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>

问题:如果我有两个视频(或更多),即使单击第一个剪辑的全屏按钮,fullscreen类名也将添加到页面上的所有video-container元素中。

为什么会这样呢?

更新:

按照Kaiido的回答,我已使用以下代码解决了此问题。
$(document).on('fullscreenchange', evt => {
    if ($(document.fullscreenElement).is(videoContainer)) {
        $(document.fullscreenElement).addClass('fullscreen');
        $(videoContainer).removeClass('fullscreen');
        $fullScreenToggler.removeClass('exit');
    } else {
        $(videoContainer).removeClass('fullscreen');
        $fullScreenToggler.addClass('exit');
    }
});

解决了上述问题。但是,我失去了使用ESC键作为全屏退出的功能。

更新2:

我已将播放器插入 Github repo

最佳答案

您将事件绑定(bind)到Document对象的次数与页面中具有元素的次数相同。

Stacksnippets不允许全屏显示,但是您正在做的一个模型是:

$('div').each(function(i, el) {
  $(document).on('click', e => console.log('clicked'));
  console.log('added click event on Document');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>


因此,每次您进入或退出全屏模式时,所有这些事件都会触发,并会在每个视频上进行调整,无论该事件是来自哪个视频。

您最好在each迭代器之外添加单个事件处理程序
$(document).on('fullscreenchange', evt => {
  if($(document.fullscreenElement).is('video')) {
    $(document.fullscreenElement).addClass('fullscreen');
  }
  else {
    $('video.fullscreen').removeClass('fullscreen');
  }
});

但是,实际上,使用全屏类的所有操作都是无用的,因为已经有一个 :fullscreen pseudo class由浏览器本地设置。

关于javascript - jQuery视频播放器的全屏切换未正确分配类名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56680150/

10-13 09:29