-
Notifications
You must be signed in to change notification settings - Fork 78
/
jquery.row-grid.min.js
5 lines (5 loc) · 2.15 KB
/
jquery.row-grid.min.js
1
2
3
4
5
(function(l){function m(d,a,f){var p=0,e=[];f=jQuery.makeArray(f||d.querySelectorAll(a.itemSelector));var q=f.length,g=d.getBoundingClientRect();d=Math.floor(g.right-g.left)-parseFloat(l(d).css("padding-left"))-parseFloat(l(d).css("padding-right"));g=[];for(var c,h,n,b=0;b<q;++b)(c=f[b].getElementsByTagName("img")[0])?((h=parseInt(c.getAttribute("width")))||c.setAttribute("width",h=c.offsetWidth),(n=parseInt(c.getAttribute("height")))||c.setAttribute("height",n=c.offsetHeight),g[b]={width:h,height:n}):
(f.splice(b,1),--b,--q);q=f.length;for(c=0;c<q;++c){f[c].classList?(f[c].classList.remove(a.firstItemClass),f[c].classList.remove(a.lastItemClass),f[c].classList.remove(a.lastRowClass)):f[c].className=f[c].className.replace(new RegExp("(^|\\b)"+a.firstItemClass+"|"+a.lastItemClass+"|"+a.lastRowClass+"(\\b|$)","gi")," ");p+=g[c].width;e.push(f[c]);if(c===q-1)for(b=0;b<e.length;b++)0===b&&(e[b].className+=" "+a.lastRowClass),e[b].style.cssText="width: "+g[c+parseInt(b)-e.length+1].width+"px;height: "+
g[c+parseInt(b)-e.length+1].height+"px;margin-right:"+(b<e.length-1?a.minMargin+"px":0);if(p+a.maxMargin*(e.length-1)>d){h=p+a.maxMargin*(e.length-1)-d;b=e.length;(a.maxMargin-a.minMargin)*(b-1)<h?(n=a.minMargin,h-=(a.maxMargin-a.minMargin)*(b-1)):(n=a.maxMargin-h/(b-1),h=0);var m=0;for(b=0;b<e.length;b++){var r=e[b];var t=g[c+parseInt(b)-e.length+1].width,k=t-t/p*h;t=Math.round(g[c+parseInt(b)-e.length+1].height*(k/t));.5<=m+1-k%1?(m-=k%1,k=Math.floor(k)):(m+=1-k%1,k=Math.ceil(k));r.style.cssText=
"width: "+k+"px;height: "+t+"px;margin-right: "+(b<e.length-1?n:0)+"px";0===b&&(r.className+=" "+a.firstItemClass);b==e.length-1&&(r.className+=" "+a.lastItemClass)}e=[];p=0}}}l.fn.rowGrid=function(d){return this.each(function(){var a=l(this);if("appended"===d)d=a.data("grid-options"),a=a.children("."+d.lastRowClass),a=a.nextAll(d.itemSelector).add(a),m(this,d,a);else if(d=l.extend({},l.fn.rowGrid.defaults,d),a.data("grid-options",d),m(this,d),d.resize)l(window).on("resize.rowGrid",{container:this},
function(a){m(a.data.container,d)})})};l.fn.rowGrid.defaults={minMargin:null,maxMargin:null,resize:!0,lastRowClass:"last-row",firstItemClass:null,lastItemClass:null}})(jQuery);