forked from jquery-backstretch/jquery-backstretch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.backstretch.min.js
4 lines (4 loc) · 5.56 KB
/
jquery.backstretch.min.js
1
2
3
4
/*! Backstretch - v2.1.1 - 2015-05-8
* http://srobbin.com/jquery-plugins/backstretch/
* Copyright (c) 2015 Scott Robbin; Licensed MIT */
!function(t,i,s){"use strict";t.fn.backstretch=function(e,o){return(e===s||null===e||0===e.length)&&console.debug("Warning: No images were supplied for Backstretch"),0===t(i).scrollTop()&&i.scrollTo(0,0),this.each(function(){var i=t(this),s=i.data("backstretch");if(s){if("string"==typeof e&&"function"==typeof s[e])return void s[e](o);o=t.extend(s.options,o),s.destroy(!0)}s=new h(this,e,o),i.data("backstretch",s)})},t.backstretch=function(i,s){return t("body").backstretch(i,s).data("backstretch")},t.expr[":"].backstretch=function(i){return t(i).data("backstretch")!==s},t.fn.backstretch.defaults={centeredX:!0,centeredY:!0,duration:5e3,fade:0,bleed:0,parallax:!1};var e={wrap:{left:0,top:0,overflow:"hidden",margin:0,padding:0,height:"100%",width:"100%",zIndex:-999999},img:{position:"absolute",display:"none",margin:0,padding:0,border:"none",width:"auto",height:"auto",maxHeight:"none",maxWidth:"none",zIndex:-999999}},h=function(s,h,n){this.options=t.extend({},t.fn.backstretch.defaults,n||{}),this.options.parallax&&(this.options.centeredY=!1),this.images=t.isArray(h)?h:[h],this.$window=t(i),t.each(this.images,function(){t("<img />")[0].src=this}),this.isBody=s===document.body,this.$container=t(s),this.$root=this.isBody?t(o?i:document):this.$container;var r=this.$container.children(".backstretch").first();if(this.$wrap=r.length?r:t('<div class="backstretch"></div>').css(e.wrap).appendTo(this.$container),!this.isBody){var a=this.$container.css("position"),c=this.$container.css("zIndex");this.$container.css({position:"static"===a?"relative":a,zIndex:"auto"===c?0:c,background:"none"}),this.$wrap.css({zIndex:-999998}),this.options.bleed>0&&(this.$container.css("overflow","hidden"),this.$wrap.css({top:-1*this.options.bleed,left:-1*this.options.bleed}))}this.$wrap.css({position:this.isBody&&o?"fixed":"absolute"}),this.index=0,this.show(this.index),this.$window.on("resize.backstretch",t.proxy(this.resize,this)).on("orientationchange.backstretch",t.proxy(function(){this.isBody&&0===i.pageYOffset&&(i.scrollTo(0,1),this.resize())},this))};h.prototype={resize:function(){try{var t=this;this.bgCSS={left:0,top:0},this.rootWidth=this.isBody?this.$root.width():this.$root.innerWidth()+2*this.options.bleed,this.bgWidth=this.rootWidth,this.rootHeight=this.isBody?i.innerHeight?i.innerHeight:this.$root.height():this.$root.innerHeight()+2*this.options.bleed,this.bgHeight=this.bgWidth/this.$img.data("ratio"),this.bgOffset=0,this.bgHeight>=this.rootHeight?this.options.parallax&&!this.isBody?(this.bgOffset=this.rootHeight-this.bgHeight,this.bgCSS.top=this.bgOffset+"px"):this.options.centeredY?(this.bgOffset=-1*(this.bgHeight-this.rootHeight)/2,this.bgCSS.top=this.bgOffset+"px"):this.bgOffset=0:(this.bgHeight=this.rootHeight,this.bgWidth=this.bgHeight*this.$img.data("ratio"),this.options.centeredX&&(this.bgOffset=(this.bgWidth-this.rootWidth)/2,this.bgCSS.left="-"+this.bgOffset+"px")),this.$wrap.css({width:this.rootWidth,height:this.rootHeight}).find("img:not(.deleteable)").css({width:this.bgWidth,height:this.bgHeight}).css(this.bgCSS),this.options.parallax&&!this.isBody&&this.bgHeight>this.rootHeight&&(this.parallax(),this.$window.scroll(function(){t.parallax()})),this.options.parallax&&this.isBody&&console.warn("Warning: Parallax cannot be applied to $.backstretch elements.")}catch(s){}return this},parallax:function(){this.isVisible=!1,this.pos=this.$root.offset().top-this.$window.scrollTop();var t=this.$window.height()+this.rootHeight,i=this.bgHeight-this.rootHeight;this.pos<this.$window.height()&&this.pos+this.$root.outerHeight()>0&&(this.isVisible=!0,this.bgCSS.top=-1*(this.pos+this.rootHeight)/t*i,this.$wrap.find("img:not(.deleteable)").css(this.bgCSS))},show:function(i){if(!(Math.abs(i)>this.images.length-1)){var s=this,h=s.$wrap.find("img").addClass("deleteable"),o={relatedTarget:s.$container[0]};return s.$container.trigger(t.Event("backstretch.before",o),[s,i]),this.index=i,clearInterval(s.interval),s.$img=t("<img />").css(e.img).bind("load",function(e){var n=this.width||t(e.target).width(),r=this.height||t(e.target).height();t(this).data("ratio",n/r),t(this).fadeIn(s.options.speed||s.options.fade,function(){h.remove(),s.paused||s.cycle(),t(["after","show"]).each(function(){s.$container.trigger(t.Event("backstretch."+this,o),[s,i])})}),s.resize()}).appendTo(s.$wrap),s.$img.attr("src",s.images[i]),s}},next:function(){return this.show(this.index<this.images.length-1?this.index+1:0)},prev:function(){return this.show(0===this.index?this.images.length-1:this.index-1)},pause:function(){return this.paused=!0,this},resume:function(){return this.paused=!1,this.next(),this},cycle:function(){return this.images.length>1&&(clearInterval(this.interval),this.interval=setInterval(t.proxy(function(){this.paused||this.next()},this),this.options.duration)),this},destroy:function(s){t(i).off("resize.backstretch orientationchange.backstretch"),clearInterval(this.interval),s||this.$wrap.remove(),this.$container.removeData("backstretch")}};var o=function(){var t=navigator.userAgent,s=navigator.platform,e=t.match(/AppleWebKit\/([0-9]+)/),h=!!e&&e[1],o=t.match(/Fennec\/([0-9]+)/),n=!!o&&o[1],r=t.match(/Opera Mobi\/([0-9]+)/),a=!!r&&r[1],c=t.match(/MSIE ([0-9]+)/),d=!!c&&c[1];return!((s.indexOf("iPhone")>-1||s.indexOf("iPad")>-1||s.indexOf("iPod")>-1)&&h&&534>h||i.operamini&&"[object OperaMini]"==={}.toString.call(i.operamini)||r&&7458>a||t.indexOf("Android")>-1&&h&&533>h||n&&6>n||"palmGetResource"in i&&h&&534>h||t.indexOf("MeeGo")>-1&&t.indexOf("NokiaBrowser/8.5.0")>-1||d&&6>=d)}()}(jQuery,window);