如何在React中创建一个带有上下滚动的网格。

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

How to create a grid with up and down in react js

问题

I want to create a grid with 3 columns, each column have 4 rows. First and last one start with margin-top 0, center one start with margin-top of 15px.

上面的图片是一个示例。

Thanks for the help.

I added the image, I want a code for this example in css

.hand-item-grid {
  display: grid;
  grid-template-columns: repeat(var(--player-count), auto);
  grid-template-rows: repeat(1, auto);
  column-gap: calc(var(--card-gap) * 2);
}
.hand-grid-columns {
  padding: var(--card-gap);
  display: grid;
  grid-row: 1 / 2;
  grid-gap: var(--card-gap);
}
<div
  className="hand-grid-columns"
  id={`hand-grid-col-${i + 1}`}
  key={i}
  onMouseEnter={() => setHovered(true)}
  onMouseLeave={() => setHovered(false)}
>
  {hand.map((card, j) => {
    <HandGridData
      key={`${card.rank}${card.suit}`}
      card={card}
      player={i}
      trump={props.trump}
      share={props.share}
      forLayer={props.forLayer}
    />;
  })}
</div>
英文:

I want to create a grid with 3 columns, each column have 4 rows. First and last one start with margin-top 0, center one start with margin-top of 15px 如何在React中创建一个带有上下滚动的网格。

Above image is for example.
Thanks for the help.

I added the image, I want a code for this example in css

.hand-item-grid {
  display: grid;
  grid-template-columns: repeat(var(--player-count), auto);
  grid-template-rows: repeat(1, auto);
  column-gap: calc(var(--card-gap) * 2);
}
.hand-grid-columns {
  padding: var(--card-gap);
  display: grid;
  grid-row: 1 / 2;
  grid-gap: var(--card-gap);
}
       
   &lt;div
      className=&quot;hand-grid-columns&quot;
      id={`hand-grid-col-${i + 1}`}
      key={i}
      onMouseEnter={() =&gt; setHovered(true)}
      onMouseLeave={() =&gt; setHovered(false)}
    &gt;
      {hand.map((card,j) =&gt; {
        
          &lt;HandGridData
            key={`${card.rank}${card.suit}`}
            card={card}
            player={i}
            trump={props.trump}
            share={props.share}
            forLayer={props.forLayer}
          /&gt;
        
      })}
    &lt;/div&gt;

答案1

得分: 0

你可以使用弹性布局 (flex layout)。flexDirection 样式属性可以控制 div 元素内部元素的方向。

const App = () => {
  const noOfColumns = 3;
  const noOfRows = 4;
  const Box = () => {
    return (
      <div
        style={{
          backgroundColor: "white",
          marginTop: 2,
          border: "6px solid",
          width: 50,
          height: 50
        }}
      ></div>
    );
  };
  return (
    <div style={{ display: "flex", backgroundColor: "darkgray" }}>
      {[...Array(noOfColumns)].map((e, i) => (
        <div
          key={i}
          style={{
            marginRight: 10,
            marginTop: (i + 1) % 2 == 0 ? 15 : 0
          }}
        >
          {[...Array(noOfRows)].map((e, i) => (
            <Box 
              key={i}/>
          )}
        </div>
      ))}
    </div>
  );
}

// 渲染
ReactDOM.createRoot(
  document.getElementById("root")
).render(
  <App />
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js"></script>
英文:

You could use flex layout. flexDirection style attribute would control the direction of elements within the div.

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

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

const App = ()=&gt; {
  const noOfColumns = 3;
  const noOfRows = 4;
  const Box = () =&gt; {
    return (
      &lt;div
        style={{
          backgroundColor: &quot;white&quot;,
          marginTop: 2,
          border: &quot;6px solid&quot;,
          width: 50,
          height: 50
        }}
      &gt;&lt;/div&gt;
    );
  };
  return (
    &lt;div style={{ display: &quot;flex&quot;, backgroundColor: &quot;darkgray&quot; }}&gt;
      {[...Array(noOfColumns)].map((e, i) =&gt; (
        &lt;div
        key={i}
          style={{
            marginRight: 10,
            marginTop: (i + 1) % 2 == 0 ? 15 : 0
          }}
        &gt;
          {[...Array(noOfRows)].map((e, i) =&gt; (
            &lt;Box 
        key={i}/&gt;
          ))}
        &lt;/div&gt;
      ))}
    &lt;/div&gt;
  );
}


// Render it
ReactDOM.createRoot(
    document.getElementById(&quot;root&quot;)
).render(
    &lt;App /&gt;
);

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

&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 15:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553912.html
匿名

发表评论

匿名网友

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

确定