forked from terrymun/Fluidbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fluidbox.min.js
1 lines (1 loc) · 5.45 KB
/
jquery.fluidbox.min.js
1
function whichTransitionEvent(){var a,b=document.createElement("fakeelement"),c={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(a in c)if(void 0!==b.style[a])return c[a]}!function(a,b){var c=function(a,b,c){var d;return function(){function g(){c||a.apply(e,f),d=null}var e=this,f=arguments;d?clearTimeout(d):c&&a.apply(e,f),d=setTimeout(g,b||100)}};jQuery.fn[b]=function(a){return a?this.bind("resize",c(a)):this.trigger(b)}}(jQuery,"smartresize");var customTransitionEnd=whichTransitionEvent();!function(a){var b=0;a.fn.fluidbox=function(c){var d=a.extend(!0,{viewportFill:.95,debounceResize:!0,stackIndex:1e3,stackIndexDelta:10,closeTrigger:[{selector:".fluidbox-overlay",event:"click"},{selector:"document",event:"keyup",keyCode:27}]},c),e=["keyup","keydown","keypress"];d.stackIndex<d.stackIndexDelta&&(d.stackIndexDelta=d.stackIndex),$fbOverlay=a("<div />",{"class":"fluidbox-overlay",css:{"z-index":d.stackIndex}});var h,f=this,g=a(window),i=function(b){a(b+".fluidbox-opened").trigger("click")},j=function(a,b){var c=a.find("img"),e=a.find(".fluidbox-ghost"),f=a.find(".fluidbox-wrap"),i=a.data(),j=0,k=0;c.data().imgRatio=i.natWidth/i.natHeight;var l,m,n;if(h>c.data().imgRatio)j=i.natHeight<g.height()*d.viewportFill?i.natHeight:g.height()*d.viewportFill,i.imgScale=j/c.height(),i.imgScaleY=i.imgScale,l=c.height()*i.imgScaleY,m=l/i.natHeight,n=i.natWidth*m/c.width(),i.imgScaleX=n;else{k=i.natWidth<g.width()*d.viewportFill?i.natWidth:g.width()*d.viewportFill,i.imgScale=k/c.width(),i.imgScaleX=i.imgScale;var o=c.width()*i.imgScaleX,m=o/i.natWidth,n=i.natHeight*m/c.height();i.imgScaleY=n}var p=g.scrollTop()-c.offset().top+.5*c.data("imgHeight")*(c.data("imgScale")-1)+.5*(g.height()-c.data("imgHeight")*c.data("imgScale")),q=.5*c.data("imgWidth")*(c.data("imgScale")-1)+.5*(g.width()-c.data("imgWidth")*c.data("imgScale"))-c.offset().left,r=parseInt(1e3*i.imgScaleX)/1e3+","+parseInt(1e3*i.imgScaleY)/1e3;e.css({transform:"translate("+parseInt(10*q)/10+"px,"+parseInt(10*p)/10+"px) scale("+r+")",top:c.offset().top-f.offset().top,left:c.offset().left-f.offset().left}).one(customTransitionEnd,function(){a.trigger(b)})},k=function(a){function i(){f.imgWidth=b.width(),f.imgHeight=b.height(),f.imgRatio=b.width()/b.height(),c.css({width:b.width(),height:b.height(),top:b.offset().top-e.offset().top+parseInt(b.css("borderTopWidth"))+parseInt(b.css("paddingTop")),left:b.offset().left-e.offset().left+parseInt(b.css("borderLeftWidth"))+parseInt(b.css("paddingLeft"))}),f.imgScale=h>f.imgRatio?g.height()*d.viewportFill/b.height():g.width()*d.viewportFill/b.width()}if(h=g.width()/g.height(),a.hasClass("fluidbox")){var b=a.find("img"),c=a.find(".fluidbox-ghost"),e=a.find(".fluidbox-wrap"),f=b.data();i(),b.load(i)}},l=function(b){if(a(this).hasClass("fluidbox")){var c=a(this),e=a(this).find("img"),f=a(this).find(".fluidbox-ghost"),g=a(this).find(".fluidbox-wrap"),h={};0!==a(this).data("fluidbox-state")&&a(this).data("fluidbox-state")?(c.trigger("closestart"),c.data("fluidbox-state",0).removeClass("fluidbox-opened").addClass("fluidbox-closed"),h.open&&window.clearTimeout(h.open),h.close=window.setTimeout(function(){a(".fluidbox-overlay").remove(),g.css({"z-index":d.stackIndex-d.stackIndexDelta})},10),a(".fluidbox-overlay").css({opacity:0}),f.css({transform:"translate(0,0) scale(1)",opacity:0,top:e.offset().top-g.offset().top+parseInt(e.css("borderTopWidth"))+parseInt(e.css("paddingTop")),left:e.offset().left-g.offset().left+parseInt(e.css("borderLeftWidth"))+parseInt(e.css("paddingLeft"))}).one(customTransitionEnd,function(){c.trigger("closeend")}),e.css({opacity:1})):(a(this).trigger("openstart"),a("<img />",{src:e.attr("src")}).load(function(){a("<img />",{src:c.attr("href")}).load(function(){c.data("natWidth",a(this)[0].naturalWidth).data("natHeight",a(this)[0].naturalHeight),c.append($fbOverlay).data("fluidbox-state",1).removeClass("fluidbox-closed").addClass("fluidbox-opened"),h.close&&window.clearTimeout(h.close),h.open=window.setTimeout(function(){a(".fluidbox-overlay").css({opacity:1})},10),a(".fluidbox-wrap").css({zIndex:d.stackIndex-d.stackIndexDelta-1}),g.css({"z-index":d.stackIndex+d.stackIndexDelta}),f.css({"background-image":"url("+e.attr("src")+")",opacity:1}),e.css({opacity:0}),f.css({"background-image":"url("+c.attr("href")+")"}),j(c,"openend")})})),b.preventDefault()}},m=function(b){b?k(b):f.each(function(){k(a(this))});var c=a("a.fluidbox.fluidbox-opened");c.length>0&&j(c,"resizeend")};return d.debounceResize?a(window).smartresize(function(){m()}):a(window).resize(function(){m()}),f.each(function(){if(a(this).is("a")&&1===a(this).children().length&&a(this).children().is("img")&&"none"!==a(this).css("display")&&"none"!==a(this).parents().css("display")){var f=a("<div />",{"class":"fluidbox-wrap",css:{"z-index":d.stackIndex-d.stackIndexDelta}});b+=1;var h=a(this);h.addClass("fluidbox fluidbox-closed").attr("id","fluidbox-"+b).wrapInner(f).find("img").css({opacity:1}).after('<div class="fluidbox-ghost" />').each(function(){var b=a(this);b.width()>0&&b.height()>0?(k(h),h.click(l)):b.load(function(){k(h),h.click(l)})}),a(this).on("recompute",function(){m(a(this)),a(this).trigger("recomputeend")});var j="#fluidbox-"+b;d.closeTrigger&&a.each(d.closeTrigger,function(b){var c=d.closeTrigger[b];"window"!=c.selector?"document"==c.selector&&(c.keyCode&&e.indexOf(c.event)>-1?a(document).on(c.event,function(a){a.keyCode==c.keyCode&&i(j)}):a(document).on(c.event,j,function(){i(j)})):g.on(c.event,function(){i(j)})})}}),f}}(jQuery);