javascript - 多个具有相同功能的目标

标签 javascript video

我尝试使用 Video JS HD Toggle Plugin 为我的用户提供更多功能,但我确实知道如何在同一页面上显示两个视频并在我的播放器上使用 HD 插件。如何更改代码并使其适用于多个视频?

        
        function HDtoggle () {
           
		   var HD1 = false;
		   /* It is the variable which tells us that that HD video is getting played or not.
			  HD1 = false ---> video is not HD
			  HD1 = true ----> video is HD */
          
             videojs.HD = videojs.Button.extend({
           /* @constructor */
               init: function(player, options){
                 videojs.Button.call(this, player, options);
                 this.on('click', this.onClick);
               }
             });
            
			/* Changing sources by clicking on HD button */
			/* This function is called when HD button is clicked */
            videojs.HD.prototype.onClick = function() {
         	
         	
         var HDsrc = $("#video_1").find( "video" ).attr("HD"); 
		 /* Value of HD attribute in video tag is saved in HDsrc. */
         var noHDsrc = $("#video_1").find( "video" ).attr("nonHD");
		 /* Value of nonHD attribute in video tag is saved in noHDsrc. */
         	
			if (HD1) { /* If video is not HD */
         var css = document.createElement("style");
         css.type = "text/css";
         css.innerHTML = ".vjs-control.vjs-HD-button { color: silver; font-weight:normal; text-shadow: 0 0 5em #fff;}";
		  /* Changing the HD button to initial styling when we play non HD video by clicking on HD button.*/
         document.body.appendChild(css);
         videojs("video_1").src([{type: "video/mp4", src: noHDsrc }]);
		 
         videojs("video_1").play();
		 /* This automatically plays the video when we click on HD button to change the source.*/
         HD1 = false;
         }
         else { /* if video is HD */
         var css = document.createElement("style");
         css.type = "text/css";
         css.innerHTML = ".vjs-control.vjs-HD-button { color: #36D8DE; font-weight:bold; text-shadow: 0 0 1em #fff;}";
		/*This css applies when HD video is played. You can easily change the blue color of HD button by changing the value of color above. If you would like to remove the shadow from HD button, remove text-shadow from above.*/
		document.body.appendChild(css);
         videojs("video_1").src([{type: "video/mp4", src: HDsrc }]);
         videojs("video_1").play();
          /*This automatically plays the video when we  click on HD button to change the source.*/
		 HD1 = true;
         }
         	
         };
         
		 /* Create HD button */
		 var createHDButton = function() {
               var props = {
                   className: 'vjs-HD-button vjs-control',
                   innerHTML: '<div class="vjs-control-content">' + ('HD') + '</div>',
                   role: 'button',
                   'aria-live': 'polite', 
                   tabIndex: 0
                 };
               
               return videojs.Component.prototype.createEl(null, props);
             };
         
		 /* Add HD button to the control bar */
         var HD;
             videojs.plugin('HD', function() {
               var options = { 'el' : createHDButton() };
               HD = new videojs.HD(this, options);
               this.controlBar.el().appendChild(HD.el());
             });
         
          /* Set Up Video.js Player */
		 var vid = videojs("video_1", {
              plugins : { HD : {} }
            });
             
}
    HDtoggle();
 .vjs-default-skin .vjs-control.vjs-HD-button {
         display: block;
		 font-size: 1.5em;
         line-height: 2;
         position: relative;
         top: 0;
         float:right;
		 left: 10px;
         height: 100%;
         text-align: center;
         cursor: pointer;
         }
<script src="http://vjs.zencdn.net/4.6.4/video.js"></script>
<link href="http://vjs.zencdn.net/4.6.4/video-js.css" rel="stylesheet"/>


 <video id="video_1" class="video-js vjs-default-skin vjs-big-play-centered" HD="http://video-js.zencoder.com/oceans-clip.mp4" nonHD="http://video-js.zencoder.com/oceans-clip.mp4" width="640" height="480" controls>
         <source type="video/mp4" src="http://video-js.zencoder.com/oceans-clip.mp4">
      </video>

 <video id="video_2" class="video-js vjs-default-skin vjs-big-play-centered" HD="http://video-js.zencoder.com/oceans-clip.mp4" nonHD="http://video-js.zencoder.com/oceans-clip.mp4" width="640" height="480" controls>
         <source type="video/mp4" src="http://video-js.zencoder.com/oceans-clip.mp4">
      </video>

最佳答案

如果您只是想让您的代码在尽可能多的可用视频中工作,那么这非常简单,并且可以通过循环完成。您只需抓取页面中的视频并循环播放即可。我首先使用 jquery 按类名选择视频,在本例中为 $(.video-js),然后使用 jquery 的 .each ( https://api.jquery.com/each/ ) 循环遍历它们,然后然后为每个视频调用 HDtoggle() 函数,并传入唯一的视频 ID 作为名为 videoID 的参数。然后只需将 video_1 实例替换为该参数即可。这是一个适用于两个视频的 fiddle :

http://jsfiddle.net/parnj1Lj/2/

创建循环是编程中最重要的部分之一。现在,只要您添加的视频具有 video-js 类和唯一 ID,这将适用于任意数量的视频。

关于javascript - 多个具有相同功能的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30387308/

相关文章:

c++ - QMediaPlayer 给我有关视频的错误信息

javascript - jQuery 在字符串中查找 url 并用 href 换行(如果尚未换行)

javascript - rxjs 观察超时的正确方法

javascript - 只有在第一个完成后才启用第二个类型

php - 如何使嵌入式视频仅对特定播放器可用

iPhone - 从视频文件中分离音频并将其保存到单独的文件中

javascript - 使用 javascript 设置屏幕方向、宽度和高度

javascript - 使 Bootstrap 模式可拖动并保持背景可用

安卓视频播放器问题

python - 一个非常简单的轻量级视频编辑器