如何获得一个元素与浏览器分辨率相同的高度? [英] How to get an element the same height as browser resolution?

查看:217
本文介绍了如何获得一个元素与浏览器分辨率相同的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery设置元素的高度等于用户浏览器。这里是我到目前为止的代码不工作。是因为我在css中设置了最小高度吗?我真的需要该类来等于用户的解析度。

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


解决方案

c $ c> window.innerHeight

  $(document).ready(function b $ b var height = window.innerHeight; 
$(。bg)css(height,height);
});
/ pre>

查看: https://developer.mozilla.org/en-US/docs/DOM/window.innerHeight 了解有关 window.innerHeight 的更多详情。 p>

window.innerHeight 返回用户视口的高度,而不是真正屏幕分辨率,你也在问,但它工作,还有 window.innerWidth 和其他方法抓住用户的屏幕统计信息。



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



此外,您使用 $(window).css(height); c $ c> css()函数分配 css值,它不返回字符串。这将是 $(window).height()因为 height()会返回一个字符串

  var height = function(){return this.length; } 
// height()返回this.length


I am trying use jquery to set the height of an element to equal the users browser. Here is the code I got so far that's not working. Is it because I have a minimal height set in css? I really need that class to equal the users resolution.

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

解决方案

Set a variable to window.innerHeight

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

See: https://developer.mozilla.org/en-US/docs/DOM/window.innerHeight for more details on window.innerHeight.

window.innerHeight returns the height of the user's viewport, not really the screen resolution, as you were asking, but it works. There is also window.innerWidth and other methods of grabbing the user's screen statistics.

You can also use self.innerHeight, parent.innerHeight, and top.innerHeight but these have different purposes. (See link).

Also, you're using $(window).css("height"); The jQuery css() function assigns css values, it doesn't return a string. It would be $(window).height() because height() does return a string.

var height = function() { return this.length; }
// height() returns "this.length"

这篇关于如何获得一个元素与浏览器分辨率相同的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆