-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitobject.min.js
1 lines (1 loc) · 1.79 KB
/
fitobject.min.js
1
!function(t,i){"function"==typeof define&&define.amd?define(["jquery"],i):"object"==typeof module&&module.exports?module.exports=i(require("jquery")):t.fitObject=i(t.jQuery)}(this,function(t){var i=function(i){t(this).each(function(h,a){var r=t(this),f=null,l=null,c=null,d=null,s={},u={},p={},g=null,w={container:null,fit:"cover",safeArea:{top:0,right:0,bottom:0,left:0}};i&&"object"==typeof i&&(w=t.extend(w,i)),i&&"string"==typeof i&&(w.fit=i),r.data("safe-area")&&(w.safeArea=r.data("safe-area")),r.data("object-fit")&&(w.fit=r.data("object-fit")),r.data("fit-container")&&(w.container=r.data("fit-container")),l=r.parents(".fit-object-wrapper"),(f=e(r,l,w)).css({display:"block",visibility:"visible"}),d=(c=r.innerHeight()/r.width())<f.height()/f.width(),s.height=f.width()*c,s.width=f.height()/c,p.x=s.width-f.width(),p.y=s.height-f.height(),u.x=(f.width()-s.width)/2,u.y=(f.height()-s.height)/2,"cover"===w.fit&&d&&(g={height:f.height(),width:s.width,left:o("horizontal",u.x,p.x,w),top:0}),"cover"!==w.fit||d||(g={height:s.height,width:f.width(),left:0,top:o("vertical",u.y,p.y,w)}),"contain"===w.fit&&d&&(g={height:s.height,width:f.width(),left:0,top:u.y}),"contain"!==w.fit||d||(g={height:f.height(),width:s.width,left:u.x,top:0}),f.attr("style",null),n(r,l,f,g)})},e=function(i,e,n){return 0===e.length&&null===n.container?i.parent():null===n.container?e.parent():t(n.container)},n=function(i,e,n,o){t.extend(o,{position:"absolute"}),e.length<1?((e=t('<div class="fit-object-wrapper"></div>')).css(o),i.wrap(e)):e.css(o),"static"===n.css("position")&&n.css("position","relative"),"hidden"!==n.css("overflow")&&n.css("overflow","hidden")},o=function(t,i,e,n){var o=n.safeArea,h=(o.bottom-o.top)/100,a=(o.right-o.left)/100;return"horizontal"===t&&(i+=e*a),"vertical"===t&&(i+=e*h),i>0&&(i=0),i<-e&&(i=-e),i};t.fn.fitObject=i});