具有相同功能的多个目标

时间:2015-05-22 02:12:07

标签: 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();
&#13;
 .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;
         }
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

如果您只是想让您的代码在可用的视频中运行,那么这非常简单并且通过循环完成。您只需抓取页面中的视频并循环浏览它们即可。我首先使用jquery按类名选择视频,在本例中为$(.video-js),使用jquery&#39; s .eachhttps://api.jquery.com/each/)循环播放,然后调用{{1}每个函数都有一个函数,并将唯一的视频ID作为名为HDtoggle()的参数传递。然后,用该参数替换了videoID的实例。这是两个视频的小提琴:

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

创建循环是编程中最重要的部分之一。现在,只要他们拥有video-js类和唯一ID,您就可以添加尽可能多的视频。

答案 1 :(得分:0)

  

创建循环是编程最重要的部分之一

是的,但创建一个通用的插件而不是一个硬编码的例子甚至更好:)