【发布时间】:2013-05-28 10:44:07
【问题描述】:
我有一个 javascript 文件,我用它来获取视口的宽度和高度,以调整我网站的分辨率。我可以使用 javascript 获取这些值,但我真的不知道从哪里开始。我想将这些值发送到我的 CSS 变量,但我还没有找到快速完成此操作的方法。可能吗?这是我的 JS 代码:
<script type="text/javascript">
<!--
var viewportwidth;
var viewportheight;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth,
viewportheight = document.documentElement.clientHeight
}
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
viewportheight = document.getElementsByTagName('body')[0].clientHeight
}
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
//-->
</script>
对于 CSS,我希望能够快速简单地摆脱它,但它不起作用:
@variables{
ViewWidth:'+viewportwidth+';}
@variables{
ViewHeight:'+viewportheight+';}
html{
max-width: var(ViewWidth);
max-height: var(ViewHeight);}
我假设我在变量声明中的语法不正确。我这样写是为了表明我正在尝试从 JS 获取变量值并将其传递给 CSS。
【问题讨论】:
-
在计算 css 后解析 JavaScript。您需要使用元素的 javascript 样式属性来执行此操作。
-
很抱歉打扰您,但您没有 CSS 变量。
标签: javascript css