Skip to content

Instantly share code, notes, and snippets.

@337547038
Last active April 13, 2018 05:55
Show Gist options
  • Save 337547038/18f39b1bdfa699ba6cf5cf20efb8990e to your computer and use it in GitHub Desktop.
Save 337547038/18f39b1bdfa699ba6cf5cf20efb8990e to your computer and use it in GitHub Desktop.
js获取dom元素的高度数值不精确
页面使用rem布局,dom元素的宽高会有浮点数的情况,如110.32px。获取元素高度时使用jq的height及offsetHeight函数取到的数值都是整数,如110px。
例如在一些滚动效果上,滚动的单位数越大则误差越大,如110*10=1100px,实际应该是110.32*10=1103.2,解决的办法:
var obj = document.getElementById('demo');
var oStyle = obj.currentStyle?obj.currentStyle:window.getComputedStyle(obj, null);
var height = parseFloat(oStyle.height);
currentStyle这个是针对IE浏览器的
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment