简体   繁体   English

将 html 元素与内部高度等同起来

[英]equate html element with inner height

Sorry, I'm new to using the Javascript DOM and after my research I couldn't find anything.抱歉,我是使用 Javascript DOM 的新手,经过我的研究,我找不到任何东西。 then i decided to post here然后我决定在这里发帖

For example: When I type例如:当我输入

console.log(window.innerHeight);

it outputs 633. then I create an html element and give its height a value of 633px look like:它输出 633。然后我创建一个 html 元素并将其高度设置为 633px,如下所示:

    width: 100%;
    height: 633px;

I want this html element to look like a full page, but I cannot我希望这个 html 元素看起来像一个完整的页面,但我不能

height: 100%

because a different html element will come under it.因为它下面会出现一个不同的 html 元素。 When the page height changes, the html element whose height I set as 633px is broken当页面高度改变时,我设置的高度为633px的html元素被破坏

the main question: Is there a way to equalize the window.innerHeight output with the height of the html element?主要问题:有没有办法使 window.innerHeight 输出与 html 元素的高度相等?

I'm not sure why you have two html elements on the same page but setting that aside... How does the second one come in?我不知道为什么你在同一个页面上有两个 html 元素,但把它放在一边......第二个是如何进来的? Does it get loaded via JavaScript?它是通过 JavaScript 加载的吗? If so, you could make sure the first html id has an id like "" and use javascript to do something like this after the new one gets loaded:如果是这样,您可以确保第一个 html id 具有类似 "" 的 id,并在加载新的 html 后使用 javascript 执行以下操作:

var element = document.getElementById('whatever');
element.style.height = window.innerHeight + 'px';

if you use vh;如果你使用 vh; automatically its happened what you want它会自动发生你想要的

This can be more easily solved using CSS using the relative units vw and vh .这可以使用 CSS 使用相对单位vwvh更容易地解决。 This unit is relative to the viewport.这个单位是相对于视口的。 It can be used like this:它可以像这样使用:

.fullscreen {
  width: 100vw;
  height: 100vh;
}

This example will always remain 100% of both width and height of the viewport and will therefor also scale responsively.此示例将始终保持视口宽度和高度的 100%,因此也会响应缩放。

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

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