響應(yīng)式示例, 圖片自然放大縮小

總結(jié)下響應(yīng)式該如何處理。
一、有圖片時(shí)圖片直接設(shè)置 width 100%; 關(guān)鍵點(diǎn):如果是Flex布局中是要用好flex屬性, 如果它是1, 那圖片就會(huì)占滿當(dāng)前的容器,隨容器大小來(lái)變化。Grid 布局用的少,直接的width 100%貌似也可以。
二、圖片可以加上minWidth 屬性來(lái)控制最小顯示時(shí)該占有的寬度。
三、有二維的布局時(shí)要考慮使用Grid, 即兩行兩行或以上的情況。
四、Flex 布局當(dāng)圖片src地址失效后是不占寬高的, 但Grid布局占高寬,看下圖


image.png

使用chakra-ui 的 demo, 代碼如下:
index.jsx:

import {
  Box,
  Flex,
  Image,
  Text,
  Grid,
  GridItem,
  useBreakpointValue,
} from '@chakra-ui/react';
import { Eyebrow, Headline, CTAButton } from './index.styles';
const Demo = ({
  eyebrow,
  headline,
  body,
  CTA1,
  CTA2,
  largeImgSrc,
  largeImgAlt,
  smallImages,
  bgcolor,
}) => {
  const layout = useBreakpointValue({
    base: 'column',
    md: bgcolor ? (largeImgSrc ? 'column' : 'row') : 'row',
    lg: 'row',
  });

  return (
    <Flex
      direction={layout}
      justify={'center'}
      align={'center'}
      backgroundColor={bgcolor}
      p={bgcolor ? '20px' : 0}
      borderRadius={{ base: '4px', lg: '8px' }}
      id="collageComponentWrap"
      gap={'12px'}
    >
      <Box
        id="firstSection"
        w={'100%'}
        alignSelf={{ md: !largeImgSrc ? 'center' : 'flex-start', lg: 'center' }}
        flex={{ base: 1, md: 1 }}
      >
        <Flex
          width={{ base: '100%' }}
          display="column"
          justifyContent="center"
          align={'left'}
          pt="12px"
          id="collageTextSection"
        >
          {eyebrow && largeImgSrc && (
            <Eyebrow id="eyebrow" fontSize={bgcolor ? '18px' : '16px'}>
              {eyebrow}
            </Eyebrow>
          )}
          {headline && (
            <Headline
              id="headline"
              fontSize={!largeImgSrc ? '32px' : '24px'}
              fontWeight={!largeImgSrc ? 900 : 400}
            >
              {headline}
            </Headline>
          )}
          {body && <Text id="bodyText">{body}</Text>}
        </Flex>
        <Flex my={'12px'} dir="row" justify={'flex-start'} gap={'12px'} flexWrap={'wrap'}>
          {CTA1?.label && (
            <CTAButton
              as="a"
              href={CTA1?.href}
              id="CTA1"
              color={CTA1?.color || 'inherit'}
              borderColor={CTA1?.borderColor}
            >
              {CTA1?.label}
            </CTAButton>
          )}
          {CTA2?.label && (
            <CTAButton
              as="a"
              href={CTA2?.href}
              id="CTA2"
              color={CTA2?.color || 'inherit'}
              borderColor={CTA2?.borderColor}
            >
              {CTA2?.label}
            </CTAButton>
          )}
        </Flex>
      </Box>
      <Flex
        id="collageImageSection"
        direction={{ base: 'column', md: 'row' }}
        width={{ base: '100%' }}
        gap={'20px'}
        justify={{ base: 'center', md: 'flex-start' }}
        align={{ base: 'center' }}
        flex={{ base: 2, md: largeImgSrc ? 2 : 1, lg: 2 }}
      >
        {largeImgSrc && (
          <Box id="bigImage" flex={1} w={'100%'}>
            <Image
              src={largeImgSrc}
              alt={largeImgAlt}
              borderRadius={'8px'}
              objectFit={'cover'}
              width={'100%'}
              minWidth={'196px'}
            />
          </Box>
        )}
        <Grid
          id="smallImagesGroup"
          templateRows={{
            base: 'repeat(2, 1fr)',
            lg: largeImgSrc ? 'repeat(2, 1fr)' : 'unset',
          }}
          templateColumns={{
            base: 'repeat(2, 1fr)',
            lg: largeImgSrc ? 'repeat(2, 1fr)' : 'repeat(4, 1fr)',
          }}
          gap={4}
          flex={1}
          w={'100%'}
        >
          {smallImages?.map((img, index) => (
            <GridItem key={index}>
              <Image
                src={img.src}
                alt={img.alt}
                key={index}
                width={'100%'}
                // maxWidth={'205px'}
                minWidth={'89px'}
              />
            </GridItem>
          ))}
        </Grid>
      </Flex>
    </Flex>
  );
};
export default Demo;

index.styles.js

import { Box, Button } from '@chakra-ui/react';
import styled from '@emotion/styled';

export const Eyebrow = styled(Box)`
  font-weight: 700;
`;

export const Headline = styled(Box)`
  font-family: Gelica;
  font-size: ${(props) => props?.fontSize || 'inherit'};
  line-height: 120%;
`;

export const CTAButton = styled(Button)`
  border-radius: 28px;
  min-width: 52px;
  border: 2px solid #1c1c1c;
  background: #fff;
  padding: 12px 20px;
  border-color: ${(props) => props?.borderColor || 'inherit'};
`;

推薦一篇 Flex布局的技術(shù)文章,可以交互直接看效果,鏈接:An Interactive Guide to Flexbox. 這些東西都是打基礎(chǔ),了解后直接干就好

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容