From 3e07ad765785aca8812840f9b1116caf6b616f66 Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Fri, 24 Feb 2017 22:22:12 +0100 Subject: [PATCH 01/24] Nearly rewrote the gallery. The vertical option no longer creates horizontal scrollbars, instead the height is variable for better integration in websites. Fixes the flickering issue on resizing and the bug where the images get distorted after several resizing events. Also improved performance on resizing. --- jquery.balanced-gallery.js | 327 ++++++++++++++++++++----------------- 1 file changed, 181 insertions(+), 146 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index dceb535..cd34ccb 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -12,12 +12,12 @@ orientation: 'horizontal', padding: 5, shuffleUnorderedPartitions: true, - viewportHeight: null, - viewportWidth: null + divisor: 4 }, - ALL_CHILDREN_LOADED = 'ALL_CHILDREN_LOADED', resizeTimeout = null, - RADIX = 10; + RADIX = 10, + THRESHOLD = 1 - 0.00001, //-0.00001 due to imprecision of floating values + overflow = 0; //this wrapper prevents multiple instantiations of the plugin: $.fn[pluginName] = function ( options ) { @@ -31,34 +31,23 @@ function BalancedGallery( element, options ) { balancedGallery = this; // for contexts when 'this' doesn't refer to the BalancedGallery class. this.element = element; - this.elementChildren = $(element).children('*'); + $(this.element).wrapInner(''); + this.container = $(this.element).children()[0]; + this.elementChildren = $(this.container).children('*'); this.options = $.extend( {}, defaults, options); // merge arg options and defaults + this.options.orientation = (this.options.orientation).toLowerCase(); if(this.options.autoResize) { - this.unadulteratedHtml = $(this.element).html(); - this.unadulteratedCSS = getUnadulteratedCss(); + this.unadulteratedCSS = {width: $(this.element)[0].style.width}; this.unadulteratedOptions = $.extend({}, this.options); setupAutoResize(); } + this.quickResize = false; this.init(); this.createGallery(); } - function getUnadulteratedCss() { - var $element = $(balancedGallery.element); - //only the properties modified by the plugin - return { - width: $element[0].style.width, - height: $element[0].style.height, - background: $element.css('background'), - paddingLeft: $element.css('padding-left'), - paddingTop: $element.css('padding-top'), - overflow: $element.css('overflow'), - fontSize: $element.css('font-size') - }; - } - function setupAutoResize() { $(window).resize(function() { clearTimeout(resizeTimeout); @@ -70,59 +59,45 @@ } BalancedGallery.prototype.recreate = function () { - $(this.element).on(ALL_CHILDREN_LOADED, function() { - balancedGallery.init(); - balancedGallery.createGallery(); - }); - this.reset(); - }; - - BalancedGallery.prototype.reset = function() { - var childCount = this.elementChildren.length; - - $(this.element).html(this.unadulteratedHtml); - $(this.element).css(this.unadulteratedCSS); this.options = $.extend({}, this.unadulteratedOptions); - this.elementChildren = $(this.element).children('*'); + $(this.element).css(this.unadulteratedCSS); //used to reset width so it's calculated from browser - var loadedChildren = 0; - this.elementChildren.each(function() { - $(this).load(function() { - if(++loadedChildren === childCount) { - $(balancedGallery.element).trigger(ALL_CHILDREN_LOADED); - } - }); - }); + balancedGallery.quickResize = true; + balancedGallery.init(); + balancedGallery.createGallery(); }; BalancedGallery.prototype.init = function () { - if(this.options.viewportWidth === null) { - this.options.viewportWidth = $(this.element).width(); - } - if(this.options.viewportHeight === null) { - this.options.viewportHeight = $(this.element).height(); + if(balancedGallery.quickResize === false) { + this.elementChildren.each(function() { + $(this).css({display: 'inline-block', padding: 0, margin: 0}); + }); + + var padding = this.options.padding + 'px'; + $(this.container).css({fontSize: 0, paddingTop: padding, paddingLeft: padding}); + + if(this.options.background !== null) { + $(this.container).css({background: this.options.background}); + } } + this.options.viewportWidth = $(this.element).width() - this.options.padding; + if(this.options.idealWidth === null) { - this.options.idealWidth = $(this.element).width() / 4; + this.options.idealWidth = this.options.viewportWidth / this.options.divisor; } if(this.options.idealHeight === null) { - this.options.idealHeight = $(this.element).height() / 2; - } - - if(this.options.background !== null) { - $(this.element).css({background: this.options.background}); + this.options.idealHeight = this.options.viewportWidth / this.options.divisor; } - this.elementChildren.css({display: 'inline-block', padding: 0, margin: 0}); - var padding = this.options.padding + 'px'; - $(this.element).css({fontSize: 0, paddingTop: padding, paddingLeft: padding}); + //setting explicit width prevents image wrapping on resizing + $(this.container).width(this.options.viewportWidth); }; BalancedGallery.prototype.createGallery = function() { - var orientation = (this.options.orientation).toLowerCase(); + var orientation = this.options.orientation; if(orientation === 'horizontal') { createHorizontalGallery(); } else if(orientation === 'vertical') { @@ -134,10 +109,10 @@ function createHorizontalGallery() { var rows, weights, partitions; - rows = getRows(); - if(rows === 0) { - balancedGallery.fallbackToStandardSize(); + if(balancedGallery.quickResize) { + quickResizeHorizontal(); } else { + rows = getRows(); weights = getWidthWeights(); partitions = getPartitions(weights, rows); resizeHorizontalElements(partitions); @@ -146,10 +121,10 @@ function createVerticalGallery() { var cols, weights, partitions; - cols = getColumns(); - if(cols === 0) { - balancedGallery.fallbackToStandardSize(); + if(balancedGallery.quickResize) { + quickResizeVertical(); } else { + cols = getColumns(); weights = getHeightWeights(); partitions = getPartitions(weights, cols); orientElementsVertically(partitions); @@ -158,11 +133,21 @@ } function getRows () { - return Math.round( collectiveIdealWidth() / (balancedGallery.options.viewportWidth - balancedGallery.options.padding) ); + var rows = Math.round(collectiveIdealWidth() / balancedGallery.options.viewportWidth); + if(rows > 0 && rows < 1) { // if it's BETWEEN 0 and 1 means there is at least one image + rows = 1; + } + return rows; } function getColumns() { - return Math.round( collectiveIdealHeight() / (balancedGallery.options.viewportHeight - balancedGallery.options.padding) ); + var cols = Math.round(balancedGallery.options.viewportWidth / balancedGallery.options.idealWidth); + var elements = balancedGallery.elementChildren.length; + if(cols <= elements) { + return cols; + } else { + return elements; + } } function collectiveIdealWidth() { @@ -173,40 +158,20 @@ return sum; } - function collectiveIdealHeight() { - var sum = 0; - balancedGallery.elementChildren.each(function () { - sum += idealHeight($(this)); - }); - return sum; - } - function idealWidth($image) { - return aspectRatio($image) * (balancedGallery.options.idealHeight + balancedGallery.options.padding); - } - - function idealHeight($image) { - return (1/aspectRatio($image)) * (balancedGallery.options.idealWidth + balancedGallery.options.padding); + return (aspectRatio($image) * balancedGallery.options.idealHeight) + balancedGallery.options.padding; } - BalancedGallery.prototype.fallbackToStandardSize = function() { - var idealHeight = this.options.idealHeight; - this.elementChildren.each(function () { - $(this).height( idealHeight ); - $(this).width( balancedGallery.idealWidth($(this)) ); - }); - }; - function getWidthWeights() { return balancedGallery.elementChildren.map(function () { - var weight = parseInt( aspectRatio($(this)) * 100, RADIX ); + var weight = aspectRatio($(this)); return {element: this, weight: weight }; }); } function getHeightWeights() { return balancedGallery.elementChildren.map(function () { - var weight = parseInt( (1/aspectRatio($(this))) * 100, RADIX ); + var weight = 1/aspectRatio($(this)); return {element: this, weight: weight }; }); } @@ -231,17 +196,14 @@ return []; } - if(sections >= elementCount) { - return weights.map(function(key, value) { return [([value])]; }); - } - var solution = createSolutionTable(weights, sections); elementCount -= 1; sections -= 2; var partitions = []; + var results = []; while(sections >= 0) { - var results = []; + results = []; for(var f = (solution[elementCount-1][sections]+1); f < elementCount+1; f++){ results.push(weights[f]); } @@ -263,7 +225,7 @@ var elementCount = weights.length; var table = []; - for (var i = 0; i < elementCount; i++) { + for(var i = 0; i < elementCount; i++) { var res = []; for (var j = 0; j < sections; j++) { res.push(0); @@ -272,7 +234,7 @@ } var solution = []; - for (var k = 0; k < elementCount-1; k++) { + for(var k = 0; k < elementCount-1; k++) { var res2 = []; for (var l = 0; l < sections-1; l++) { res2.push(0); @@ -307,7 +269,7 @@ var sortedWeights = weights.sort(function(a,b){ return b.weight - a.weight; }); var partitions = new Array(sections); - for (var i=0; i = THRESHOLD) { + parsedWidth += 1; + overflow -= 1; } + return parsedWidth; } - function orientElementsVertically(partitions) { - var $element = $(balancedGallery.element), - $container; - $element.html(''); //clear the images - $element.css({overflow: 'scroll'}); - if(balancedGallery.element !== document.body) { - $container = $(''); - $element.append($container[0]); - } else { - $container = $(balancedGallery.element); + //if a scrollbar appears or disappears after resizing + function checkWidth(orientation) { + if((balancedGallery.options.viewportWidth + balancedGallery.options.padding) !== $(balancedGallery.element).width()) { + balancedGallery.options.viewportWidth = $(balancedGallery.element).width() - balancedGallery.options.padding; + $(balancedGallery.container).width(balancedGallery.options.viewportWidth); + if(orientation == 'horizontal') { + quickResizeHorizontal(); + } else if(orientation == 'vertical') { + quickResizeVertical(); + } } - balancedGallery.container = $container[0]; + } + function orientElementsVertically(partitions) { + var $container = $(balancedGallery.container); + $container.html(''); //clear the images for(var i = 0; i < partitions.length; i++) { var colName = 'balanced-gallery-col'+i; - var column = ''; + var column = ''; $container.append(column); + var $col = $($container.find("div#"+colName)); for(var j = 0; j < partitions[i].length; j++) { var child = partitions[i][j].element; - var $col = $($container.find("div#"+colName)); - $col.append(child).append('
'); + $col.append(child).append('
'); } } + //add clearing div + var clearingDiv = ''; + $container.append(clearingDiv); } function aspectRatio($image) { - var padding = balancedGallery.options.padding; - return ($image.width()+padding) / ($image.height()+padding); + return $image[0].naturalWidth / $image[0].naturalHeight; } function shuffleArray(array) { From a7712186016461e3908b6d51281a99b6a3503fa8 Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 25 Feb 2017 12:20:34 +0100 Subject: [PATCH 02/24] Added fix for Firefox on vertical galleries that prevents columns from wrapping --- jquery.balanced-gallery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index cd34ccb..bd178bb 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -470,7 +470,7 @@ var $col = $($container.find("div#"+colName)); for(var j = 0; j < partitions[i].length; j++) { var child = partitions[i][j].element; - $col.append(child).append('
'); + $col.append(child).append('
'); //Fix for Firefox; without 'style="display: block;"' Firefox assigns a width for an
-element. Strange! } } From 3ebb23ddcf64ee19f33a8e6f6ca2a71ca4c1b0fe Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 25 Feb 2017 13:09:37 +0100 Subject: [PATCH 03/24] Added function to create vertical galleries with the exact same height. Now the width and height of the galleries are always an exact rectangle. --- jquery.balanced-gallery.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index bd178bb..c646050 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -407,6 +407,7 @@ } checkWidth(balancedGallery.options.orientation); + alignColumnHeights(); } function quickResizeVertical() { @@ -433,6 +434,7 @@ } checkWidth(balancedGallery.options.orientation); + alignColumnHeights(); } //ensures that the rows or all columns are as wide as the container width @@ -446,6 +448,31 @@ return parsedWidth; } + //stretch or shrink image by image, pixel by pixel to get exact same column heights + function alignColumnHeights() { + var averageHeight = 0; + var imagesPerCol = 0; + for(var i = 0; i < balancedGallery.resizingValue.length; i++) { + averageHeight += balancedGallery.resizingValue[i].columnHeight; + } + averageHeight = Math.round(averageHeight / balancedGallery.resizingValue.length); + for(var j = 0; j < balancedGallery.resizingValue.length; j++) { + imagesPerCol += balancedGallery.resizingValue[j].length; + var counter = Math.sign(averageHeight - balancedGallery.resizingValue[j].columnHeight); + //starting with last image in column, because it's more likely to be outside of the current viewport + var k = imagesPerCol-1; + while(averageHeight != balancedGallery.resizingValue[j].columnHeight) { + balancedGallery.elementChildren[k].height(balancedGallery.elementChildren[k].height() + counter); + balancedGallery.resizingValue[j].columnHeight += counter; + k--; + //if all images in a column got streched/shrinked, start iteration again with last image in column till averageHeight matches columnHeight + if(k < (imagesPerCol - balancedGallery.resizingValue[j].length)) { + k = imagesPerCol-1; + } + } + } + } + //if a scrollbar appears or disappears after resizing function checkWidth(orientation) { if((balancedGallery.options.viewportWidth + balancedGallery.options.padding) !== $(balancedGallery.element).width()) { From 8f0393eecf690c0d588c0ee750b203cf3f5878cb Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 25 Feb 2017 14:23:04 +0100 Subject: [PATCH 04/24] Fix for galleries that are directly inside the body-element. Some third-party addons place elements in there that cause BalancedGallery stop working correctly. --- jquery.balanced-gallery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index c646050..f712438 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -33,7 +33,7 @@ this.element = element; $(this.element).wrapInner(''); this.container = $(this.element).children()[0]; - this.elementChildren = $(this.container).children('*'); + this.elementChildren = $(this.container).children('img'); this.options = $.extend( {}, defaults, options); // merge arg options and defaults this.options.orientation = (this.options.orientation).toLowerCase(); From b860e64ecf5d428230f0b2423ec9d42addbf5409 Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 25 Feb 2017 14:41:04 +0100 Subject: [PATCH 05/24] Fix in column ratio calculation on vertical galleries. Now the results after resizing are better so less work needs to be done later in allignColumnHeights() --- jquery.balanced-gallery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index f712438..6cb975a 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -382,7 +382,7 @@ columnRatio[i] += partitions[i][j].weight; } // also add ratios from padding bars between each image in a column - columnRatio[i] += partitions[i].length * (padding / balancedGallery.options.idealWidth); + columnRatio[i] += partitions[i].length * (padding / (balancedGallery.options.idealWidth - padding)); summedColRatios += columnRatio[i]; } balancedGallery.summedColRatios = summedColRatios; From ad0ba17a2b7af6608833f93007d2c43359c8a8a8 Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 25 Feb 2017 15:52:44 +0100 Subject: [PATCH 06/24] Bug fix: When the scrollbar disappears after resizing and after checkWidth() it appears again, then this would cause an infinite loop. --- jquery.balanced-gallery.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index 6cb975a..270d56f 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -68,7 +68,6 @@ }; BalancedGallery.prototype.init = function () { - if(balancedGallery.quickResize === false) { this.elementChildren.each(function() { $(this).css({display: 'inline-block', padding: 0, margin: 0}); @@ -117,6 +116,7 @@ partitions = getPartitions(weights, rows); resizeHorizontalElements(partitions); } + checkWidth(balancedGallery.options.orientation); } function createVerticalGallery() { @@ -130,6 +130,8 @@ orientElementsVertically(partitions); resizeVerticalElements(partitions); } + checkWidth(balancedGallery.options.orientation); + alignColumnHeights(); } function getRows () { @@ -339,8 +341,6 @@ $image.css({margin: 0, marginRight: padding+'px', marginBottom: padding+'px'}); } } - - checkWidth(balancedGallery.options.orientation); } function quickResizeHorizontal() { @@ -364,8 +364,6 @@ $image.width(imgWidth); $image.height(imgHeight); } - - checkWidth(balancedGallery.options.orientation); } function resizeVerticalElements(partitions) { @@ -405,9 +403,6 @@ } balancedGallery.resizingValue[k] = {ratio: resizingFactor, length: partitions[k].length, columnHeight: columnHeight}; } - - checkWidth(balancedGallery.options.orientation); - alignColumnHeights(); } function quickResizeVertical() { @@ -432,9 +427,6 @@ $image.width(imgWidth); $image.height(imgHeight); } - - checkWidth(balancedGallery.options.orientation); - alignColumnHeights(); } //ensures that the rows or all columns are as wide as the container width @@ -452,6 +444,7 @@ function alignColumnHeights() { var averageHeight = 0; var imagesPerCol = 0; + for(var i = 0; i < balancedGallery.resizingValue.length; i++) { averageHeight += balancedGallery.resizingValue[i].columnHeight; } From a038bb3a4bf74bd5df59bc42ea6e4ecc7348834f Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 25 Feb 2017 16:00:56 +0100 Subject: [PATCH 07/24] Updated minified version --- jquery.balanced-gallery.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.balanced-gallery.min.js b/jquery.balanced-gallery.min.js index d98bda0..6422cc4 100644 --- a/jquery.balanced-gallery.min.js +++ b/jquery.balanced-gallery.min.js @@ -1 +1 @@ -!function(t,e,n){"use strict";function i(e,n){I=this,this.element=e,this.elementChildren=t(e).children("*"),this.options=t.extend({},T,n),this.options.autoResize&&(this.unadulteratedHtml=t(this.element).html(),this.unadulteratedCSS=r(),this.unadulteratedOptions=t.extend({},this.options),o()),this.init(),this.createGallery()}function r(){var e=t(I.element);return{width:e[0].style.width,height:e[0].style.height,background:e.css("background"),paddingLeft:e.css("padding-left"),paddingTop:e.css("padding-top"),overflow:e.css("overflow"),fontSize:e.css("font-size")}}function o(){t(e).resize(function(){clearTimeout(G),G=setTimeout(function(){I.recreate()},500)})}function a(){var t,e,n;t=h(),0===t?I.fallbackToStandardSize():(e=f(),n=v(e,t),S(n))}function l(){var t,e,n;t=s(),0===t?I.fallbackToStandardSize():(e=g(),n=v(e,t),z(n),k(n))}function h(){return Math.round(d()/(I.options.viewportWidth-I.options.padding))}function s(){return Math.round(u()/(I.options.viewportHeight-I.options.padding))}function d(){var e=0;return I.elementChildren.each(function(){e+=p(t(this))}),e}function u(){var e=0;return I.elementChildren.each(function(){e+=c(t(this))}),e}function p(t){return W(t)*(I.options.idealHeight+I.options.padding)}function c(t){return 1/W(t)*(I.options.idealWidth+I.options.padding)}function f(){return I.elementChildren.map(function(){var e=parseInt(100*W(t(this)),R);return{element:this,weight:e}})}function g(){return I.elementChildren.map(function(){var e=parseInt(100*(1/W(t(this))),R);return{element:this,weight:e}})}function v(t,e){if(I.options.maintainOrder)return m(t,e);var n=y(t,e);return I.options.shuffleUnorderedPartitions&&(n=H(n)),C(n),n}function m(t,e){var n=t.length;if(0>=e)return[];if(e>=n)return t.map(function(t,e){return[[e]]});var i=w(t,e);n-=1,e-=2;for(var r=[];e>=0;){for(var o=[],a=i[n-1][e]+1;n+1>a;a++)o.push(t[a]);r=[o].concat(r),n=i[n-1][e],e-=1}for(var l=[],h=0;n+1>h;h++)l.push(t[h]);return[l].concat(r)}function w(t,e){for(var n=t.length,i=[],r=0;n>r;r++){for(var o=[],a=0;e>a;a++)o.push(0);i.push(o)}for(var l=[],h=0;n-1>h;h++){for(var s=[],d=0;e-1>d;d++)s.push(0);l.push(s)}for(var u=0;n>u;u++)i[u][0]=t[u].weight+(0!==u?i[u-1][0]:0);for(var p=0;e>p;p++)i[0][p]=t[0].weight;for(var c=function(t,e){return t[0]-e[0]},f=1;n>f;f++)for(var g=1;e>g;g++){for(var v=[],m=0;f>m;m++)v.push([Math.max(i[m][g-1],i[f][0]-i[m][0]),m]);var w=v.sort(c)[0];i[f][g]=w[0],l[f-1][g-1]=w[1]}return l}function y(t,e){for(var n=t.sort(function(t,e){return e.weight-t.weight}),i=new Array(e),r=0;e>r;r++)i[r]=[];for(var o=0;os&&(a=i[h],l=s)}a.push(n[o])}return i}function b(e){var n=0;return t.each(e,function(t,e){n+=e.weight}),n}function H(t){for(var e=0;e'),r.append(i[0])):i=t(I.element),I.container=i[0];for(var o=0;o';i.append(l);for(var h=0;h")}}}function W(t){var e=I.options.padding;return(t.width()+e)/(t.height()+e)}function x(t){for(var e,n,i=t.length;i--;)n=0|Math.random()*i,e=t[i],t[i]=t[n],t[n]=e;return t}var I,L="BalancedGallery",T={autoResize:!0,background:null,idealHeight:null,idealWidth:null,maintainOrder:!0,orientation:"horizontal",padding:5,shuffleUnorderedPartitions:!0,viewportHeight:null,viewportWidth:null},O="ALL_CHILDREN_LOADED",G=null,R=10;t.fn[L]=function(e){return this.each(function(){t.data(this,"plugin_"+L)||t.data(this,"plugin_"+L,new i(this,e))})},i.prototype.recreate=function(){t(this.element).on(O,function(){I.init(),I.createGallery()}),this.reset()},i.prototype.reset=function(){var e=this.elementChildren.length;t(this.element).html(this.unadulteratedHtml),t(this.element).css(this.unadulteratedCSS),this.options=t.extend({},this.unadulteratedOptions),this.elementChildren=t(this.element).children("*");var n=0;this.elementChildren.each(function(){t(this).load(function(){++n===e&&t(I.element).trigger(O)})})},i.prototype.init=function(){null===this.options.viewportWidth&&(this.options.viewportWidth=t(this.element).width()),null===this.options.viewportHeight&&(this.options.viewportHeight=t(this.element).height()),null===this.options.idealWidth&&(this.options.idealWidth=t(this.element).width()/4),null===this.options.idealHeight&&(this.options.idealHeight=t(this.element).height()/2),null!==this.options.background&&t(this.element).css({background:this.options.background}),this.elementChildren.css({display:"inline-block",padding:0,margin:0});var e=this.options.padding+"px";t(this.element).css({fontSize:0,paddingTop:e,paddingLeft:e})},i.prototype.createGallery=function(){var t=this.options.orientation.toLowerCase();if("horizontal"===t)a();else{if("vertical"!==t)throw"BalancedGallery: Invalid Orientation.";l()}},i.prototype.fallbackToStandardSize=function(){var e=this.options.idealHeight;this.elementChildren.each(function(){t(this).height(e),t(this).width(I.idealWidth(t(this)))})}}(jQuery,window,document); \ No newline at end of file +!function(a,b,c,d){"use strict";function l(b,c){f=this,this.element=b,a(this.element).wrapInner(''),this.container=a(this.element).children()[0],this.elementChildren=a(this.container).children("img"),this.options=a.extend({},g,c),this.options.orientation=this.options.orientation.toLowerCase(),this.options.autoResize&&(this.unadulteratedCSS={width:a(this.element)[0].style.width},this.unadulteratedOptions=a.extend({},this.options),m()),this.quickResize=!1,this.init(),this.createGallery()}function m(){a(b).resize(function(){clearTimeout(h),h=setTimeout(function(){f.recreate()},500)})}function n(){var a,b,c;f.quickResize?D():(a=p(),b=t(),c=v(b,a),C(c)),I(f.options.orientation)}function o(){var a,b,c;f.quickResize?F():(a=q(),b=u(),c=v(b,a),J(c),E(c)),I(f.options.orientation),H()}function p(){var a=Math.round(r()/f.options.viewportWidth);return a>0&&a<1&&(a=1),a}function q(){var a=Math.round(f.options.viewportWidth/f.options.idealWidth),b=f.elementChildren.length;return a<=b?a:b}function r(){var b=0;return f.elementChildren.each(function(){b+=s(a(this))}),b}function s(a){return K(a)*f.options.idealHeight+f.options.padding}function t(){return f.elementChildren.map(function(){var b=K(a(this));return{element:this,weight:b}})}function u(){return f.elementChildren.map(function(){var b=1/K(a(this));return{element:this,weight:b}})}function v(a,b){if(f.options.maintainOrder)return w(a,b);var c=y(a,b);return f.options.shuffleUnorderedPartitions&&(c=A(c)),B(c),c}function w(a,b){var c=a.length;if(b<=0)return[];var d=x(a,b);c-=1,b-=2;for(var e=[],f=[];b>=0;){f=[];for(var g=d[c-1][b]+1;g=j&&(b+=1,k-=1),b}function H(){for(var a=0,b=0,c=0;c';c.append(g);for(var h=a(c.find("div#"+e)),i=0;i')}}var k='';c.append(k)}function K(a){return a[0].naturalWidth/a[0].naturalHeight}function L(a){for(var c,d,b=a.length;b--;)d=Math.random()*b|0,c=a[b],a[b]=a[d],a[d]=c;return a}var f,e="BalancedGallery",g={autoResize:!0,background:null,idealHeight:null,idealWidth:null,maintainOrder:!0,orientation:"horizontal",padding:5,shuffleUnorderedPartitions:!0,divisor:4},h=null,i=10,j=.99999,k=0;a.fn[e]=function(b){return this.each(function(){a.data(this,"plugin_"+e)||a.data(this,"plugin_"+e,new l(this,b))})},l.prototype.recreate=function(){this.options=a.extend({},this.unadulteratedOptions),a(this.element).css(this.unadulteratedCSS),f.quickResize=!0,f.init(),f.createGallery()},l.prototype.init=function(){if(f.quickResize===!1){this.elementChildren.each(function(){a(this).css({display:"inline-block",padding:0,margin:0})});var b=this.options.padding+"px";a(this.container).css({fontSize:0,paddingTop:b,paddingLeft:b}),null!==this.options.background&&a(this.container).css({background:this.options.background})}this.options.viewportWidth=a(this.element).width()-this.options.padding,null===this.options.idealWidth&&(this.options.idealWidth=this.options.viewportWidth/this.options.divisor),null===this.options.idealHeight&&(this.options.idealHeight=this.options.viewportWidth/this.options.divisor),a(this.container).width(this.options.viewportWidth)},l.prototype.createGallery=function(){var a=this.options.orientation;if("horizontal"===a)n();else{if("vertical"!==a)throw"BalancedGallery: Invalid Orientation.";o()}}}(jQuery,window,document); \ No newline at end of file From 792ac6470fa4f5ed4889758e2c89de5f95b22358 Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 25 Feb 2017 17:27:11 +0100 Subject: [PATCH 08/24] Adjusted documentation --- README.md | 14 +++++++------- demo_small_box.html | 4 ++-- jquery.balanced-gallery.js | 6 +++--- jquery.balanced-gallery.min.js | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 46425de..b4c9c89 100644 --- a/README.md +++ b/README.md @@ -29,16 +29,16 @@ Options ------- ``` javascript var defaults = { - autoResize: true, // re-partition and resize the images when the window size changes + autoResize: true, // resize the images when the window size changes background: null, // the css properties of the gallery's containing element - idealHeight: null, // ideal row height, only used for horizontal galleries, defaults to half the containing element's height - idealWidth: null, // ideal column width, only used for vertical galleries, defaults to 1/4 of the containing element's width + idealHeight: null, // ideal row height, only used for horizontal galleries, defaults to 1/4 of the container width + idealWidth: null, // ideal column width, only used for vertical galleries, defaults to 1/4 of the container width maintainOrder: true, // keeps images in their original order, setting to 'false' can create a slightly better balance between rows - orientation: 'horizontal', // 'horizontal' galleries are made of rows and scroll vertically; 'vertical' galleries are made of columns and scroll horizontally + orientation: 'horizontal', // 'horizontal' galleries are made of rows and scroll vertically; 'vertical' galleries are made of columns and also scroll vertically padding: 5, // pixels between images - shuffleUnorderedPartitions: true, // unordered galleries tend to clump larger images at the begining, this solves that issue at a slight performance cost - viewportHeight: null, // the assumed height of the gallery, defaults to the containing element's height - viewportWidth: null // the assumed width of the gallery, defaults to the containing element's width + shuffleUnorderedPartitions: true, // unordered galleries tend to clump larger images at the beginning, this solves that issue at a slight performance cost + widthDivisor: 4 // used to define idealHeight/idealWidth (when they aren't set) by dividing the elements container width by the given value; + // the bigger the widthDivisor value, the smaller the images get; on vertical galleries the value simply defines the number of columns }; ``` diff --git a/demo_small_box.html b/demo_small_box.html index 0408d83..d09b887 100644 --- a/demo_small_box.html +++ b/demo_small_box.html @@ -14,13 +14,13 @@ -';c.append(g);for(var h=a(c.find("div#"+e)),i=0;i')}}var k='';c.append(k)}function K(a){return a[0].naturalWidth/a[0].naturalHeight}function L(a){for(var c,d,b=a.length;b--;)d=Math.random()*b|0,c=a[b],a[b]=a[d],a[d]=c;return a}var f,e="BalancedGallery",g={autoResize:!0,background:null,idealHeight:null,idealWidth:null,maintainOrder:!0,orientation:"horizontal",padding:5,shuffleUnorderedPartitions:!0,widthDivisor:4},h=null,i=10,j=.99999,k=0;a.fn[e]=function(b){return this.each(function(){a.data(this,"plugin_"+e)||a.data(this,"plugin_"+e,new l(this,b))})},l.prototype.recreate=function(){this.options=a.extend({},this.unadulteratedOptions),a(this.element).css(this.unadulteratedCSS),f.quickResize=!0,f.init(),f.createGallery()},l.prototype.init=function(){if(f.quickResize===!1){this.elementChildren.each(function(){a(this).css({display:"inline-block",padding:0,margin:0})});var b=this.options.padding+"px";a(this.container).css({fontSize:0,paddingTop:b,paddingLeft:b}),null!==this.options.background&&a(this.container).css({background:this.options.background})}this.options.viewportWidth=a(this.element).width()-this.options.padding,null===this.options.idealWidth&&(this.options.idealWidth=this.options.viewportWidth/this.options.widthDivisor),null===this.options.idealHeight&&(this.options.idealHeight=this.options.viewportWidth/this.options.widthDivisor),a(this.container).width(this.options.viewportWidth)},l.prototype.createGallery=function(){var a=this.options.orientation;if("horizontal"===a)n();else{if("vertical"!==a)throw"BalancedGallery: Invalid Orientation.";o()}}}(jQuery,window,document); \ No newline at end of file From cc74220e03eee5a4b305bf718aef4a01cd4ee86a Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 11 Mar 2017 16:20:09 +0100 Subject: [PATCH 09/24] Added new orientation: grid --- jquery.balanced-gallery.js | 55 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index c2b5832..3e1500e 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -101,7 +101,10 @@ createHorizontalGallery(); } else if(orientation === 'vertical') { createVerticalGallery(); - } else { + } else if(orientation === 'grid') { + createGridGallery(); + checkWidth(orientation); + } else { throw("BalancedGallery: Invalid Orientation."); } }; @@ -133,6 +136,47 @@ checkWidth(balancedGallery.options.orientation); alignColumnHeights(); } + + function createGridGallery() { + var padding = balancedGallery.options.padding; + var quadratLength = parseInt(balancedGallery.options.idealWidth) - padding; + var wrapper = '
'; + var paddingGap = parseInt(((balancedGallery.options.viewportWidth % balancedGallery.options.widthDivisor) / 2), RADIX); + + //if necessary, increase the padding on the left side of the container so the grid is more centered + if(paddingGap > 0) { + var increasedPadding = paddingGap + padding; + var $container = $(balancedGallery.container); + $container.css({paddingLeft: increasedPadding+'px'}); + $container.width(balancedGallery.options.viewportWidth - paddingGap); + } + + balancedGallery.elementChildren.each(function(){ + var $image = $(this); + var ratio = aspectRatio($image); + var offset = 0; + + if(ratio >= 1) { + $image.height(quadratLength); + $image.width(quadratLength * ratio); + offset = (((quadratLength * ratio) - quadratLength) / 2) * -1; + $image.css({left: offset+'px', position: 'absolute'}); + } else if(ratio < 1) { + $image.width(quadratLength); + $image.height(quadratLength * (1/ratio)); + offset = (((quadratLength * (1/ratio)) - quadratLength) / 2) * -1; + $image.css({top: offset+'px', position: 'absolute'}); + } + + if(balancedGallery.quickResize) { + var $div = $image.parent(); + $div.width(quadratLength); + $div.height(quadratLength); + } else { + $image.wrap(wrapper); + } + }); + } function getRows () { var rows = Math.round(collectiveIdealWidth() / balancedGallery.options.viewportWidth); @@ -475,7 +519,10 @@ quickResizeHorizontal(); } else if(orientation == 'vertical') { quickResizeVertical(); - } + } else if(orientation == 'grid') { + balancedGallery.options.idealWidth = balancedGallery.options.viewportWidth / balancedGallery.options.widthDivisor; + createGridGallery(); + } } } @@ -498,6 +545,10 @@ var clearingDiv = ''; $container.append(clearingDiv); } + + function wrapImages() { + + } function aspectRatio($image) { return $image[0].naturalWidth / $image[0].naturalHeight; From 68a28d8a47b68bbc5bc00e2db3014fcf09bf329b Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Sat, 11 Mar 2017 19:27:36 +0100 Subject: [PATCH 10/24] Added new demo files and adjusted documentation --- README.md | 2 +- balancedgallery.jquery.json | 2 +- demo.html => demo_grid.html | 6 ++- demo_small_box.html => demo_horizontal.html | 2 +- demo_vertical.html | 42 +++++++++++++++++++++ jquery.balanced-gallery.js | 6 +-- jquery.balanced-gallery.min.js | 3 +- 7 files changed, 53 insertions(+), 10 deletions(-) rename demo.html => demo_grid.html (91%) rename demo_small_box.html => demo_horizontal.html (96%) create mode 100644 demo_vertical.html diff --git a/README.md b/README.md index b4c9c89..794c421 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ var defaults = { idealHeight: null, // ideal row height, only used for horizontal galleries, defaults to 1/4 of the container width idealWidth: null, // ideal column width, only used for vertical galleries, defaults to 1/4 of the container width maintainOrder: true, // keeps images in their original order, setting to 'false' can create a slightly better balance between rows - orientation: 'horizontal', // 'horizontal' galleries are made of rows and scroll vertically; 'vertical' galleries are made of columns and also scroll vertically + orientation: 'horizontal', // 'horizontal' galleries are made of rows; 'vertical' galleries are made of columns; 'grid' galleries combine both padding: 5, // pixels between images shuffleUnorderedPartitions: true, // unordered galleries tend to clump larger images at the beginning, this solves that issue at a slight performance cost widthDivisor: 4 // used to define idealHeight/idealWidth (when they aren't set) by dividing the elements container width by the given value; diff --git a/balancedgallery.jquery.json b/balancedgallery.jquery.json index 105ed16..f6f6414 100644 --- a/balancedgallery.jquery.json +++ b/balancedgallery.jquery.json @@ -15,5 +15,5 @@ "dependencies" : { "jquery": ">=1.5" }, -"description" : "Balanced Gallery is a jQuery plugin that evenly distributes photos across rows or columns, making the most of the space provided. Photos are scaled based on the size of the 'container' element by default, making Balanced Gallery a great choice for (responsive) websites." +"description" : "Balanced Gallery is a jQuery plugin that evenly distributes photos across rows, columns or a grid, making the most of the space provided. Photos are scaled based on the size of the 'container' element by default, making Balanced Gallery a great choice for (responsive) websites." } \ No newline at end of file diff --git a/demo.html b/demo_grid.html similarity index 91% rename from demo.html rename to demo_grid.html index b51680d..5a98c4f 100644 --- a/demo.html +++ b/demo_grid.html @@ -14,11 +14,13 @@ + + + diff --git a/demo_small_box.html b/demo_horizontal.html similarity index 96% rename from demo_small_box.html rename to demo_horizontal.html index d09b887..8bd126f 100644 --- a/demo_small_box.html +++ b/demo_horizontal.html @@ -20,7 +20,7 @@ -'),c=g(d.find("div#"+c)),h=0;h');d.append('');d=b.options.padding;f=[];h=c=0;b.resizingValue=[];for(e=r=0;e Date: Mon, 13 Mar 2017 15:21:29 +0100 Subject: [PATCH 11/24] Added autoResize support for multiple galleries on a page --- jquery.balanced-gallery.js | 35 ++++++++++++++++++++-------------- jquery.balanced-gallery.min.js | 3 +-- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index 418d712..c7d7ae1 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -1,8 +1,9 @@ -;(function ( $, window, document, undefined ) { +(function ($, window, undefined) { "use strict"; var pluginName = 'BalancedGallery', - balancedGallery, + galleries = [], + balancedGallery, defaults = { autoResize: true, background: null, @@ -20,21 +21,22 @@ overflow = 0; //this wrapper prevents multiple instantiations of the plugin: - $.fn[pluginName] = function ( options ) { + $.fn[pluginName] = function (options) { return this.each(function () { if (!$.data(this, 'plugin_' + pluginName)) { - $.data(this, 'plugin_' + pluginName, new BalancedGallery( this, options )); + $.data(this, 'plugin_' + pluginName, new BalancedGallery(this, options)); } }); }; - function BalancedGallery( element, options ) { - balancedGallery = this; // for contexts when 'this' doesn't refer to the BalancedGallery class. + function BalancedGallery(element, options) { + balancedGallery = this // for contexts when 'this' doesn't refer to the BalancedGallery class. + galleries.push(this); this.element = element; $(this.element).wrapInner(''); this.container = $(this.element).children()[0]; this.elementChildren = $(this.container).children('img'); - this.options = $.extend( {}, defaults, options); // merge arg options and defaults + this.options = $.extend({}, defaults, options); // merge arg options and defaults this.options.orientation = (this.options.orientation).toLowerCase(); if(this.options.autoResize) { @@ -53,7 +55,12 @@ clearTimeout(resizeTimeout); resizeTimeout = setTimeout(function() { - balancedGallery.recreate(); + for(var index = 0; index < galleries.length; index++) { + if(galleries[index].options.autoResize) { + balancedGallery = galleries[index]; + galleries[index].recreate(); + } + } }, 500); }); } @@ -62,13 +69,13 @@ this.options = $.extend({}, this.unadulteratedOptions); $(this.element).css(this.unadulteratedCSS); //used to reset width so it's calculated from browser - balancedGallery.quickResize = true; - balancedGallery.init(); - balancedGallery.createGallery(); + this.quickResize = true; + this.init(); + this.createGallery(); }; BalancedGallery.prototype.init = function () { - if(balancedGallery.quickResize === false) { + if(this.quickResize === false) { this.elementChildren.each(function() { $(this).css({display: 'inline-block', padding: 0, margin: 0}); }); @@ -300,7 +307,7 @@ for(var q = 1; q < sections; q++) { var results = []; for (var r = 0; r < p; r++) { - results.push([ Math.max( table[r][q - 1], table[p][0]-table[r][0] ), r ]); + results.push([Math.max(table[r][q - 1], table[p][0]-table[r][0]), r]); } var arr = results.sort(subArraySort)[0]; table[p][q] = arr[0]; @@ -561,4 +568,4 @@ return array; } -})( jQuery, window, document ); \ No newline at end of file +}(jQuery, window)); \ No newline at end of file diff --git a/jquery.balanced-gallery.min.js b/jquery.balanced-gallery.min.js index ef23998..a18f9d9 100644 --- a/jquery.balanced-gallery.min.js +++ b/jquery.balanced-gallery.min.js @@ -1,2 +1 @@ -(function(g,E,M,N){function n(a,d){b=this;this.element=a;g(this.element).wrapInner('');this.container=g(this.element).children()[0];this.elementChildren=g(this.container).children("img");this.options=g.extend({},F,d);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:g(this.element)[0].style.width},this.unadulteratedOptions=g.extend({},this.options),G());this.quickResize=!1;this.init();this.createGallery()}function G(){g(E).resize(function(){clearTimeout(t);t=setTimeout(function(){b.recreate()},500)})}function y(){var a=b.options.padding,d=parseInt(b.options.idealWidth,10)-a,f='
',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10);if(0c&&(a.width(d),a.height(1/c*d),c=(1/c*d-d)/2*-1,a.css({top:c+"px",position:"absolute"}));b.quickResize?(a=a.parent(),a.width(d),a.height(d)):a.wrap(f)})}function H(){var a=0;b.elementChildren.each(function(){a+=p(g(this))*b.options.idealHeight+b.options.padding});return a}function I(){return b.elementChildren.map(function(){return{element:this,weight:p(g(this))}})}function J(){return b.elementChildren.map(function(){return{element:this,weight:1/p(g(this))}})}function z(a,d){if(b.options.maintainOrder){var f;var c=d;f=a.length;if(0>=c)f=[];else{var h=K(a,c);--f;for(var c=c-2,e=[],k;0<=c;){k=[];for(var m=h[f-1][c]+1;ma&&(a=1);d=I();a=z(d,a);d=b.options.padding;var f=0;b.resizingValue=[];for(var c=0;c'),c=g(d.find("div#"+c)),h=0;h');d.append('');d=b.options.padding;f=[];h=c=0;b.resizingValue=[];for(e=r=0;e');this.container=g(this.element).children()[0];this.elementChildren=g(this.container).children("img");this.options=g.extend({},F,d);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:g(this.element)[0].style.width},this.unadulteratedOptions=g.extend({},this.options),G());this.quickResize=!1;this.init();this.createGallery()}function G(){g(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10);if(0c&&(a.width(d),a.height(1/c*d),c=(1/c*d-d)/2*-1,a.css({top:c+"px",position:"absolute"}));b.quickResize?(a=a.parent(),a.width(d),a.height(d)):a.wrap(f)})}function H(){var a=0;b.elementChildren.each(function(){a+=q(g(this))*b.options.idealHeight+b.options.padding});return a}function I(){return b.elementChildren.map(function(){return{element:this,weight:q(g(this))}})}function J(){return b.elementChildren.map(function(){return{element:this,weight:1/q(g(this))}})}function z(a,d){if(b.options.maintainOrder){var f;var c=d;f=a.length;if(0>=c)f=[];else{var h=K(a,c);--f;for(var c=c-2,e=[],k;0<=c;){k=[];for(var m=h[f-1][c]+1;ma&&(a=1);d=I();a=z(d,a);d=b.options.padding;var f=0;b.resizingValue=[];for(var c=0;c'),c=g(d.find("div#"+c)),h=0;h');d.append('');d=b.options.padding;f=[];h=c=0;b.resizingValue=[];for(e=t=0;e Date: Tue, 14 Mar 2017 01:07:04 +0100 Subject: [PATCH 12/24] Added option for grid galleries for different aspect ratios --- README.md | 1 + jquery.balanced-gallery.js | 32 +++++++++++++++++--------------- jquery.balanced-gallery.min.js | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 794c421..7065a11 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ var defaults = { orientation: 'horizontal', // 'horizontal' galleries are made of rows; 'vertical' galleries are made of columns; 'grid' galleries combine both padding: 5, // pixels between images shuffleUnorderedPartitions: true, // unordered galleries tend to clump larger images at the beginning, this solves that issue at a slight performance cost + gridAspectRatio: 1, // the aspect ratio with which the images are going to be displayed; only used for grid galleries widthDivisor: 4 // used to define idealHeight/idealWidth (when they aren't set) by dividing the elements container width by the given value; // the bigger the widthDivisor value, the smaller the images get; on vertical galleries the value simply defines the number of columns }; diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index c7d7ae1..6bf1453 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -13,6 +13,7 @@ orientation: 'horizontal', padding: 5, shuffleUnorderedPartitions: true, + gridAspectRatio: 1, widthDivisor: 4 }, resizeTimeout = null, @@ -146,8 +147,10 @@ function createGridGallery() { var padding = balancedGallery.options.padding; - var quadratLength = parseInt(balancedGallery.options.idealWidth, RADIX) - padding; - var wrapper = '
'; + var cellRatio = balancedGallery.options.gridAspectRatio; + var cellWidth = parseInt(balancedGallery.options.idealWidth - padding, RADIX); + var cellHeight = cellWidth * (1 / cellRatio); + var wrapper = '
'; var paddingGap = parseInt(((balancedGallery.options.viewportWidth % balancedGallery.options.widthDivisor) / 2), RADIX); //if necessary, increase the padding on the left side of the container so the grid is more centered @@ -160,25 +163,25 @@ balancedGallery.elementChildren.each(function(){ var $image = $(this); - var ratio = aspectRatio($image); + var imgRatio = aspectRatio($image); var offset = 0; - if(ratio >= 1) { - $image.height(quadratLength); - $image.width(quadratLength * ratio); - offset = (((quadratLength * ratio) - quadratLength) / 2) * -1; + if(imgRatio >= cellRatio) { + $image.height(cellHeight); + $image.width(cellHeight * imgRatio); + offset = (cellWidth - (cellHeight * imgRatio)) / 2; $image.css({left: offset+'px', position: 'absolute'}); - } else if(ratio < 1) { - $image.width(quadratLength); - $image.height(quadratLength * (1/ratio)); - offset = (((quadratLength * (1/ratio)) - quadratLength) / 2) * -1; + } else if(imgRatio < cellRatio) { + $image.width(cellWidth); + $image.height(cellWidth * (1/imgRatio)); + offset = (cellHeight - (cellWidth * (1/imgRatio))) / 2; $image.css({top: offset+'px', position: 'absolute'}); } if(balancedGallery.quickResize) { var $div = $image.parent(); - $div.width(quadratLength); - $div.height(quadratLength); + $div.width(cellWidth); + $div.height(cellHeight); } else { $image.wrap(wrapper); } @@ -324,7 +327,6 @@ var partitions = new Array(sections); for (var i=0; i < sections; i++) { partitions[i] = []; } - for(var j = 0; j < sortedWeights.length; j++) { var shortestPartition = partitions[0]; var shortestPartitionWeight = getPartitionWeight(shortestPartition); @@ -338,7 +340,7 @@ } shortestPartition.push(sortedWeights[j]); } - + return partitions; } diff --git a/jquery.balanced-gallery.min.js b/jquery.balanced-gallery.min.js index a18f9d9..9615521 100644 --- a/jquery.balanced-gallery.min.js +++ b/jquery.balanced-gallery.min.js @@ -1 +1 @@ -(function(g,E,M){function p(a,d){b=this;n.push(this);this.element=a;g(this.element).wrapInner('');this.container=g(this.element).children()[0];this.elementChildren=g(this.container).children("img");this.options=g.extend({},F,d);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:g(this.element)[0].style.width},this.unadulteratedOptions=g.extend({},this.options),G());this.quickResize=!1;this.init();this.createGallery()}function G(){g(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10);if(0c&&(a.width(d),a.height(1/c*d),c=(1/c*d-d)/2*-1,a.css({top:c+"px",position:"absolute"}));b.quickResize?(a=a.parent(),a.width(d),a.height(d)):a.wrap(f)})}function H(){var a=0;b.elementChildren.each(function(){a+=q(g(this))*b.options.idealHeight+b.options.padding});return a}function I(){return b.elementChildren.map(function(){return{element:this,weight:q(g(this))}})}function J(){return b.elementChildren.map(function(){return{element:this,weight:1/q(g(this))}})}function z(a,d){if(b.options.maintainOrder){var f;var c=d;f=a.length;if(0>=c)f=[];else{var h=K(a,c);--f;for(var c=c-2,e=[],k;0<=c;){k=[];for(var m=h[f-1][c]+1;ma&&(a=1);d=I();a=z(d,a);d=b.options.padding;var f=0;b.resizingValue=[];for(var c=0;c'),c=g(d.find("div#"+c)),h=0;h');d.append('');d=b.options.padding;f=[];h=c=0;b.resizingValue=[];for(e=t=0;e');this.container=g(this.element).children()[0];this.elementChildren=g(this.container).children("img");this.options=g.extend({},F,f);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:g(this.element)[0].style.width},this.unadulteratedOptions=g.extend({},this.options),G());this.quickResize=!1;this.init();this.createGallery()}function G(){g(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10);if(0=f?(a.height(d),a.width(d*c),c=(e-d*c)/2,a.css({left:c+"px",position:"absolute"})):c=d)e=[];else{var k=K(a,d);--e;for(var d=d-2,c=[],h;0<=d;){h=[];for(var l=k[e-1][d]+1;la&&(a=1);f=I();a=z(f,a);f=b.options.padding;var e=0;b.resizingValue=[];for(var d=0;d'),d=g(f.find("div#"+d)),k=0;k');f.append('');f=b.options.padding;e=[];k=d=0;b.resizingValue=[];for(c=t=0;c Date: Tue, 21 Mar 2017 15:39:48 +0100 Subject: [PATCH 13/24] Refactoring --- jquery.balanced-gallery.js | 78 +++++++++++++++++----------------- jquery.balanced-gallery.min.js | 16 ++++++- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index 6bf1453..5ee49d2 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -30,18 +30,18 @@ }); }; - function BalancedGallery(element, options) { + function BalancedGallery(container, options) { balancedGallery = this // for contexts when 'this' doesn't refer to the BalancedGallery class. galleries.push(this); - this.element = element; - $(this.element).wrapInner(''); - this.container = $(this.element).children()[0]; - this.elementChildren = $(this.container).children('img'); + this.container = container; + $(this.container).wrapInner(''); + this.wrapper = $(this.container).children()[0]; + this.elements = $(this.wrapper).children('img'); this.options = $.extend({}, defaults, options); // merge arg options and defaults this.options.orientation = (this.options.orientation).toLowerCase(); if(this.options.autoResize) { - this.unadulteratedCSS = {width: $(this.element)[0].style.width}; + this.unadulteratedCSS = {width: $(this.container)[0].style.width}; this.unadulteratedOptions = $.extend({}, this.options); setupAutoResize(); } @@ -68,7 +68,7 @@ BalancedGallery.prototype.recreate = function () { this.options = $.extend({}, this.unadulteratedOptions); - $(this.element).css(this.unadulteratedCSS); //used to reset width so it's calculated from browser + $(this.container).css(this.unadulteratedCSS); //used to reset width so it's calculated from browser this.quickResize = true; this.init(); @@ -77,19 +77,19 @@ BalancedGallery.prototype.init = function () { if(this.quickResize === false) { - this.elementChildren.each(function() { + this.elements.each(function() { $(this).css({display: 'inline-block', padding: 0, margin: 0}); }); var padding = this.options.padding + 'px'; - $(this.container).css({fontSize: 0, paddingTop: padding, paddingLeft: padding}); + $(this.wrapper).css({fontSize: 0, paddingTop: padding, paddingLeft: padding}); if(this.options.background !== null) { - $(this.container).css({background: this.options.background}); + $(this.wrapper).css({background: this.options.background}); } } - this.options.viewportWidth = $(this.element).width() - this.options.padding; + this.options.viewportWidth = $(this.container).width() - this.options.padding; if(this.options.idealWidth === null) { this.options.idealWidth = this.options.viewportWidth / this.options.widthDivisor; @@ -100,7 +100,7 @@ } //setting explicit width prevents image wrapping on resizing - $(this.container).width(this.options.viewportWidth); + $(this.wrapper).width(this.options.viewportWidth); }; BalancedGallery.prototype.createGallery = function() { @@ -153,15 +153,15 @@ var wrapper = '
'; var paddingGap = parseInt(((balancedGallery.options.viewportWidth % balancedGallery.options.widthDivisor) / 2), RADIX); - //if necessary, increase the padding on the left side of the container so the grid is more centered + //if necessary, increase the padding on the left side of the wrapper so the grid is more centered if(paddingGap > 0) { var increasedPadding = paddingGap + padding; - var $container = $(balancedGallery.container); - $container.css({paddingLeft: increasedPadding+'px'}); - $container.width(balancedGallery.options.viewportWidth - paddingGap); + var $wrapper = $(balancedGallery.wrapper); + $wrapper.css({paddingLeft: increasedPadding+'px'}); + $wrapper.width(balancedGallery.options.viewportWidth - paddingGap); } - balancedGallery.elementChildren.each(function(){ + balancedGallery.elements.each(function(){ var $image = $(this); var imgRatio = aspectRatio($image); var offset = 0; @@ -198,7 +198,7 @@ function getColumns() { var cols = Math.round(balancedGallery.options.viewportWidth / balancedGallery.options.idealWidth); - var elements = balancedGallery.elementChildren.length; + var elements = balancedGallery.elements.length; if(cols <= elements) { return cols; } else { @@ -208,7 +208,7 @@ function collectiveIdealWidth() { var sum = 0; - balancedGallery.elementChildren.each(function () { + balancedGallery.elements.each(function () { sum += idealWidth($(this)); }); return sum; @@ -219,14 +219,14 @@ } function getWidthWeights() { - return balancedGallery.elementChildren.map(function () { + return balancedGallery.elements.map(function () { var weight = aspectRatio($(this)); return {element: this, weight: weight }; }); } function getHeightWeights() { - return balancedGallery.elementChildren.map(function () { + return balancedGallery.elements.map(function () { var weight = 1/aspectRatio($(this)); return {element: this, weight: weight }; }); @@ -360,11 +360,11 @@ } function reorderElements(partitions) { - $(balancedGallery.container).html(''); //remove all elements + $(balancedGallery.wrapper).html(''); //remove all elements for(var i = 0; i < partitions.length; i++) { var subPartition = partitions[i]; for(var j = 0; j < subPartition.length; j++) { - $(balancedGallery.container).append(subPartition[j].element); + $(balancedGallery.wrapper).append(subPartition[j].element); } } } @@ -385,7 +385,7 @@ var rawImgHeight = (balancedGallery.options.viewportWidth - rowPadding) / rowRatio; for(var k = 0; k < partitions[i].length; k++) { var $image = $(partitions[i][k].element); - balancedGallery.elementChildren[index++] = $image; + balancedGallery.elements[index++] = $image; var imgHeight = parseInt(rawImgHeight, RADIX); var imgWidth = rawImgHeight * aspectRatio($image); imgWidth = checkWidthOverflow(imgWidth); @@ -402,7 +402,7 @@ var imagesPerRow = 0; var rawImgHeight, rowPadding; - for(var i = 0; i < balancedGallery.elementChildren.length; i++) { + for(var i = 0; i < balancedGallery.elements.length; i++) { if(i == imagesPerRow) { rowPadding = padding * balancedGallery.resizingValue[index].length; rawImgHeight = (balancedGallery.options.viewportWidth - rowPadding) / balancedGallery.resizingValue[index].ratio; @@ -410,7 +410,7 @@ index++; overflow = 0; } - var $image = balancedGallery.elementChildren[i]; + var $image = balancedGallery.elements[i]; var imgHeight = parseInt(rawImgHeight, RADIX); var imgWidth = rawImgHeight * aspectRatio($image); imgWidth = checkWidthOverflow(imgWidth); @@ -447,7 +447,7 @@ var columnHeight = 0; for(var l = 0; l < partitions[k].length; l++) { var $image = $(partitions[k][l].element); - balancedGallery.elementChildren[index++] = $image; + balancedGallery.elements[index++] = $image; var imgHeight = Math.round(rawImgWidth * (1/aspectRatio($image))); columnHeight += imgHeight + padding; $image.width(imgWidth); @@ -465,7 +465,7 @@ overflow = 0; var columnWidth, rawImgWidth, imgWidth; - for(var i = 0; i < balancedGallery.elementChildren.length; i++) { + for(var i = 0; i < balancedGallery.elements.length; i++) { if(i == imagesPerCol) { index++; balancedGallery.resizingValue[index].columnHeight = 0; @@ -474,7 +474,7 @@ imgWidth = checkWidthOverflow(rawImgWidth); imagesPerCol += balancedGallery.resizingValue[index].length; } - var $image = balancedGallery.elementChildren[i]; + var $image = balancedGallery.elements[i]; var imgHeight = parseInt(rawImgWidth * (1/aspectRatio($image)), RADIX); balancedGallery.resizingValue[index].columnHeight += imgHeight + padding; $image.width(imgWidth); @@ -482,7 +482,7 @@ } } - //ensures that the rows or all columns are as wide as the container width + //ensures that the rows or all columns are as wide as the wrapper width function checkWidthOverflow(width) { var parsedWidth = parseInt(width, RADIX); overflow += width - parsedWidth; @@ -508,7 +508,7 @@ //starting with last image in column, because it's more likely to be outside of the current viewport var k = imagesPerCol-1; while(averageHeight != balancedGallery.resizingValue[j].columnHeight) { - balancedGallery.elementChildren[k].height(balancedGallery.elementChildren[k].height() + counter); + balancedGallery.elements[k].height(balancedGallery.elements[k].height() + counter); balancedGallery.resizingValue[j].columnHeight += counter; k--; //if all images in a column got streched/shrinked, start iteration again with last image in column till averageHeight matches columnHeight @@ -521,9 +521,9 @@ //if a scrollbar appears or disappears after resizing function checkWidth(orientation) { - if((balancedGallery.options.viewportWidth + balancedGallery.options.padding) !== $(balancedGallery.element).width()) { - balancedGallery.options.viewportWidth = $(balancedGallery.element).width() - balancedGallery.options.padding; - $(balancedGallery.container).width(balancedGallery.options.viewportWidth); + if((balancedGallery.options.viewportWidth + balancedGallery.options.padding) !== $(balancedGallery.container).width()) { + balancedGallery.options.viewportWidth = $(balancedGallery.container).width() - balancedGallery.options.padding; + $(balancedGallery.wrapper).width(balancedGallery.options.viewportWidth); if(orientation == 'horizontal') { quickResizeHorizontal(); } else if(orientation == 'vertical') { @@ -536,14 +536,14 @@ } function orientElementsVertically(partitions) { - var $container = $(balancedGallery.container); - $container.html(''); //clear the images + var $wrapper = $(balancedGallery.wrapper); + $wrapper.html(''); //clear the images for(var i = 0; i < partitions.length; i++) { var colName = 'balanced-gallery-col'+i; var column = ''; - $container.append(column); - var $col = $($container.find("div#"+colName)); + $wrapper.append(column); + var $col = $($wrapper.find("div#"+colName)); for(var j = 0; j < partitions[i].length; j++) { var child = partitions[i][j].element; $col.append(child).append('
'); //Fix for Firefox; without 'style="display: block;"' Firefox assigns a width for an
-element. Strange! @@ -552,7 +552,7 @@ //add clearing div var clearingDiv = ''; - $container.append(clearingDiv); + $wrapper.append(clearingDiv); } function aspectRatio($image) { diff --git a/jquery.balanced-gallery.min.js b/jquery.balanced-gallery.min.js index 9615521..90f8959 100644 --- a/jquery.balanced-gallery.min.js +++ b/jquery.balanced-gallery.min.js @@ -1 +1,15 @@ -(function(g,E,M){function p(a,f){b=this;n.push(this);this.element=a;g(this.element).wrapInner('');this.container=g(this.element).children()[0];this.elementChildren=g(this.container).children("img");this.options=g.extend({},F,f);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:g(this.element)[0].style.width},this.unadulteratedOptions=g.extend({},this.options),G());this.quickResize=!1;this.init();this.createGallery()}function G(){g(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10);if(0=f?(a.height(d),a.width(d*c),c=(e-d*c)/2,a.css({left:c+"px",position:"absolute"})):c=d)e=[];else{var k=K(a,d);--e;for(var d=d-2,c=[],h;0<=d;){h=[];for(var l=k[e-1][d]+1;la&&(a=1);f=I();a=z(f,a);f=b.options.padding;var e=0;b.resizingValue=[];for(var d=0;d'),d=g(f.find("div#"+d)),k=0;k');f.append('');f=b.options.padding;e=[];k=d=0;b.resizingValue=[];for(c=t=0;c');this.wrapper=g(this.container).children()[0];this.elements=g(this.wrapper).children("img");this.options=g.extend({},F,f);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:g(this.container)[0].style.width},this.unadulteratedOptions=g.extend({},this.options),G());this.quickResize=!1;this.init(); +this.createGallery()}function G(){g(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10); +if(0=f?(a.height(d),a.width(d*c),c=(e-d*c)/2,a.css({left:c+"px",position:"absolute"})):c=d)e=[];else{var k=K(a,d);--e;for(var d=d-2,c=[],h;0<=d;){h=[];for(var l=k[e-1][d]+1;la&&(a=1);f=I();a=z(f,a);f=b.options.padding;var e=0;b.resizingValue=[];for(var d=0;d'),d=g(f.find("div#"+d)),k=0;k');f.append('');f=b.options.padding;e=[];k=d=0;b.resizingValue=[];for(c=t=0;c Date: Tue, 21 Mar 2017 19:23:16 +0100 Subject: [PATCH 14/24] Added compatibility for 'Lightbox' libraries like Fullsizable or Magnific Popup --- jquery.balanced-gallery.js | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index 5ee49d2..72abbd9 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -36,7 +36,13 @@ this.container = container; $(this.container).wrapInner(''); this.wrapper = $(this.container).children()[0]; - this.elements = $(this.wrapper).children('img'); + this.elements = $(this.wrapper).children().map(function(){ + var img = $(this).find('img')[0]; + if(img === undefined) { + img = this; + } + return {element: this, image: $(img)}; + }); this.options = $.extend({}, defaults, options); // merge arg options and defaults this.options.orientation = (this.options.orientation).toLowerCase(); @@ -78,7 +84,7 @@ BalancedGallery.prototype.init = function () { if(this.quickResize === false) { this.elements.each(function() { - $(this).css({display: 'inline-block', padding: 0, margin: 0}); + $(this.element).css({display: 'inline-block', padding: 0, margin: 0}); }); var padding = this.options.padding + 'px'; @@ -162,7 +168,7 @@ } balancedGallery.elements.each(function(){ - var $image = $(this); + var $image = this.image; var imgRatio = aspectRatio($image); var offset = 0; @@ -209,7 +215,7 @@ function collectiveIdealWidth() { var sum = 0; balancedGallery.elements.each(function () { - sum += idealWidth($(this)); + sum += idealWidth(this.image); }); return sum; } @@ -220,15 +226,15 @@ function getWidthWeights() { return balancedGallery.elements.map(function () { - var weight = aspectRatio($(this)); - return {element: this, weight: weight }; + var weight = aspectRatio(this.image); + return {element: this.element, image: this.image, weight: weight }; }); } function getHeightWeights() { return balancedGallery.elements.map(function () { - var weight = 1/aspectRatio($(this)); - return {element: this, weight: weight }; + var weight = 1/aspectRatio(this.image); + return {element: this.element, image: this.image, weight: weight }; }); } @@ -360,7 +366,6 @@ } function reorderElements(partitions) { - $(balancedGallery.wrapper).html(''); //remove all elements for(var i = 0; i < partitions.length; i++) { var subPartition = partitions[i]; for(var j = 0; j < subPartition.length; j++) { @@ -384,8 +389,8 @@ var rowPadding = padding * partitions[i].length; var rawImgHeight = (balancedGallery.options.viewportWidth - rowPadding) / rowRatio; for(var k = 0; k < partitions[i].length; k++) { - var $image = $(partitions[i][k].element); - balancedGallery.elements[index++] = $image; + var $image = partitions[i][k].image; + balancedGallery.elements[index++] = {element: partitions[i][k].element, image: $image}; var imgHeight = parseInt(rawImgHeight, RADIX); var imgWidth = rawImgHeight * aspectRatio($image); imgWidth = checkWidthOverflow(imgWidth); @@ -410,7 +415,7 @@ index++; overflow = 0; } - var $image = balancedGallery.elements[i]; + var $image = balancedGallery.elements[i].image; var imgHeight = parseInt(rawImgHeight, RADIX); var imgWidth = rawImgHeight * aspectRatio($image); imgWidth = checkWidthOverflow(imgWidth); @@ -446,8 +451,8 @@ var imgWidth = checkWidthOverflow(rawImgWidth); var columnHeight = 0; for(var l = 0; l < partitions[k].length; l++) { - var $image = $(partitions[k][l].element); - balancedGallery.elements[index++] = $image; + var $image = partitions[k][l].image; + balancedGallery.elements[index++] = {element: partitions[k][l].element, image: $image}; var imgHeight = Math.round(rawImgWidth * (1/aspectRatio($image))); columnHeight += imgHeight + padding; $image.width(imgWidth); @@ -474,7 +479,7 @@ imgWidth = checkWidthOverflow(rawImgWidth); imagesPerCol += balancedGallery.resizingValue[index].length; } - var $image = balancedGallery.elements[i]; + var $image = balancedGallery.elements[i].image; var imgHeight = parseInt(rawImgWidth * (1/aspectRatio($image)), RADIX); balancedGallery.resizingValue[index].columnHeight += imgHeight + padding; $image.width(imgWidth); @@ -508,7 +513,7 @@ //starting with last image in column, because it's more likely to be outside of the current viewport var k = imagesPerCol-1; while(averageHeight != balancedGallery.resizingValue[j].columnHeight) { - balancedGallery.elements[k].height(balancedGallery.elements[k].height() + counter); + balancedGallery.elements[k].image.height(balancedGallery.elements[k].image.height() + counter); balancedGallery.resizingValue[j].columnHeight += counter; k--; //if all images in a column got streched/shrinked, start iteration again with last image in column till averageHeight matches columnHeight @@ -537,8 +542,6 @@ function orientElementsVertically(partitions) { var $wrapper = $(balancedGallery.wrapper); - $wrapper.html(''); //clear the images - for(var i = 0; i < partitions.length; i++) { var colName = 'balanced-gallery-col'+i; var column = ''; From 71f90d85a915e9f2b92979e2ccb085382446e916 Mon Sep 17 00:00:00 2001 From: Timo Kosse Date: Tue, 21 Mar 2017 19:42:02 +0100 Subject: [PATCH 15/24] Small improvement on vertical mode. --- jquery.balanced-gallery.js | 139 ++++++++++++++++----------------- jquery.balanced-gallery.min.js | 31 ++++---- 2 files changed, 85 insertions(+), 85 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index 72abbd9..21e5c7c 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -3,7 +3,7 @@ var pluginName = 'BalancedGallery', galleries = [], - balancedGallery, + balancedGallery, defaults = { autoResize: true, background: null, @@ -13,7 +13,7 @@ orientation: 'horizontal', padding: 5, shuffleUnorderedPartitions: true, - gridAspectRatio: 1, + gridAspectRatio: 1, widthDivisor: 4 }, resizeTimeout = null, @@ -32,7 +32,7 @@ function BalancedGallery(container, options) { balancedGallery = this // for contexts when 'this' doesn't refer to the BalancedGallery class. - galleries.push(this); + galleries.push(this); this.container = container; $(this.container).wrapInner(''); this.wrapper = $(this.container).children()[0]; @@ -63,11 +63,11 @@ resizeTimeout = setTimeout(function() { for(var index = 0; index < galleries.length; index++) { - if(galleries[index].options.autoResize) { - balancedGallery = galleries[index]; - galleries[index].recreate(); - } - } + if(galleries[index].options.autoResize) { + balancedGallery = galleries[index]; + galleries[index].recreate(); + } + } }, 500); }); } @@ -83,8 +83,12 @@ BalancedGallery.prototype.init = function () { if(this.quickResize === false) { + var displayMode = 'inline-block'; + if(this.options.orientation == 'vertical') { + displayMode = 'block'; + } this.elements.each(function() { - $(this.element).css({display: 'inline-block', padding: 0, margin: 0}); + $(this.element).css({display: displayMode, padding: 0, margin: 0}); }); var padding = this.options.padding + 'px'; @@ -116,9 +120,9 @@ } else if(orientation === 'vertical') { createVerticalGallery(); } else if(orientation === 'grid') { - createGridGallery(); - checkWidth(orientation); - } else { + createGridGallery(); + checkWidth(orientation); + } else { throw("BalancedGallery: Invalid Orientation."); } }; @@ -148,51 +152,51 @@ resizeVerticalElements(partitions); } checkWidth(balancedGallery.options.orientation); - alignColumnHeights(); + alignColumnHeights(); + } + + function createGridGallery() { + var padding = balancedGallery.options.padding; + var cellRatio = balancedGallery.options.gridAspectRatio; + var cellWidth = parseInt(balancedGallery.options.idealWidth - padding, RADIX); + var cellHeight = cellWidth * (1 / cellRatio); + var wrapper = '
'; + var paddingGap = parseInt(((balancedGallery.options.viewportWidth % balancedGallery.options.widthDivisor) / 2), RADIX); + + //if necessary, increase the padding on the left side of the wrapper so the grid is more centered + if(paddingGap > 0) { + var increasedPadding = paddingGap + padding; + var $wrapper = $(balancedGallery.wrapper); + $wrapper.css({paddingLeft: increasedPadding+'px'}); + $wrapper.width(balancedGallery.options.viewportWidth - paddingGap); + } + + balancedGallery.elements.each(function(){ + var $image = this.image; + var imgRatio = aspectRatio($image); + var offset = 0; + + if(imgRatio >= cellRatio) { + $image.height(cellHeight); + $image.width(cellHeight * imgRatio); + offset = (cellWidth - (cellHeight * imgRatio)) / 2; + $image.css({left: offset+'px', position: 'absolute'}); + } else if(imgRatio < cellRatio) { + $image.width(cellWidth); + $image.height(cellWidth * (1/imgRatio)); + offset = (cellHeight - (cellWidth * (1/imgRatio))) / 2; + $image.css({top: offset+'px', position: 'absolute'}); + } + + if(balancedGallery.quickResize) { + var $div = $image.parent(); + $div.width(cellWidth); + $div.height(cellHeight); + } else { + $image.wrap(wrapper); + } + }); } - - function createGridGallery() { - var padding = balancedGallery.options.padding; - var cellRatio = balancedGallery.options.gridAspectRatio; - var cellWidth = parseInt(balancedGallery.options.idealWidth - padding, RADIX); - var cellHeight = cellWidth * (1 / cellRatio); - var wrapper = '
'; - var paddingGap = parseInt(((balancedGallery.options.viewportWidth % balancedGallery.options.widthDivisor) / 2), RADIX); - - //if necessary, increase the padding on the left side of the wrapper so the grid is more centered - if(paddingGap > 0) { - var increasedPadding = paddingGap + padding; - var $wrapper = $(balancedGallery.wrapper); - $wrapper.css({paddingLeft: increasedPadding+'px'}); - $wrapper.width(balancedGallery.options.viewportWidth - paddingGap); - } - - balancedGallery.elements.each(function(){ - var $image = this.image; - var imgRatio = aspectRatio($image); - var offset = 0; - - if(imgRatio >= cellRatio) { - $image.height(cellHeight); - $image.width(cellHeight * imgRatio); - offset = (cellWidth - (cellHeight * imgRatio)) / 2; - $image.css({left: offset+'px', position: 'absolute'}); - } else if(imgRatio < cellRatio) { - $image.width(cellWidth); - $image.height(cellWidth * (1/imgRatio)); - offset = (cellHeight - (cellWidth * (1/imgRatio))) / 2; - $image.css({top: offset+'px', position: 'absolute'}); - } - - if(balancedGallery.quickResize) { - var $div = $image.parent(); - $div.width(cellWidth); - $div.height(cellHeight); - } else { - $image.wrap(wrapper); - } - }); - } function getRows () { var rows = Math.round(collectiveIdealWidth() / balancedGallery.options.viewportWidth); @@ -346,7 +350,7 @@ } shortestPartition.push(sortedWeights[j]); } - + return partitions; } @@ -502,7 +506,7 @@ function alignColumnHeights() { var averageHeight = 0; var imagesPerCol = 0; - + for(var i = 0; i < balancedGallery.resizingValue.length; i++) { averageHeight += balancedGallery.resizingValue[i].columnHeight; } @@ -516,14 +520,14 @@ balancedGallery.elements[k].image.height(balancedGallery.elements[k].image.height() + counter); balancedGallery.resizingValue[j].columnHeight += counter; k--; - //if all images in a column got streched/shrinked, start iteration again with last image in column till averageHeight matches columnHeight + //if all images in a column got streched/shrinked, start iteration again with last image in column till averageHeight matches columnHeight if(k < (imagesPerCol - balancedGallery.resizingValue[j].length)) { k = imagesPerCol-1; } } } } - + //if a scrollbar appears or disappears after resizing function checkWidth(orientation) { if((balancedGallery.options.viewportWidth + balancedGallery.options.padding) !== $(balancedGallery.container).width()) { @@ -534,9 +538,9 @@ } else if(orientation == 'vertical') { quickResizeVertical(); } else if(orientation == 'grid') { - balancedGallery.options.idealWidth = balancedGallery.options.viewportWidth / balancedGallery.options.widthDivisor; - createGridGallery(); - } + balancedGallery.options.idealWidth = balancedGallery.options.viewportWidth / balancedGallery.options.widthDivisor; + createGridGallery(); + } } } @@ -544,18 +548,13 @@ var $wrapper = $(balancedGallery.wrapper); for(var i = 0; i < partitions.length; i++) { var colName = 'balanced-gallery-col'+i; - var column = ''; + var column = ''; $wrapper.append(column); var $col = $($wrapper.find("div#"+colName)); for(var j = 0; j < partitions[i].length; j++) { - var child = partitions[i][j].element; - $col.append(child).append('
'); //Fix for Firefox; without 'style="display: block;"' Firefox assigns a width for an
-element. Strange! + $col.append(partitions[i][j].element); } } - - //add clearing div - var clearingDiv = ''; - $wrapper.append(clearingDiv); } function aspectRatio($image) { diff --git a/jquery.balanced-gallery.min.js b/jquery.balanced-gallery.min.js index 90f8959..d6b79f8 100644 --- a/jquery.balanced-gallery.min.js +++ b/jquery.balanced-gallery.min.js @@ -1,15 +1,16 @@ -(function(g,E,M){function p(a,f){b=this;n.push(this);this.container=a;g(this.container).wrapInner('');this.wrapper=g(this.container).children()[0];this.elements=g(this.wrapper).children("img");this.options=g.extend({},F,f);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:g(this.container)[0].style.width},this.unadulteratedOptions=g.extend({},this.options),G());this.quickResize=!1;this.init(); -this.createGallery()}function G(){g(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10); -if(0=f?(a.height(d),a.width(d*c),c=(e-d*c)/2,a.css({left:c+"px",position:"absolute"})):c=d)e=[];else{var k=K(a,d);--e;for(var d=d-2,c=[],h;0<=d;){h=[];for(var l=k[e-1][d]+1;la&&(a=1);f=I();a=z(f,a);f=b.options.padding;var e=0;b.resizingValue=[];for(var d=0;d'),d=g(f.find("div#"+d)),k=0;k');f.append('');f=b.options.padding;e=[];k=d=0;b.resizingValue=[];for(c=t=0;c');this.wrapper=h(this.container).children()[0];this.elements=h(this.wrapper).children().map(function(){var a=h(this).find("img")[0];a===F&&(a=this);return{element:this,image:h(a)}});this.options=h.extend({},G,f);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:h(this.container)[0].style.width}, +this.unadulteratedOptions=h.extend({},this.options),H());this.quickResize=!1;this.init();this.createGallery()}function H(){h(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10);if(0=f?(a.height(d),a.width(d*c),c=(e-d*c)/2,a.css({left:c+"px",position:"absolute"})):c=d)e=[];else{var k=L(a,d);--e;for(var d=d-2,c=[],g;0<=d;){g=[];for(var l=k[e-1][d]+1;la&&(a=1);f=J();a=z(f,a);f=b.options.padding;var e=0;b.resizingValue=[];for(var d=0;d'),d=h(f.find("div#"+d)),k=0;k Date: Tue, 21 Mar 2017 20:27:15 +0100 Subject: [PATCH 16/24] Set padding/margin of image in init to 0 and set css properties via function and not in string --- jquery.balanced-gallery.js | 4 +++- jquery.balanced-gallery.min.js | 17 +---------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/jquery.balanced-gallery.js b/jquery.balanced-gallery.js index 21e5c7c..74e1680 100644 --- a/jquery.balanced-gallery.js +++ b/jquery.balanced-gallery.js @@ -89,6 +89,7 @@ } this.elements.each(function() { $(this.element).css({display: displayMode, padding: 0, margin: 0}); + this.image.css({display: displayMode, padding: 0, margin: 0}); }); var padding = this.options.padding + 'px'; @@ -160,7 +161,7 @@ var cellRatio = balancedGallery.options.gridAspectRatio; var cellWidth = parseInt(balancedGallery.options.idealWidth - padding, RADIX); var cellHeight = cellWidth * (1 / cellRatio); - var wrapper = '
'; + var wrapper = '
'; var paddingGap = parseInt(((balancedGallery.options.viewportWidth % balancedGallery.options.widthDivisor) / 2), RADIX); //if necessary, increase the padding on the left side of the wrapper so the grid is more centered @@ -194,6 +195,7 @@ $div.height(cellHeight); } else { $image.wrap(wrapper); + $($image[0].parentNode).css({position: 'relative', display: 'inline-block', overflow: 'hidden', width: cellWidth+'px', height: cellHeight+'px', margin: 0, marginRight: padding+'px', marginBottom: padding+'px'}); } }); } diff --git a/jquery.balanced-gallery.min.js b/jquery.balanced-gallery.min.js index d6b79f8..ca0f171 100644 --- a/jquery.balanced-gallery.min.js +++ b/jquery.balanced-gallery.min.js @@ -1,16 +1 @@ -(function(h,E,F){function m(a,f){b=this;n.push(this);this.container=a;h(this.container).wrapInner('');this.wrapper=h(this.container).children()[0];this.elements=h(this.wrapper).children().map(function(){var a=h(this).find("img")[0];a===F&&(a=this);return{element:this,image:h(a)}});this.options=h.extend({},G,f);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:h(this.container)[0].style.width}, -this.unadulteratedOptions=h.extend({},this.options),H());this.quickResize=!1;this.init();this.createGallery()}function H(){h(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a',c=parseInt(b.options.viewportWidth%b.options.widthDivisor/2,10);if(0=f?(a.height(d),a.width(d*c),c=(e-d*c)/2,a.css({left:c+"px",position:"absolute"})):c=d)e=[];else{var k=L(a,d);--e;for(var d=d-2,c=[],g;0<=d;){g=[];for(var l=k[e-1][d]+1;la&&(a=1);f=J();a=z(f,a);f=b.options.padding;var e=0;b.resizingValue=[];for(var d=0;d'),d=h(f.find("div#"+d)),k=0;k');this.wrapper=h(this.container).children()[0];this.elements=h(this.wrapper).children().map(function(){var a=h(this).find("img")[0];a===F&&(a=this);return{element:this,image:h(a)}});this.options=h.extend({},G,g);this.options.orientation=this.options.orientation.toLowerCase();this.options.autoResize&&(this.unadulteratedCSS={width:h(this.container)[0].style.width},this.unadulteratedOptions=h.extend({},this.options),H());this.quickResize=!1;this.init();this.createGallery()}function H(){h(E).resize(function(){clearTimeout(u);u=setTimeout(function(){for(var a=0;a=g?(c.height(d),c.width(d*f),f=(e-d*f)/2,c.css({left:f+"px",position:"absolute"})):f"),h(c[0].parentNode).css({position:"relative",display:"inline-block",overflow:"hidden",width:e+"px",height:d+"px",margin:0,marginRight:a+"px",marginBottom:a+"px"}))})}function I(){var a=0;b.elements.each(function(){a+=q(this.image)*b.options.idealHeight+b.options.padding});return a}function J(){return b.elements.map(function(){var a=q(this.image);return{element:this.element,image:this.image,weight:a}})}function K(){return b.elements.map(function(){var a=1/q(this.image);return{element:this.element,image:this.image,weight:a}})}function z(a,g){if(b.options.maintainOrder){var e;var d=g;e=a.length;if(0>=d)e=[];else{var f=L(a,d);--e;for(var d=d-2,c=[],k;0<=d;){k=[];for(var l=f[e-1][d]+1;la&&(a=1);g=J();a=z(g,a);g=b.options.padding;var e=0;b.resizingValue=[];for(var d=0;d'),d=h(g.find("div#"+d)),f=0;f Date: Sat, 9 Sep 2017 11:30:31 +0200 Subject: [PATCH 17/24] Makes a grid gallery more robust in foreign environments --- demo_grid.html | 4 ++++ jquery.balanced-gallery.js | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/demo_grid.html b/demo_grid.html index 5a98c4f..6692c1a 100644 --- a/demo_grid.html +++ b/demo_grid.html @@ -10,6 +10,10 @@ height: 100%; padding: 0; margin: 0; + line-height: 12px; + } + img { + max-width: 100%; }