簡體   English   中英

React Material-UI GridList 級聯布局

[英]React Material-UI GridList cascading layout

是否可以在像級聯布局這樣的 pinterest 中使用 React Material-UI 庫的GridList組件?

我使用 Material-UI Card組件作為GridList

    const cards = props.items.map((item) => <Card key={item.id} {...item} />);
    return (
      <GridList cols={3} cellHeight={'auto'}>
        {React.Children.toArray(cards)}
      </GridList>
    );

結果如下: 結果圖像

我想消除紅色圓圈中的空白。 任何幫助都感激不盡。

你想要的CSS等價物是將flex-direction設置為column而不是row ,但它看起來不像material-ui文檔,你可以選擇更改渲染事物的順序

我會嘗試使用不同的容器對象或自己構建一些東西,因為Pinterest樣式布局在確定要渲染的下一個元素的位置時需要一些更復雜的邏輯。

這個布局有一個名字:masonry,它來自建築結構,因為磚塊是如何放在一起的:) 它有一個 mui 組件:

https://mui.com/components/masonry/#basic-masonry

在這里也復制示例:

import * as React from 'react';
import Box from '@mui/material/Box';
import { styled } from '@mui/material/styles';
import Paper from '@mui/material/Paper';
import Masonry from '@mui/lab/Masonry';

const heights = [150, 30, 90, 70, 110, 150, 130, 80, 50, 90, 100, 150, 30, 50, 80];

const Item = styled(Paper)(({ theme }) => ({
  ...theme.typography.body2,
  color: theme.palette.text.secondary,
  border: '1px solid black',
  display: 'flex',
  alignItems: 'center',
  justifyContent: 'center',
}));

export default function BasicMasonry() {
  return (
    <Box sx={{ width: 500, minHeight: 393 }}>
      <Masonry columns={4} spacing={1}>
        {heights.map((height, index) => (
          <Item key={index} sx={{ height }}>
            {index + 1}
          </Item>
        ))}
      </Masonry>
    </Box>
  );
}

在此處輸入圖片說明

暫無
暫無

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

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