public
Created

Re-make of MooTools Element.measure for jQuery. Taken from - http://stackoverflow.com/a/7351956/617615

  • Download Gist
jquery.measure.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
!function(global) {
function definition($) {
$.fn.measure = function(fn) {
var clone = $(this).clone(), result;
 
clone.css({
visibility: 'hidden',
position: 'absolute'
});
clone.appendTo(document.body);
 
if (typeof fn == 'function') {
result = fn.apply(clone);
}
clone.remove();
 
return result;
};
}
 
if (typeof define === 'function' && define.amd) {
define(['jquery'], definition);
} else {
definition(global.jQuery);
}
}(this);
 
 
 
// Usage
/*
var elem = $('<div></div>').html(someContent);
 
var width = elem.measure(function() {
return this.width();
});
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.