为什么 “style.height/width” 不适用于变量,只适用于设置的值?

huangapple go评论67阅读模式
英文:

Why does "style.height/width" doesn't work with variables, only set values?

问题

目前正在尝试使用Flexbox和一些DOM操作来显示一个16x16的网格。以下是我想要在网格容器内创建的16个块行的代码:

<div id="grid-container"></div>
// 获取网格的尺寸
const gridContainer = document.getElementById("grid-container");
const gridHeight = gridContainer.offsetHeight;
const gridWidth = gridContainer.offsetWidth;

function createBlock() {
  const gridBlock = document.createElement("div");
  gridBlock.style.height = gridHeight / 16;
  gridBlock.style.width = gridWidth / 16;
  gridBlock.style.border = "1px solid black";
  gridContainer.appendChild(gridBlock);
}

function createRow() {
  for (let i = 0; i < 16; i++) {
    createBlock();
  }
}

createRow();
#grid-container {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  align-items: flex-start;
  background-color: #FFF;
  height: 40rem;
  width: 40rem;
}

如果我使用console.log输出gridBlock.style.heightwidth值是存在的,但它们不会创建一个块。

如果我将它们设置为固定值,比如40px,那么代码会完美运行,网格行就会按预期显示。

我知道可以使用CSS Grid和其他方法来创建网格,但Flexbox和DOM操作是我目前正在学习的内容,因此需要使用它们。非常感谢任何帮助 为什么 “style.height/width” 不适用于变量,只适用于设置的值?

英文:

Currently trying to display a 16x16 grid using Flexbox and some DOM manipulation. This is the code that I would like to create a 16 block row inside the grid container:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Gets grid&#39;s dimensions
const gridContainer = document.getElementById(&quot;grid-container&quot;);
const gridHeight = gridContainer.offsetHeight;
const gridWidth = gridContainer.offsetWidth;


function createBlock() {
  const gridBlock = document.createElement(&quot;div&quot;);
  gridBlock.style.height = gridHeight / 16;
  gridBlock.style.width = gridWidth / 16;
  gridBlock.style.border = &quot;1px solid black&quot;;
  gridContainer.appendChild(gridBlock);
}

function createRow() {
  for (let i = 0; i &lt; 16; i++) {
    createBlock();
  }
}

createRow();

<!-- language: lang-css -->

#grid-container {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  align-items: flex-start;
  background-color: #FFF;
  height: 40rem;
  width: 40rem;
}

<!-- language: lang-html -->

&lt;div id=&quot;grid-container&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

If I console.log gridBlock.style.height and width, the values are there, but they do not create a block.

If I set them to a fixed value like 40px, the code runs perfectly and the grid row is there as intended.

I know that I can create them with CSS Grid and other methods, but Flexbox and DOM manipulation is what I'm learning ATM and this needs to be done using both. Any help is highly appreciated 为什么 “style.height/width” 不适用于变量,只适用于设置的值?

答案1

得分: 1

你需要在宽度/高度后面添加一个单位,因为这些属性的无单位值对CSS没有意义。

function createBlock() {
    const gridBlock = document.createElement("div");
    gridBlock.style.height = `${gridHeight / 16}px`;
    gridBlock.style.width = `${gridWidth / 16}px`;
    gridBlock.style.border = "1px solid black";
    gridContainer.appendChild(gridBlock);
}

如果你来自React世界,这是需要适应的事情,因为React会智能地将数值转换为内部的像素值:

{/* 在React世界中相同 */}
<div style={{ width: 16 }} />
<div style={{ width: '16px' }} />
英文:

You need to append a unit to the width/height, because unitless values for these properties don't mean anything to CSS.

function createBlock() {
    const gridBlock = document.createElement(&quot;div&quot;);
    gridBlock.style.height = `${gridHeight / 16}px`;
    gridBlock.style.width = `${gridWidth / 16}px`;
    gridBlock.style.border = &quot;1px solid black&quot;;
    gridContainer.appendChild(gridBlock);
}

If you're coming from the React world, it is something that needs getting used to, as React intelligently casts numeric values to pixel values internally:

{/* Identical in React world */}
&lt;div style={{ width: 16 }} /&gt;
&lt;div style={{ width: &#39;16px&#39; }} /&gt;

答案2

得分: 0

以下是翻译好的部分:

自从你说你想学习CSS,你可以使用一个类和一些 `calc()` 来实现,这里我添加了一些颜色作为边框和背景,只是为了展示它的工作原理。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    // 获取网格的尺寸
    const gridContainer = document.getElementById("grid-container");

    function createBlock() {
      const gridBlock = document.createElement("div");
      gridBlock.classList.add("sixteenth");
      gridContainer.appendChild(gridBlock);
    }

    function createRow() {
      for (let i = 0; i < 16; i++) {
        createBlock();
      }
    }

    createRow();

<!-- language: lang-css -->

    #grid-container {
      display: flex;
      flex-flow: row nowrap;
      justify-content: flex-start;
      align-items: flex-start;
      background-color: #FFF;
      height: 40rem;
      width: 40rem;
    }

    .sixteenth {
      width: calc(100% / 16);
      height: calc(100% / 16);
      border: solid 1px green;
      background-color: #ddffdd44;
    }

<!-- language: lang-html -->

    <div id="grid-container"></div>

<!-- end snippet -->

请注意,代码部分没有翻译。

英文:

Since you say you want to learn CSS you can do this using a class and some calc() here I added some color for border and background just to show it working.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Gets grid&#39;s dimensions
const gridContainer = document.getElementById(&quot;grid-container&quot;);

function createBlock() {
  const gridBlock = document.createElement(&quot;div&quot;);
  gridBlock.classList.add(&quot;sixteenth&quot;);
  gridContainer.appendChild(gridBlock);
}

function createRow() {
  for (let i = 0; i &lt; 16; i++) {
    createBlock();
  }
}

createRow();

<!-- language: lang-css -->

#grid-container {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  align-items: flex-start;
  background-color: #FFF;
  height: 40rem;
  width: 40rem;
}

.sixteenth {
  width: calc(100% /16);
  height: calc(100% /16);
  border: solid 1px green;
  background-color: #ddffdd44;
}

<!-- language: lang-html -->

&lt;div id=&quot;grid-container&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 21:35:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定