Skip to content

Instantly share code, notes, and snippets.

@simonsmith
Created March 11, 2013 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonsmith/5135933 to your computer and use it in GitHub Desktop.
Save simonsmith/5135933 to your computer and use it in GitHub Desktop.
Re-make of MooTools Element.measure for jQuery. Taken from - http://stackoverflow.com/a/7351956/617615
!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();
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment