javascript - 在javascript中以全屏模式制作一个div

标签 javascript jquery html

我需要在全屏模式下制作一个div。
在这个 div 中,我有一个 video 和自定义控件。
所以我希望当用户单击 FS 视频时,自定义控件会进入全屏模式。
这是我使用的代码。

<div id="custom-video">
    <video id="video" src="path/to/src"></video>
    <button onclick="fsMode()">FS</button>
</div>

这是javascript代码。

function fsMode(){
    var i = $('#custom-video');
    if (i.requestFullscreen) {
        console.log('1');   
        i.requestFullscreen();
    } else if (i.webkitRequestFullscreen) {
        console.log('2');
        i.webkitRequestFullscreen();
    } else if (i.mozRequestFullScreen) {
        console.log('3');
        i.mozRequestFullScreen();
    } else if (i.msRequestFullscreen) {
        console.log('4');
        i.msRequestFullscreen();
    }else{
        console.log('Not available');
    }
}

当我尝试此代码时,输​​出是 Not available
但我以这种方式尝试了相同的代码及其工作,这意味着浏览器支持 fullscreenMode,但为什么这不适用于上面的代码。

// this code is working but I don't want to use it because 
// custom controls will not show in fullscreen mode 
var video = $('#video').get(0);
if (video.requestFullscreen) {
    video.requestFullscreen();
} else if (video.mozRequestFullScreen) {
    video.mozRequestFullScreen(); // Firefox
} else if (video.webkitRequestFullscreen) {
    video.webkitRequestFullscreen(); // Chrome and Safari
}

有什么建议吗?
谢谢...

最佳答案

jQuery 选择器如 $("#video-id") 返回的内容与 document.getElementById("video-id") 返回的内容不同。在大多数情况下,您可以使用 $("#video-id")[0] 或 $("#video-id").get(0) 来获取等效的 javascript,这正是我们在这种情况下所需要的。这就是您的代码无法正常工作的原因。

关于javascript - 在javascript中以全屏模式制作一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36615061/

相关文章:

javascript - 如何使用 javascript 创建监听器

javascript - angularjs json两种方式绑定(bind)到文本区域

javascript - 来自未发布的 Google Drive 电子表格的 JSON 数据?

jQuery "queue"(ajaxQueue 插件)-如何清除?

html - 设置导航菜单事件选项卡 HTML/CSS 样式的问题

html - 从 Javascript 添加时,Bootstrap 列表组元素不显示

javascript - 将div分成2列

javascript - 我应该如何修改 PHP 和 Javascript 文件,以便复选框是强制性的?

jquery - 固定位置 div 仅垂直

html - 如何在 Angular 中的选择之外获取所选选项的索引?