简体   繁体   English

如何将浏览器高度应用于CSS类?

[英]How do I apply the browser height to a CSS class?

You can read your browser dimensions with jQuerys $document.height or with simple JS via height: window.innerHeight || 您可以使用jQuerys $ document.height或使用简单的JS通过height读取浏览器尺寸:window.innerHeight || document.body.clientHeight. document.body.clientHeight。

But how do I apply the value to a CSS class? 但是如何将该值应用于CSS类?

jQuery的:

$('.targetClass').css('height',otherObject.height()+'px');

You cannot rewrite/add new values to CSS properties/classes on the fly; 您无法动态地重写/添加新值到CSS属性/类; you can write/rewrite/add values to elements. 您可以编写/重写/向元素添加值。

You should post your situation, maybe there is a different way to do what you're trying to do. 你应该发布你的情况,也许有不同的方式来做你想做的事情。

$('#target').css('height',$(document.body).height()+'px')

(jQuery的)

Find the rule object in a browser-specific way ( http://www.javascriptkit.com/domref/cssrule.shtml ) 以特定于浏览器的方式查找规则对象( http://www.javascriptkit.com/domref/cssrule.shtml

Then you can edit the style object of the rule object and actually change the CSS class definition 然后,您可以编辑规则对象的样式对象,并实际更改CSS类定义

No need to overcomplicate this - you don't need to find a way to apply the height to a CSS class. 无需过度复杂化 - 您无需找到将高度应用于CSS类的方法。 Just give each of your <div> sa class in the HTML: 只需在HTML中为每个<div> sa类提供:

<div class="viewport_height">This is some content.</div>

then use jQuery code similar to this: 然后使用类似这样的jQuery代码:

var document_height = $(document).height();
$('.viewport_height').css('height', document_height + 'px');

to apply the measured height to all elements of that class. 将测量的高度应用于该类的所有元素。

For a more robust solution, attach a function to the window 's onresize event to recalculate and apply the height whenever the viewport height changes. 要获得更强大的解决方案,请将函数附加到windowonresize事件,以便在视口高度更改时重新计算并应用高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM