From 4a4e52c13fd6419220e42e566d09deb18cd14ca1 Mon Sep 17 00:00:00 2001 From: Bajax Date: Wed, 22 May 2019 01:32:00 -0400 Subject: [PATCH] support for non-integer pixels Makes it possible to get accurate REM units for clicking/dragging/etc from pixel values. Useful for dynamically scaled pages. --- getEmPixels.js | 3 ++- getEmPixels.min.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/getEmPixels.js b/getEmPixels.js index 8388d1b..aad2ed7 100644 --- a/getEmPixels.js +++ b/getEmPixels.js @@ -24,7 +24,8 @@ element.appendChild(testElement); // Get the client width of the test element - var value = testElement.clientWidth; + var value = window.getComputedStyle(testElement).width; + value = parseFloat(value.replace('px', '')); if (extraBody) { // Remove the extra body element diff --git a/getEmPixels.min.js b/getEmPixels.min.js index a36e83c..9a74398 100644 --- a/getEmPixels.min.js +++ b/getEmPixels.min.js @@ -1,2 +1,2 @@ /*! getEmPixels | Author: Tyson Matanich (http://matanich.com), 2013 | License: MIT */ -(function(n,t){"use strict";var i="!important;",r="position:absolute"+i+"visibility:hidden"+i+"width:1em"+i+"font-size:1em"+i+"padding:0"+i;window.getEmPixels=function(u){var f,e,o;return u||(u=f=n.createElement("body"),f.style.cssText="font-size:1em"+i,t.insertBefore(f,n.body)),e=n.createElement("i"),e.style.cssText=r,u.appendChild(e),o=e.clientWidth,f?t.removeChild(f):u.removeChild(e),o}})(document,document.documentElement); \ No newline at end of file +(function(n,t){"use strict";var i="!important;",r="position:absolute"+i+"visibility:hidden"+i+"width:1em"+i+"font-size:1em"+i+"padding:0"+i;window.getEmPixels=function(u){var f,e,o;return u||(u=f=n.createElement("body"),f.style.cssText="font-size:1em"+i,t.insertBefore(f,n.body)),e=n.createElement("i"),e.style.cssText=r,u.appendChild(e),o=parseFloat(window.getComputedStyle(e).width.replace('px', '')),f?t.removeChild(f):u.removeChild(e),o}})(document,document.documentElement); \ No newline at end of file