-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.equalHeightInRow.min.js
17 lines (16 loc) · 3.06 KB
/
jquery.equalHeightInRow.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/***
*
* jquery.equalHeightInRow v2.0
* The jQuery plugin for equal height of the elements in the row
* Asanov Ruslan //github.com/AsanovRuslan
* Released under the MIT license - http://opensource.org/licenses/MIT
* EXAMPLE
* $('.block').equalHeightInRow();
*
* Equal height internal child elements
* $('.block').equalHeightInRow({ child: ['.child1,.child2']});
*
***/
(function(d){var e={imageLoad:function(c,a){var b=c.get(0);c.each(function(){if(c.is("img")){c.one("load",function(){a.notify()});if(!b.complete||"undefined"!==typeof b.naturalWidth&&0===b.naturalWidth)return!1;c.trigger("load")}});return a},rowLoad:function(c){var a=d.Deferred(function(){this.count=0}),b=c.length;c.each(function(){var b=d(this).find("img"),c=b.length,g=d.Deferred(function(){this.count=0});g.progress(function(){g.count++;g.count==c&&g.resolve()}).always(function(){setTimeout(function(){a.notify()},0)});b.each(function(){e.imageLoad(d(this),g)})});return a.progress(function(){a.count++;a.count==b&&a.resolve()}).promise()},countElementInRow:function(c,a){var b=0,e=c.eq(0).position().top;if(a){var h=[];c.each(function(a){d(this).position().top>e&&(e=c.eq(a).position().top,h.push(b),b=0);b++});h.push(b);return h}c.each(function(c){if(d(this).position().top>e)return b=c,!1});return b},getMaxHeight:function(c){var a=0,b=0;c.css({height:"","min-height":0});c.each(function(){b=d(this).outerHeight();b>a&&(a=b)});return a},setHeight:function(c,a){"table-cell"==c.eq(0).css("display")?c.css("height",a):c.css("min-height",a);return{elements:c,height:a}}};d.fn.equalHeightInRow=function(c){function a(){h.each(function(){var c=d(this),a=d(this).find(k),g=1,h;h=e.countElementInRow(a,b.eachRow)||0;if(b.eachRow){var m=1,n=0,l=[];a.each(function(c){l.push(this);if(m==h[n]||!a[c+1]){var f=d(l);b.onRowBefore(f);e.rowLoad(f).always(function(){for(var a=0;a<b.child.length;a++)e.setHeight(f.find(b.child[a]),
e.getMaxHeight(f.find(b.child[a])));b.applyOnlyChild||e.setHeight(f,e.getMaxHeight(f));b.onRowAfter(f)});l=[];m=0;n++}m++});return!0}g=Math.ceil(a.length/h);if(1<g)return l=[],a.each(function(c){l.push(this);if(0==(c+1)%h||!a[c+1]){var f=d(l);b.onRowBefore(f);e.rowLoad(f).always(function(){for(var a=0;a<b.child.length;a++)e.setHeight(f.find(b.child[a]),e.getMaxHeight(f.find(b.child[a])));b.applyOnlyChild||e.setHeight(f,e.getMaxHeight(f));b.onRowAfter(f)});l=[]}}),!0;1==g&&(b.onRowBefore(a),e.rowLoad($el).always(function(){for(var d=0;d<b.child.length;d++)e.setHeight(c.find(b.child[d]),e.getMaxHeight(c.find(b.child[d])));e.setHeight(a,e.getMaxHeight(c.find(a)));b.onRowAfter(a)}))});g||(b.onLoad(k),g=!0)}var b=d.extend({child:[],eachRow:!1,windowLoad:!1,windowLoadReset:!1,applyOnlyChild:!1,parent:!1,onRowBefore:function(){},onRowAfter:function(){},onResizeBefore:function(){},onResizeAfter:function(){},onLoad:function(){}},c),k=this,h=b.parent?d(k).closest(b.parent):d(k).parent(),g=!1;if(!k.length)return!1;if(b.windowLoad)d(window).on("load",
a);else if(a(),b.windowLoadReset)d(window).on("load",a);d(window).on("resize orientationchange",function(){b.onResizeBefore(k);a();b.onResizeAfter(k)});return k}})(jQuery);