如何使元素的高度与浏览器分辨率相同?

2024-01-07

我正在尝试使用 jquery 将元素的高度设置为等于用户浏览器。这是我到目前为止得到的代码,但不起作用。是因为我在CSS中设置了最小高度吗?我确实需要该类来等于用户的分辨率。

<script type="text/javascript">
jQuery(function() { 
    jQuery(".bg").css("height", jQuery(window).css("height"));
}
</script>

将变量设置为window.innerHeight

$(document).ready(function() {
    var height = window.innerHeight;
    $(".bg").css("height", height);
});

See: https://developer.mozilla.org/en-US/docs/DOM/window.innerHeight https://developer.mozilla.org/en-US/docs/DOM/window.innerHeight欲了解更多详细信息window.innerHeight.

window.innerHeight返回用户视口的高度,而不是really正如您所要求的,屏幕分辨率,但它有效。还有window.innerWidth以及其他抓取用户屏幕统计数据的方法。

您还可以使用self.innerHeight, parent.innerHeight, and top.innerHeight但这些都有不同的目的。 (参见链接)。

另外,你正在使用$(window).css("height");jQuerycss()功能assignscss 值,它不返回字符串。这将是$(window).height()因为height()确实返回一个字符串。

var height = function() { return this.length; }
// height() returns "this.length"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使元素的高度与浏览器分辨率相同? 的相关文章

随机推荐