javascript - 在全屏视频上显示文字

标签 javascript jquery html css video.js

我使用 videojs 库编写了一段小代码,该代码在播放实际视频之前先播放广告视频。我想要一个应该出现在广告视频中的 div 标签,它工作得很好,唯一的问题是当视频全屏显示时标签不会显示。

这是我的html代码

<div class="videoContainer">
    <video id="video_1" class="video-js playads" height="200px" width="300px" video-url="http://vjs.zencdn.net/v/oceans.mp4" mimetype="video/mp4" controls controlsList="nodownload" preload="none" data-setup=''>
        <!-- ad source ad ad video url   -->
        <source src="http://techslides.com/demos/sample-videos/small.mp4" type='video/mp4' />
    </video>
    <!-- text to be displayed while playing ad   -->
    <div class="advertisement hide">Advertisement</div>
</div>

CSS 代码:

.videoContainer {
    max-width: 300px;
    position: relative;
    margin: 20px auto 0;
}
.advertisement{
    position:absolute;
    color: rgb(230, 200, 98);
    padding: 5px 10px;
    text-align: right;
    background: rgba(0, 0, 0, 0.4);
    bottom: 50px;
    right:0;
    font-size: 14px;
    font-weight: 700;
    z-index: 1 !important;
}
.hide{
    display:none;
}

Javascript 代码是:

$(document).ready(function(){
    var videotag = $('.playads');
    var myPlayer = videojs('video_1');
    // show advertisement label while play advertisement
    myPlayer.on('play', function() {
        if(myPlayer.hasClass("playads")){
            $('.advertisement').removeClass('hide');
        }
    });
    // Stop from seeking while playing advertisement
    myPlayer.on("seeking", function(event) {
        if (currentTime < myPlayer.currentTime() && myPlayer.hasClass("playads")) {
            myPlayer.currentTime(currentTime);
        }
    });
    myPlayer.on("seeked", function(event) {
        if (currentTime < myPlayer.currentTime() && myPlayer.hasClass("playads")) {
            myPlayer.currentTime(currentTime);
        }
    });
    setInterval(function() {
        if (!myPlayer.paused() && myPlayer.hasClass("playads")) {
            currentTime = myPlayer.currentTime();
        }
    }, 1000);

    // Hide Advertisement label and change data-src of player to play actual video
    myPlayer.on('ended', function() {
        if(this.hasClass("playads")){
            this.src({type: videotag.attr('mimetype'), src: videotag.attr('video-url')});
            this.play();
            this.removeClass('playads');
            $('.advertisement').addClass('hide');
        }
    });
});

我在这里错过了什么?是videojs吗?

这是我的笔的链接: https://codepen.io/isheikhspear/pen/RjMOgw?editors=0010

最佳答案

这可以通过将您的参数移到 video.js 的包装器中来实现。所以你的新 HTML 将是:

<div class="videoContainer">
<!-- Add some extra attribute as video-url and mimetype which we can later play once we are done playing ads  -->
  <video id="video_1" class="video-js playads" height="200px" width="300px" video-url="http://vjs.zencdn.net/v/oceans.mp4" mimetype="video/mp4" controls controlsList="nodownload" preload="none" data-setup=''>
<!-- ad source ad ad video url   -->
  <source src="http://techslides.com/demos/sample-videos/small.mp4" type='video/mp4' />
</video>
<!-- text to be displayed while playing ad   -->
  <div class="moveToVideoJs">
    <div class="advertisement hide">Advertisement</div>
  </div>
</div>

初始化 video.js 后,您应该将包含我们要移动到 video.js 的内容的 div 移动到 video.js。

$(".moveToVideoJs")
  .appendTo($('#video_1'));

这是 new CodePen 的链接.

关于javascript - 在全屏视频上显示文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432481/

相关文章:

php - 需要用PHP保存数据的HTML表单

javascript - 如何防止 Visual Studio Code 将链式函数推到新行?

javascript - android中KeyboardEvent的键码无法识别?

Javascript 仅在从特定目录查看时才处于事件状态

javascript - 有没有办法用 php 检查 url 中的特定参数?

javascript - jquery 添加类错误

javascript - 正则表达式不工作

javascript - 如何启用和修复链接悬停时的预览页面?

php - 如何制作动态月、日、年错误信息

html - 对单个文本使用两种文本颜色,例如基于背景颜色的蒙版