编辑:忘了提到,我还必须支持IE 8。
好的,我已经找到了解决办法。对于IE<9以外的浏览器,Window.getComputedStyle()
是拯救者。Window.getComputedStyle()
方法会返回元素应用活动样式表后所有CSS属性的值,并解析这些值所包含的任何基本计算。有关更多详细信息,请参见https://developer.mozilla.org/en-US/docs/Web/API/Window.getComputedStyle。
问题出现在IE 8及之前版本中。在这些版本中,getComputedStyle
为undefined
。幸运的是,IE有专有的currentStyle
属性,可以从中检索内容的width
和height
。遗憾的是,如果我们在样式表中使用%
定义width
,它也将返回%
。
因此,问题仍然存在,我们需要一种将百分比转换为像素值的方法。这个问题有一个来自Dean Edwards的hack可以解决。感谢他!
var PIXEL = /^\d+(px)?$/i;
function getPixelValue(element, value) {
if (PIXEL.test(value)) return parseInt(value);
var style = element.style.left;
var runtimeStyle = element.runtimeStyle.left;
element.runtimeStyle.left = element.currentStyle.left;
element.style.left = value || 0;
value = element.style.pixelLeft;
element.style.left = style;
element.runtimeStyle.left = runtimeStyle;
return value;
};
因此,以寻找内容宽度为例的跨浏览器解决方案(计算高度的逻辑相同,只需使用高度查询而非宽度)如下所示:
假设我们有一个id为'container'
的div
。其宽度在样式表中设置为50%。
<style type="text/css">
#container {
width: 50%;
padding: 5px;
}
</style>
测试JavaScript代码:
var container = document.getElementById('container');
if (window.getComputedStyle) {
var computedStyle = getComputedStyle(container, null)
alert("width : "+computedStyle.width);
} else {
alert("width : "+getPixelValue(container,container.currentStyle.width)+'px');
}
document.getElementById("elemID").clientWidth;
document.getElementById("elemID").clientHeight;
elemID
是元素的ID。jQuery(window)
的等效写法:宽度: jQuery(window).width()
=> document.querySelector('html').clientWidth
或者 document.body.clientWidth
(没有水平滚动条)
高度: jQuery(window).height()
=> document.querySelector('html').clientHeight
在重写旧的jQuery代码时,我做了一些测试,并想提到与$(window)
等效的内容,它并不总是等于!window.clientWidth/Height
window
尺寸示例(开发者工具已打开)(在Chrome(v104)中打开的页面示例)
#HTMLElement == document.querySelector('html')
width | height
jQuery(window).width(): 1905 | jQuery(window).height(): 509
document.body.clientWidth: 1905 | document.body.clientHeight: 14000
#HTMLElement.clientWidth: 1905 | #HTMLElement.clientHeight: 509 // EQUAL to jQuery
window.innerWidth: 1920 | window.innerHeight: 524
window.outerWidth: 1920 | window.outerHeight: 1055
#HTMLElement == document.querySelector('html')
width | height
jQuery(window).width(): 1920 | jQuery(window).height(): 524
document.body.clientWidth: 1920 | document.body.clientHeight: 14000
#HTMLElement.clientWidth: 1920 | #HTMLElement.clientHeight: 524 // still $ equal
window.innerWidth: 1920 | window.innerHeight: 524 // only without SB
window.outerWidth: 1920 | window.outerHeight: 1055
现在为什么要在那里包含<html>
元素?这是因为其他宽度/高度源不反映来自jQuery的值,因为它们没有考虑滚动条。
jQuery考虑了滚动条,
本地window
属性不考虑,
然而html
Element
会考虑。
$w
代替 $(window)
,那么这个代码段可以作为替代方案:const htmlEl = document.querySelector('html'); const $w = { width: () => htmlEl.clientWidth, height: () => htmlEl.clientHeight };
然后您可以调用 $w.width()
/ $w.height()
来获得与 jQuery 相同的效果 :-) - jave.web$(document.querySelector('html')).scrollTop()
相当于 jQuery 的 $(window).scrollTop()
;-) - jave.webdocument.getElementById("elementID").style.height;
可以获取元素的高度。是的,有innerWidth和innerHeight。
所以类似于
var obj = document.getElementById('idhere');
var width = obj.innerWidth...
jQuery
也是纯JavaScript。你可能想表达的是“使用本地DOM API”,虽然它大多遵循跨浏览器的通用标准,但并不保证所有部分都能跨浏览器正常工作。 - sarveshseri