javascript - 全屏上的 HTML5 视频使用 jquery/javascript 旋转屏幕,就像在移动设备中的 YouTube 上一样?

标签 javascript html jquery mobile html5-video

我有一个 html5 自定义视频播放器,现在我想单击移动设备上的全屏图标将屏幕旋转为横向,反之亦然,就像在 YouTube 中一样。

这是我的 HTML

  <div id="video-block">
    <video class="video_player" id="player" width="100%" controls="controls" autoplay="autoplay">
      <source src="INPUT VIDEO URL HERE"/>
      Your browser does not support the HTML5 video tag.  Use a better browser!
    </video>
    <button onclick="goFullscreen('player'); return false">
      View Fullscreen!
    </button>
 </div>

这里是js

$(document).ready(function() {

    // rotate function

    function rotateVideoPlayer() {
        var width = $(window).width();
        var height = $(window).height();

        $("#video-block").width(0);
        $("#video-block").height(0);

        console.log(width, height);
        // document.body.scrollTop = document.documentElement.scrollTop = 0
        if (width > height) {
            console.log("landscape");
            $("#video-block").width(width);
            $("#video-block").height(width * 9 / 16);


            $("#video-block").css("left", 0 + "px");
            $("#video-block").removeClass("rotated");

        } else {

            console.log("portrait");
            $("#video-block").width(height);
            $("#video-block").height(height * 9 / 16);


            $("#video-block").css("left", width - (width - height * 9 / 16) / 2 + "px");
            $("#video-block").addClass("rotated");
            document.body.scrollTop = document.documentElement.scrollTop = 0

        }
    }


    $('#btn').on('click', function(){
        rotateVideoPlayer();
        var element = document.getElementById('videocontainer');
        if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
        } else if (element.webkitRequestFullScreen) {
            element.webkitRequestFullScreen();
        }
    })
});

CSS

#video-block.rotated{
    -moz-transform:rotate(90deg);
  -webkit-transform:rotate(90deg);
  -o-transform:rotate(90deg);
  -ms-transform:rotate(90deg);
  transform:rotate(90deg);
}

不幸的是,我的解决方案没有按预期工作,有一个全屏,但旋转不能像在 YouTube 上那样正常工作。

有人可以帮我解决这个问题吗?任何帮助或建议将不胜感激

最佳答案

进入全屏模式后,您可以使用screen.orientation.lock('landscape')启用landscape。它仅适用于安卓。

由于 iOS 上启用全屏模式存在一些限制,因此最好像 YouTube 一样对 iOS 上的视频使用默认行为。

$(function() {

  function makeLandscape() {
    // this works on android, not iOS
    if (screen.orientation && screen.orientation.lock) {
      screen.orientation.lock('landscape');
    }
  }

  $(document).on('click', '#btn', function() {
    var element = document.getElementById('video-container');
    if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
      makeLandscape();
    } else if (element.webkitRequestFullScreen) {
      element.webkitRequestFullScreen();
      makeLandscape();
    }

  });

});
<div id="video-container">
  <video class="video_player" id="player" width="100%" autoplay="autoplay" playsinline="">
          <source src="https://wp-iframe.videomill.co/vpaidexamples/videos//vmerce.mp4" />
          Your browser does not support the HTML5 video tag. Use a better browser!
        </video>
  <button id="btn">
          View Fullscreen!
        </button>
</div>

关于javascript - 全屏上的 HTML5 视频使用 jquery/javascript 旋转屏幕,就像在移动设备中的 YouTube 上一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61469628/

相关文章:

javascript - 从 Ext.data.Store 扩展不起作用

javascript - 读取 Web-Audio API 分析器的音频文件(Node.Js 服务器、JS 前端)

javascript - 开发基于 Web 的侧滑菜单的最佳方法是什么?

HTML5 拖放仅适用于 Google Chrome

javascript - 使用 jquery 捕获 <li> 元素上的点击事件在 iPad 上不起作用

javascript - jQuery 验证弹出窗口 : Removing element's style class

javascript - 使用 animate.css(描述中的链接)如何在特定事件完成时触发动画

java - Tapestry 5.4 和 HTML5 音频标签

jquery - 应用样式前查询、完成动画

javascript - Summernote JS - 在同一初始化中注入(inject)代码和参数