如何在 JS 中获取 CSS vw 和 vh?
How do I get CSS vw and vh in JS?
我正在尝试在 Javascript 中使用 CSS vw 和 vh 值,因为我想让它们适应更改某些 JS 变量。然而,在研究了不同的方法之后,这些值似乎有点偏离,因为它弄乱了我的显示。我试过使用:
viewPortWidth = window.innerWidth;
viewPortHeight = window.innerHeight;
和
viewPortWidth = document.documentElement.clientWidth;
viewPortHeight = document.documentElement.clientHeight;
但它们似乎不起作用。
有人有什么想法吗?非常感谢任何帮助!
谢谢!
一切都很好,只需连接 viewPortWidth 和 viewPortHeight
"px",像这样,
viewPortWidth = window.innerWidth + "px";
viewPortHeight = window.innerHeight + "px";
我认为这会很好用,试试吧。
// 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 window。左右移动侧栏,table 中的 domrects 将在调整大小时发生变化。
我正在尝试在 Javascript 中使用 CSS vw 和 vh 值,因为我想让它们适应更改某些 JS 变量。然而,在研究了不同的方法之后,这些值似乎有点偏离,因为它弄乱了我的显示。我试过使用:
viewPortWidth = window.innerWidth;
viewPortHeight = window.innerHeight;
和
viewPortWidth = document.documentElement.clientWidth;
viewPortHeight = document.documentElement.clientHeight;
但它们似乎不起作用。
有人有什么想法吗?非常感谢任何帮助!
谢谢!
一切都很好,只需连接 viewPortWidth 和 viewPortHeight "px",像这样,
viewPortWidth = window.innerWidth + "px";
viewPortHeight = window.innerHeight + "px";
我认为这会很好用,试试吧。
// 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 window。左右移动侧栏,table 中的 domrects 将在调整大小时发生变化。