【发布时间】:2018-12-11 16:31:31
【问题描述】:
我想将一个元素的height 设置为window.innerheight,但我必须在CSS 中进行设置,因为不知何故我无法访问该元素来使用javascript 来更改它的样式。
有没有办法做到这一点?喜欢在 javascript 中更改 CSS 类?
我试过了:
document.getElementById('root').style.setProperty('--view-height', window.innerHeight +'px');
在 CSS 中:
.menu {
height: var(--view-height) !important;
}
它可以工作,但旧版浏览器不支持CSS Variables,所以我不能使用它,但我想要类似的东西。
编辑: 有很多答案,但他们都使用 javascript,我说我不能使用 js 来设置元素样式!我只想通过 css 类样式来做到这一点
【问题讨论】:
-
也许 polyfill 可以帮助您解决旧的浏览器问题?
-
有了这个,你可以添加一个类名,并在你的 CSS 中为这个新的类名设置样式。 w3schools.com/howto/howto_js_add_class.asp
-
如何在 css 中将样式设置为 window.innerheight? !!
标签: javascript css sass