簡體   English   中英

如何根據百分比調整 window 內寬?

[英]How can I adjust the window innerwidth according to the percentage?

我嘗試根據百分比調整 window.innerwidth 但我不知道該怎么做。 我的目標是根據 window.innerwidth 的大小,改變 html 的背景顏色。 window.innerwidth 在比較時不能使用百分比?

我試過這樣。

const resizeColor = function() {
  let width = window.innerWidth;
  if (width >= "90%") {
    document.body.style.backgroundColor = "red";
  } else if (width < "90%" && "60%" <= width) {
    document.body.style.backgroundColor = "blue";
  } else {
    document.body.style.backgroundColor = "white";
  }
};

"90%"不是innerWidth屬性的有效值。 我認為您的意思是 90% 的outerWidth 在整中嘗試以下內容

 const resizeColor = function() { let width = window.innerWidth; let vw = window.outerWidth; // viewport-width if (width >= 0.9 * vw) { document.body.style.backgroundColor = "red"; } else if (width < 0.9 * vw && 0.6 * vw <= width) { document.body.style.backgroundColor = "blue"; } else { document.body.style.backgroundColor = "white"; } }; window.onresize = resizeColor;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM