简体   繁体   English

Height 100vh - JavaScript 中其他元素的高度

[英]Height 100vh - height of other element in JavaScript

I have this code:我有这个代码:

document.querySelector('.saved').style.height = '100vh' - document.querySelector('.header').offsetHeight + 'px';

I want to make the div.saved have height 100vh - height of div.header .我想使div.saved具有高度100vh -高度div.header The code on top doesn't work.上面的代码不起作用。 What do I do?我该怎么办?

您可以使用内置的 css函数 calc

document.querySelector('.saved').style.height = 'calc(100vh - ' +   document.querySelector('.header').offsetHeight + 'px)';

You are missing a calculate call in CSS.您缺少 CSS 中的计算调用。 Your code does not make the browser aware of the fact that it has to calculate the height of the element.您的代码不会让浏览器意识到它必须计算元素的高度这一事实。 Something like就像是

document.querySelector('.saved').style.height = 'calc(100vh - ' +  document.querySelector('.header').offsetHeight + 'px)';

should work.应该管用。

You cannot subtract vh and pixel.你不能减去 vh 和像素。 Get the whole screen height and subtract it with the height of your element.获取整个屏幕高度并用元素的高度减去它。

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

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