如何获得一个元素与浏览器分辨率相同的高度? [英] How to get an element the same height as browser resolution?
问题描述
我正在尝试使用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;
/ pre>
$(。bg)css(height,height);
});
查看: 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 alsowindow.innerWidth
and other methods of grabbing the user's screen statistics.You can also use
self.innerHeight
,parent.innerHeight
, andtop.innerHeight
but these have different purposes. (See link).Also, you're using
$(window).css("height");
The jQuerycss()
function assigns css values, it doesn't return a string. It would be$(window).height()
becauseheight()
does return a string.var height = function() { return this.length; } // height() returns "this.length"
这篇关于如何获得一个元素与浏览器分辨率相同的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!