forked from cyberjo50/backgroundVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundVideo.min.js
1 lines (1 loc) · 3.71 KB
/
backgroundVideo.min.js
1
!function(i,o,t,s){"use strict";function n(o,t){function s(){n.options.originalVideoW=n.options.$video[0].videoWidth,n.options.originalVideoH=n.options.$video[0].videoHeight,n.initialised||n.init()}var n=this;this.element=o,this.options=i.extend({},r,t),this._defaults=r,this._name=e,this.options.$video=i(o),this.detectBrowser(),this.options.has3d=this.detect3d(),this.options.$videoWrap.css({position:"relative",overflow:"hidden","z-index":"10"}),this.options.$video.css({position:"absolute","z-index":"1"}),this.options.$video.on("canplay canplaythrough",s),this.options.$video[0].readyState>3&&s()}var e="backgroundVideo",r={$videoWrap:i("#video-wrap"),$outerWrap:i(o),$window:i(o),minimumVideoWidth:400,preventContextMenu:!1,parallax:!0,parallaxOptions:{effect:1.5},pauseVideoOnViewLoss:!1};n.prototype={init:function(){var i=this;this.initialised=!0,this.options.$window.resize(function(){i.positionObject(i.options.$video)}),this.options.parallax&&this.options.$window.on("scroll.backgroundVideoParallax",function(){i.positionObject(i.options.$video)}),this.options.pauseVideoOnViewLoss&&this.playPauseVideo(),this.options.preventContextMenu&&this.options.$video.on("contextmenu",function(){return!1}),this.options.$window.trigger("resize")},detect3d:function(){var i,n,e=t.createElement("p"),r={WebkitTransform:"-webkit-transform",OTransform:"-o-transform",MSTransform:"-ms-transform",MozTransform:"-moz-transform",transform:"transform"};t.body.insertBefore(e,t.body.lastChild);for(i in r)e.style[i]!==s&&(e.style[i]="matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)",n=o.getComputedStyle(e).getPropertyValue(r[i]));return n!==s?"none"!==n:!1},detectBrowser:function(){var i=navigator.userAgent.toLowerCase();i.indexOf("chrome")>-1||i.indexOf("safari")>-1?(this.options.browser="webkit",this.options.browserPrexix="-webkit-"):i.indexOf("firefox")>-1?(this.options.browser="firefox",this.options.browserPrexix="-moz-"):-1!==i.indexOf("MSIE")||i.indexOf("Trident/")>0?(this.options.browser="ie",this.options.browserPrexix="-ms-"):i.indexOf("Opera")>-1&&(this.options.browser="opera",this.options.browserPrexix="-o-")},scaleObject:function(i,o){var t,s,n;return o.width(this.options.$outerWrap.width()),o.height(this.options.$outerWrap.height()),t=this.options.$window.width()/this.options.originalVideoW,s=this.options.$window.height()/this.options.originalVideoH,n=t>s?t:s,n*this.options.originalVideoW<this.options.minimumVideoWidth&&(n=this.options.minimumVideoWidth/this.options.originalVideoW),i.width(n*this.options.originalVideoW),i.height(n*this.options.originalVideoH),{xPos:-(parseInt(i.width()-this.options.$window.width())/2),yPos:parseInt(i.height()-this.options.$window.height())/2}},positionObject:function(i){var o=this,t=this.options.$window.scrollTop(),s=this.scaleObject(i,o.options.$videoWrap),n=s.xPos,e=s.yPos;e=this.options.parallax?t>=0?this.calculateYPos(t):this.calculateYPos(0):-e,o.options.has3d?(i.css(o.options.browserPrexix+"transform3d","translate3d(-"+n+"px, "+e+"px, 0)"),i.css("transform","translate3d("+n+"px, "+e+"px, 0)")):(i.css(o.options.browserPrexix+"transform","translate(-"+n+"px, "+e+"px)"),i.css("transform","translate("+n+"px, "+e+"px)"))},calculateYPos:function(i){var o,t,s;return o=parseInt(this.options.$videoWrap.offset().top),t=o-i,s=-(t/this.options.parallaxOptions.effect)},disableParallax:function(){this.options.$window.unbind(".backgroundVideoParallax")},playPauseVideo:function(){var i=this;this.options.$window.on("scroll.backgroundVideoPlayPause",function(){i.options.$window.scrollTop()<i.options.$videoWrap.height()?i.options.$video.get(0).play():i.options.$video.get(0).pause()})}},i.fn[e]=function(o){return this.each(function(){i.data(this,"plugin_"+e)||i.data(this,"plugin_"+e,new n(this,o))})}}(jQuery,window,document);