简体   繁体   English

HTML5视频未使用.load()返回海报图像

[英]HTML5 video not returning to poster image using .load()

I have a video with a poster image and overlay content. 我有一个带有海报图像和叠加内容的视频。 The video requests full screen when user clicks custom play button. 当用户单击自定义播放按钮时,视频请求全屏显示。 If the user exits full screen, the video is reloaded and the overlay content returns. 如果用户退出全屏显示,则会重新加载视频并返回叠加内容。 Everything is working great except the poster image is not returning. 一切都很好,除了海报图像没有返回。 Can anyone help me with this? 谁能帮我这个?

The javascript/jQuery is: javascript / jQuery是:

var featuredButton = $('.featured-panel .play img');
var featuredOverlay = $('.featured-panel .overlay-content');
var featuredVideo = $('.featured-video');
var featuredDown = $('.featured-panel .down');

// when clicking play button
featuredButton.on('click', function() {
  // hide play button
  $(this).hide();
  // hide down button
  featuredDown.hide();
  // hide overlay content
  featuredOverlay.fadeOut();
  // play the video
  $(this).parent().siblings(featuredVideo)[0].play();

  if (win.width() > 1024) {
    // vv This will automatically request full screen, consider using this and then returning to default poster view when full screen is exited
    $(this).parent().siblings(featuredVideo)[0].webkitRequestFullScreen();
    $(this).parent().siblings(featuredVideo)[0].mozRequestFullScreen();
  }
});

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

  if (event == "FullscreenOff") {
    //do something when fullscreen off
    featuredVideo.load();
    featuredOverlay.fadeIn();
    featuredButton.show();
  }
});

Try to bind the fullscreenchange event on document or window instead of <video> . 尝试将fullscreenchange事件而不是<video>绑定到documentwindow According to mdn 根据mdn

fullscreenchange fullscreenchange

General info 基本信息

Specification Fullscreen Interface 规格全屏界面
Event Bubbles Yes 活动气泡
Cancelable No 取消
Target Document 目标 文件
Default Action None 默认动作

Also note that your first call to .webkitRequestFullScreen(); 还要注意,您第一次调用.webkitRequestFullScreen(); will raise an error in Firefox, which, I think, will make it stops executing the script. 会在Firefox中引发错误,我认为这将使其停止执行脚本。

here is a better polyfill : 这是一个更好的polyfill:

if(element.requestFullScreen)
    element.requestFullScreen();
    else if(element.webkitRequestFullScreen)
        element.webkitRequestFullScreen();
        else if(element.mozRequestFullScreen)
            element.mozRequestFullScreen();
            else if(element.msRequestFullscreen)
                element.msRequestFullscreen();
                else console.warn("fullscreen API not supported by this browser")

Here is a sample code with assumptive html, so I commented the featuredDown parts. 这是带有假定html的示例代码,因此我注释了featuredDown部分。
It will not work in here , but try this on your machine, it will. 它不能在这里工作 ,但是可以在您的计算机上尝试。 (fullscreeen requests are blocked inside the iframes of snippets.) (全屏请求在代码段的iframe中被阻止。)

 var featuredButton = $('.featured-panel .play img'); var featuredOverlay = $('.featured-panel .overlay-content'); var featuredVideo = $('.featured-video'); var win= $(window); //var featuredDown = $('.featured-panel .down'); function requestFullscreen(element){ if(element.requestFullScreen) element.requestFullScreen(); else if(element.webkitRequestFullScreen) element.webkitRequestFullScreen(); else if(element.mozRequestFullScreen) element.mozRequestFullScreen(); else if(element.msRequestFullscreen) element.msRequestFullscreen(); else console.warn("fullscreen API not supported by this browser"); } // when clicking play button featuredButton.on('click', function() { // hide play button $(this).hide(); // hide down button // featuredDown.hide(); // hide overlay content featuredOverlay.fadeOut(); // play the video $(this).parent().siblings(featuredVideo)[0].play(); if (win.width() > 200) { requestFullscreen($(this).parent().siblings(featuredVideo)[0]); } }); win.bind('webkitfullscreenchange mozfullscreenchange fullscreenchange', function(e) { var state = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen; var event = state ? 'FullscreenOn' : 'FullscreenOff'; console.log(event); if (event == "FullscreenOff") { //do something when fullscreen off featuredVideo.load(); featuredOverlay.fadeIn(); featuredButton.show(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="featured-panel"> <div class="play"> <img src="http://lorempixel.com/50/50"> </div> <video controls="true" width="500" poster="http://lorempixel.com/500/280" class="featured-video"> <source type="video/mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4"> <source type="video/ogg" src="http://media.w3.org/2010/05/sintel/trailer.ogv"> </video> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM