Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created May 22, 2010 04:02
Show Gist options
  • Save bangpound/409753 to your computer and use it in GitHub Desktop.
Save bangpound/409753 to your computer and use it in GitHub Desktop.
/*global jQuery */
"use strict";
/**
* jQuery balance plugin.
*
* Adds 1px padding to the right side of an element. If text wraps and
* causes the height of the element to increase, remove 1 pixel.
*
* $('h2.title').balance();
*/
(function ($) {
$.fn.balance = function () {
return this.each(function () {
var height, padding;
height = $(this).innerHeight();
padding = 0;
if ($(this).text().match(/\s/)) {
$(this).height('auto');
do {
padding += 1;
$(this).css({ paddingRight: padding });
}
while ($(this).innerHeight() <= height);
padding -= 1;
$(this).css({ paddingRight: padding });
}
});
};
})(jQuery);
/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, indent: 2 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment