如何在JS中获取CSS vw和vh?

发布于 2025-01-20 16:10:07 字数 400 浏览 3 评论 0原文

我正在尝试在JavaScript中使用CSS VW和VH值,因为我想使它们适应更改某些JS变量。但是,在研究了不同的方法之后,这些值似乎已经略有一点,因为它弄乱了我的显示。我已经尝试使用:

viewPortWidth = window.innerWidth;
viewPortHeight = window.innerHeight; 

但是

 viewPortWidth = document.documentElement.clientWidth;
 viewPortHeight = document.documentElement.clientHeight;

它们似乎没有起作用。

有人有想法吗?任何帮助都非常感谢!

谢谢!

I am trying to use the CSS vw and vh values in Javascript because I would like to make them adaptive to changing certain JS variables. However, after researching different methods, the values seem to be off by a little bit because it is messing up my display. I've tried using:

viewPortWidth = window.innerWidth;
viewPortHeight = window.innerHeight; 

and

 viewPortWidth = document.documentElement.clientWidth;
 viewPortHeight = document.documentElement.clientHeight;

But they do not seem to be working.

Does anyone have any ideas? Any help is much appreciated!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

允世 2025-01-27 16:10:07

一切都很好
使用“ px”

viewPortWidth = window.innerWidth + "px";
viewPortHeight = window.innerHeight + "px";

我认为这会很好,请尝试一下。

Everything is fine just concatenate the viewPortWidth and viewPortHeight
with "px", like this,

viewPortWidth = window.innerWidth + "px";
viewPortHeight = window.innerHeight + "px";

I think this will work just fine, try it.

风筝有风,海豚有海 2025-01-27 16:10:07
// console.log(document.body.getBoundingClientRect(),window.innerWidth, window.innerHeight);

var getrects=()=>["top","left","height","width",
              "x","y","right","bottom"
             ].map(a=>[a,document.body.getBoundingClientRect()[a]]).concat([['window height',window.innerHeight],["window width",window.innerWidth]]);

//var entries0=getrects();

//entries0.forEach(a=>console.log(a));

const tabler=(
  tbl=document.getElementById("table"),
  entries=getrects())=>(tbl.innerHTML='',
  tbl.innerHTML=`<thead>
<tr>${entries.map(a=>`<th>${a[0]}</th>`).join("")}</tr></thead>
<tbody><tr>${entries.map(a=>`<td>${Math.round(a[1])}px</td>`).join("")}</tr></tbody>`
);

window.addEventListener("resize",e=>tabler());
tabler();
* {
  box-sizing: border-box;
  margin:1em;
}
::-webkit-scrollbar {
  display: none;
}
html{padding:0; margin:0;}
body{margin:0;padding:0;
  position: absolute; left:0; right:0; top: 0; bottom: 0px;
  background-color: aliceblue;
  
}









table {
    margin: 1em;
    background-color: #f8f9fa;
/*     border: 1px solid */
    border-collapse: collapse;
    color: #000;
        border: #a2a9b1  ridge 1px;
    font-size: 100%;
    display: table;
}
tbody {
    display: table-row-group;
    vertical-align: middle;
    border-color: inherit;
    margin: 1em 0;
    background-color: #f8f9fa;
/*     border: 4px solid #a2a9b1; */
    border-collapse: collapse;
    color: #000;
}
caption{font-weight: 600; border: #a2a9b1  ridge 1px;background: white; border-bottom: 0px; padding: .3em;}
tbody{
    display: table-row-group;
    vertical-align: middle;
    border-color: inherit;
}
tr {
    display: table-row;
    vertical-align: inherit;
    border-color: inherit;
    padding: .2em;
}
tr > th {
    background-color: #eaecf0;
    text-align: center;
}
tr > th, tr > td {
    border: 1px solid #a2a9b1;
    padding: 0.2em 0.4em;
}
th.thleft{
     text-align: left;
    padding-right: 1.6em;
}
p{margin:1em;}
<h4>The css you likely want is below</h4>
<code>
 
  * {
  box-sizing: border-box;
}
::-webkit-scrollbar {
  display: none;
}
html{padding:0; margin:0;}
body{margin:0;padding:0;
  position: absolute; left:0; right:0; top: 0; bottom: 0px;
  background-color: aliceblue;
  
}
</code>
<p>  Then just check window.innerWidth and innerHeight as well as the bounding rects on document.body...
  </p>

<p> you can also propertize the body rects nad then just pass them down your node tree.</p>
<table id="table">
  
</table>

看看这支笔,它得到了主体元素和内部窗口的 vh 和 vw 。移动侧边栏,表格中的 domrect 将在调整大小时发生变化。

https://codepen.io/jfrazz/pen/KKZeaGe

// console.log(document.body.getBoundingClientRect(),window.innerWidth, window.innerHeight);

var getrects=()=>["top","left","height","width",
              "x","y","right","bottom"
             ].map(a=>[a,document.body.getBoundingClientRect()[a]]).concat([['window height',window.innerHeight],["window width",window.innerWidth]]);

//var entries0=getrects();

//entries0.forEach(a=>console.log(a));

const tabler=(
  tbl=document.getElementById("table"),
  entries=getrects())=>(tbl.innerHTML='',
  tbl.innerHTML=`<thead>
<tr>${entries.map(a=>`<th>${a[0]}</th>`).join("")}</tr></thead>
<tbody><tr>${entries.map(a=>`<td>${Math.round(a[1])}px</td>`).join("")}</tr></tbody>`
);

window.addEventListener("resize",e=>tabler());
tabler();
* {
  box-sizing: border-box;
  margin:1em;
}
::-webkit-scrollbar {
  display: none;
}
html{padding:0; margin:0;}
body{margin:0;padding:0;
  position: absolute; left:0; right:0; top: 0; bottom: 0px;
  background-color: aliceblue;
  
}









table {
    margin: 1em;
    background-color: #f8f9fa;
/*     border: 1px solid */
    border-collapse: collapse;
    color: #000;
        border: #a2a9b1  ridge 1px;
    font-size: 100%;
    display: table;
}
tbody {
    display: table-row-group;
    vertical-align: middle;
    border-color: inherit;
    margin: 1em 0;
    background-color: #f8f9fa;
/*     border: 4px solid #a2a9b1; */
    border-collapse: collapse;
    color: #000;
}
caption{font-weight: 600; border: #a2a9b1  ridge 1px;background: white; border-bottom: 0px; padding: .3em;}
tbody{
    display: table-row-group;
    vertical-align: middle;
    border-color: inherit;
}
tr {
    display: table-row;
    vertical-align: inherit;
    border-color: inherit;
    padding: .2em;
}
tr > th {
    background-color: #eaecf0;
    text-align: center;
}
tr > th, tr > td {
    border: 1px solid #a2a9b1;
    padding: 0.2em 0.4em;
}
th.thleft{
     text-align: left;
    padding-right: 1.6em;
}
p{margin:1em;}
<h4>The css you likely want is below</h4>
<code>
 
  * {
  box-sizing: border-box;
}
::-webkit-scrollbar {
  display: none;
}
html{padding:0; margin:0;}
body{margin:0;padding:0;
  position: absolute; left:0; right:0; top: 0; bottom: 0px;
  background-color: aliceblue;
  
}
</code>
<p>  Then just check window.innerWidth and innerHeight as well as the bounding rects on document.body...
  </p>

<p> you can also propertize the body rects nad then just pass them down your node tree.</p>
<table id="table">
  
</table>

check out this pen it gets vh and vw both of the body element and the inner window. Move the side bar around and the domrects in the table will change on resize.

https://codepen.io/jfrazz/pen/KKZeaGe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文