diff options
author | Oliver Sartun <osartun@gmail.com> | 2015-12-01 22:30:47 +0100 |
---|---|---|
committer | Oliver Sartun <osartun@gmail.com> | 2015-12-01 22:30:47 +0100 |
commit | 01655ae5003472ad98f276c064e8d3b78a0d84bd (patch) | |
tree | dbad964ea1ce3e31b101aa7ac5a1813b3a635ae4 | |
parent | cc2f444d77d1b80cafc3a64049a95faf3cd43eeb (diff) | |
download | jQuery.equalHeights-01655ae5003472ad98f276c064e8d3b78a0d84bd.zip jQuery.equalHeights-01655ae5003472ad98f276c064e8d3b78a0d84bd.tar.gz jQuery.equalHeights-01655ae5003472ad98f276c064e8d3b78a0d84bd.tar.bz2 |
Improving and testing watch / unwatch option
-rw-r--r-- | example/example.html | 33 | ||||
-rw-r--r-- | jquery.equalheights.js | 70 | ||||
-rw-r--r-- | jquery.equalheights.min.js | 4 |
3 files changed, 81 insertions, 26 deletions
diff --git a/example/example.html b/example/example.html index a948bf2..69a5c31 100644 --- a/example/example.html +++ b/example/example.html @@ -5,6 +5,7 @@ <title>jQuery Simple Equal Heights Example</title> <style> body { width: 960px; margin: 20px auto; } + pre { display: inline; } .column1, .column2, .column3 { width: 250px; padding: 10px; margin: 0 20px 0 0; height: auto; float: left; color: #fff; } .column1 { background: red; } .column2 { background: blue; } @@ -13,6 +14,11 @@ .clearfix:before, .clearfix:after { content: ""; display: table; } .clearfix:after { clear: both; } .clearfix { *zoom: 1; } + .row { display: block; margin: 20px 0;} + @media screen and (max-width: 960px) { + body { width: 90%; } + .column1, .column2, .column3 { width: calc(30% - 40px); } + } </style> </head> <body> @@ -59,10 +65,37 @@ </div> </div> + <div data-equal="div" data-watch="true" id="equalwatch" class="clearfix"> + <h3>Using equal heights with <pre>data-equal="div"</pre> und <pre>data-watch="true"</pre></h3> + <span class="row"> + <button id="unwatch">Click here to unwatch</button> + <button id="reset-heights">Click here to reset heights</button> + <button id="watch">Click here to watch</button> + </span> + <div class="column1"> + <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> + </div> + <div class="column2"> + <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> + </div> + <div class="column3"> + <p>Lorem ipsum dolor sit amet</p> + </div> + </div> + <script src="jquery-1.7.2.min.js"></script> <script src="../jquery.equalheights.js"></script> <script> $('#equalheight div').equalHeights(); + $('#unwatch').on('click', function () { + $('#equalwatch div').equalHeights({unwatch: true}); + }); + $('#reset-heights').on('click', function () { + $('#equalwatch div').removeAttr("style"); + }); + $('#watch').on('click', function () { + $('#equalwatch div').equalHeights({watch: true}); + }); </script> </body> diff --git a/jquery.equalheights.js b/jquery.equalheights.js index b9a0876..a7e920a 100644 --- a/jquery.equalheights.js +++ b/jquery.equalheights.js @@ -10,48 +10,70 @@ */ (function($) { - var watched = []; + var watched = [], + calcMaxHeight = function($elems) { + var height, maxHeight = 0; + $elems.each(function () { + height = $(this).innerHeight(); + if ( height > maxHeight ) { maxHeight = height; } + }); + return maxHeight; + }, + docEl = document.documentElement; + $(window).on('resize', function () { - for (var i = 0, l = watched.length; i < l; i++) { - if (watched[i]) { - $(watched[i]).css('height', 'auto').equalHeights() + // Bundle reading and writing styles to reduce synchronous layout / jank + + // Reset heights + for (var i = 0, l = watched.length, elems, _w = [], m = []; i < l; i++) { + elems = watched[i]; + // Don't waste time on elements that aren't in the DOM + if (elems.length && $.contains(docEl, elems[0]) ) { + _w.push(elems); + elems.css('height', 'auto'); } } - }) - $.fn.equalHeights = function(options) { - var maxHeight = 0, - $this = $(this), - equalHeightsFn = function() { - var height = $(this).innerHeight(); + // Calc max height + for (i = 0, l = _w.length; i < l; i++) { m[i] = calcMaxHeight(_w[i]); } + + // Set max height + for (i = 0; i < l; i++) { _w[i].css('height', m[i]); } + }); - if ( height > maxHeight ) { maxHeight = height; } - }; + $.fn.equalHeights = function(options) { + var maxHeight, $this = $(this), i, l, isContained, res, loop; options = options || {}; - $this.each(equalHeightsFn); + maxHeight = calcMaxHeight($this); - if (options.watch || options.equalWatch) { - watched.push(this) + if (options.watch) { + for (i = 0, l = watched.length, isContained; i < l; i++) { + if ($this.is(watched[i])) { + isContained = true; + break; + } + } + if (!isContained) { + watched.push($this); + } } - if (options.unwatch || options.equalUnwatch) { - for (var i = 0, l = watched.length, res = []; i < l; i++) { - if (watched[i].length && !$this.is(watched[i])) { - res.push(watched[i]); - } + if (options.unwatch) { + for (i = 0, l = watched.length, res = []; i < l; i++) { + if (!$this.is(watched[i])) { res.push(watched[i]); } } watched = res; return this; } - if (options.wait || options.equalWait) { - var loop = setInterval(function() { + if (options.wait) { + loop = setInterval(function() { if(maxHeight > 0) { clearInterval(loop); return $this.css('height', maxHeight); } - $this.each(equalHeightsFn); + maxHeight = calcMaxHeight($this); }, 100); return this; } else { @@ -59,7 +81,7 @@ } }; - // auto-initialize plugin + // Auto-initialize plugin $(document).on('ready', function() { $('[data-equal]').each(function(){ var $this = $(this), diff --git a/jquery.equalheights.min.js b/jquery.equalheights.min.js index 6bba224..128e039 100644 --- a/jquery.equalheights.min.js +++ b/jquery.equalheights.min.js @@ -6,6 +6,6 @@ * Uses the same license as jQuery, see: * http://docs.jquery.com/License * - * @version 1.5.1 + * @version 1.5.2 */ -!function(a){a.fn.equalHeights=function(){var b=0,c=a(this);return c.each(function(){var c=a(this).innerHeight();c>b&&(b=c)}),c.css("height",b)},a("[data-equal]").each(function(){var b=a(this),c=b.data("equal");b.find(c).equalHeights()})}(jQuery);
\ No newline at end of file +!function(a){var b=[],c=function(b){var c,d=0;return b.each(function(){c=a(this).innerHeight(),c>d&&(d=c)}),d},d=document.documentElement;a(window).on("resize",function(){for(var e,f=0,g=b.length,h=[],i=[];g>f;f++)e=b[f],e.length&&a.contains(d,e[0])&&(h.push(e),e.css("height","auto"));for(f=0,g=h.length;g>f;f++)i[f]=c(h[f]);for(f=0;g>f;f++)h[f].css("height",i[f])}),a.fn.equalHeights=function(d){var e,f,g,h,i,j,k=a(this);if(d=d||{},e=c(k),d.watch){for(f=0,g=b.length,h;g>f;f++)if(k.is(b[f])){h=!0;break}h||b.push(k)}if(d.unwatch){for(f=0,g=b.length,i=[];g>f;f++)k.is(b[f])||i.push(b[f]);return b=i,this}return d.wait?(j=setInterval(function(){return e>0?(clearInterval(j),k.css("height",e)):void(e=c(k))},100),this):k.css("height",e)},a(document).on("ready",function(){a("[data-equal]").each(function(){var b=a(this),c=b.data(),d=c.equal;b.find(d).equalHeights(c)})})}(jQuery);
\ No newline at end of file |