Skip to content

Instantly share code, notes, and snippets.

@lyzadanger
Created November 16, 2012 23:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyzadanger/4091892 to your computer and use it in GitHub Desktop.
Save lyzadanger/4091892 to your computer and use it in GitHub Desktop.
Jehl's jQuery px-to-em Conversion, Lyza's Version
$.fn.toEm = function(settings){
settings = jQuery.extend({
scope: 'body',
asNumber: false
}, settings);
var that = parseInt(this[0],10),
inEms,
scopeTest = jQuery('<div style="display: none; font-size: 1em; margin: 0; padding:0; height: auto; line-height: 1; border:0;">&nbsp;</div>').appendTo(settings.scope),
scopeVal = scopeTest.height();
scopeTest.remove();
inEms = (that / scopeVal).toFixed(8);
return (settings.asNumber) ? inEms : inEms + 'em';
};
$.fn.toPx = function(settings){
settings = jQuery.extend({
scope: 'body',
asNumber: false
}, settings);
var that = parseFloat(this[0]),
inPx,
scopeTest = jQuery('<div style="display: none; font-size: 1em; margin: 0; padding:0; height: auto; line-height: 1; border:0;">&nbsp;</div>').appendTo(settings.scope),
scopeVal = scopeTest.height();
scopeTest.remove();
inPx = Math.round(that * scopeVal)
return (settings.asNumber) ? inPx : inPx + 'px';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment