-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-switcher.min.js
1 lines (1 loc) · 2 KB
/
video-switcher.min.js
1
var videoSwitcher={config:{id:"myvideo",rotate:!1,vids:[]},init:function(e){var t=this;e&&(void 0!==e.id&&(t.config.id=e.id),void 0!==e.rotate&&(t.config.rotate=e.rotate),void 0!==e.vids&&(t.config.vids=e.vids));var i=document.getElementById(t.config.id);if(i){var o,d,n,r=[],c=t.config.id+"-video",h=o=t.curVideoIndex(0);i.innerHTML='<video autoplay muted preload="auto" id="'+c+'" style="width:100%"><source src="'+t.config.vids[h].file+'" type="video/mp4" /></video>',(i=document.getElementById(c)).addEventListener("loadedmetadata",function(e){if("undefined"!=typeof $){var t=$(this).height();$(this).css("height","auto");var i=$(this).height();t!=i&&$(this).height(t).animate({height:i},200,function(){$(this).css("height","")})}}),i.addEventListener("timeupdate",function(e){if(this.currentTime>this.duration/2){if(d=o+1>=t.config.vids.length?0:o+1,t.config.rotate||(d=t.curVideoIndex(d)),!t.config.rotate){var i=this.getAttribute("loop");d==o?i||this.setAttribute("loop",!0):i&&this.removeAttribute("loop")}if(!r[d]&&d!=o){var c=t.config.vids[d].file;fetcher_id=t.config.id+"-fetch",fetcher=document.getElementById(fetcher_id),fetcher&&document.body.removeChild(fetcher),fetcher=document.createElement("video"),fetcher.style.display="none",fetcher.muted=!0,fetcher.preload="auto",fetcher.id=fetcher_id,fetcher.innerHTML='<source src="'+c+'" type="video/mp4" />',document.body.appendChild(fetcher),r[d]=1}}!n&&this.currentTime>1&&(this.setAttribute("controls",!0),n=!0)}),i.addEventListener("ended",function(e){if(!1,d!=o){var i=this.clientHeight;this.src=t.config.vids[d].file,this.style.height=i+"px",o=d}this.removeAttribute("controls"),n=!1,this.play()})}else console.error("Video element not found: "+t.config.id)},curVideoIndex:function(e){var t=this.config.vids;if(t.length&&!this.config.rotate){for(var i=new Date,o=3600*i.getHours()+60*i.getMinutes()+i.getSeconds(),d=0,n=0;n<t.length;++n)d+=void 0!==t[n].duration?t[n].duration:1;o%=d,d=0;for(n=0;n<t.length;++n)if(o<(d+=void 0!==t[n].duration?t[n].duration:1))return n}return e}};