【问题标题】:Using React-Bootstrap, how can i keep my Instrument Cards dynamically sized, while still specifying a minimum card size使用 React-Bootstrap,我如何保持我的仪器卡动态大小,同时仍然指定最小卡大小
【发布时间】:2021-10-13 14:52:14
【问题描述】:

所以,这就是我的困境

这是我的卡片目前的样子:

这看起来一切都很好,但是,当你改变屏幕尺寸时,它就是这样的:

这不是很好,因为我们希望引导应用程序,以及现在任何网站都能够响应。

我想要的是让卡片水平拉伸,这样无论我们的显示器宽度是多少,卡片都会填满整个卡片组——也就是标题为“仪器列表”的底部部分

现在,如果我放了

style={{flexGrow: 1}} 

在每张卡片上,然后它似乎有点工作,但是,并没有像我真正想要的那样工作。

如果我有一排卡片不包含 5 张卡片,它会将它们拉长:

我只想让我的卡片组伸展以填满整个容器,但我也希望它保持我卡片的标准尺寸

这是我的 Card Deck 代码,它从 JSON 文件和 Card 项目生成仪器。

列表代码:

import InstrumentCard from './InstrumentCard'
import styles from '../styles/InstrumentList.module.scss'
import {CardDeck} from 'react-bootstrap'


const InstrumentList = ({instruments, onDel, onLoc, onShuf, onRep, onRepButClick, setRepInstrumentID}) => {
    
    return (
            <CardDeck key={1} className={styles.container} style={{display: 'flex', flexDirection: 'row'}}>
                {instruments.map((instrument, i) => (
                    <InstrumentCard
                        key={i} 
                        id={i}
                        instr = {instrument}
                        name={instrument.name} 
                        description={instrument.description}
                        imagePath={instrument.image}
                        wikiLink={instrument.wikipedia}
                        tubeLink={instrument.youtube}
                        onDelete = {onDel}
                        isLocked = {instrument.locked}
                        onLock = {onLoc}
                        onShuffle = {onShuf}
                        onReplace = {onRep}
                        onRepButtonClick = {onRepButClick}
                        setReplacementInstrumentID = {setRepInstrumentID}
                        style={{flex: 1}}>
                            
                    </InstrumentCard>
                )) }
            </CardDeck>
    )
}

export default InstrumentList

卡号:

import {Card, Button, ButtonGroup, ButtonToolbar} from 'react-bootstrap'
import styles from '../styles/InstrumentCard.module.scss'
import {useState} from 'react'

const InstrumentCard = ({id, name, description, imagePath, wikiLink, tubeLink, onDelete, isLocked, onLock, onShuffle, setReplacementInstrumentID, onRepButtonClick}) => {
    
    return (
            <Card style={{ minWidth: '18rem', flexGrow: 1, margin:'1rem', minHeight:'32rem'}}>
                
                <div>
                    <button id="lockButton" onClick={() => {onLock(id)}} className={isLocked ? styles.btnOverrideLocked : styles.btnOverride}>{isLocked ? "????" : "????"}</button>
                    <button id="shuffleButton" autoFocus={true} onClick={()=> {onShuffle(id)}} className={styles.btnOverrideShuffle} style={{float: "right"}}><span>????</span></button>
                </div>
                <Card.Img variant="top" src={imagePath} style={{padding:'1rem', maxHeight: '10rem', height:'100%', objectFit: 'contain'}} />
                <Card.Body style={{display: 'flex', flexDirection: 'column', justifyContent: 'space-between'}}>
                    <div>
                    <Card.Title>{name}</Card.Title>
                    <Card.Text>
                        {description}
                    </Card.Text>
                    </div>
                    <div style={{marginTop: '.5rem'}}>
                    <ButtonGroup style={{width: "100%"}}>
                        <Button variant="danger" onClick={()=> onDelete(id)}>Delete</Button>
                        <Button variant="secondary" onClick={()=> {setReplacementInstrumentID(id); onRepButtonClick()}} >Replace</Button>
                    </ButtonGroup>
                    </div>
                </Card.Body>
                <Card.Footer>
                    <Card.Link target="_blank" rel="noopener noreferrer" href={wikiLink}>Wikipedia</Card.Link>
                    <Card.Link target="_blank" rel="noopener noreferrer" href={tubeLink}>Youtube</Card.Link>
                </Card.Footer>
            </Card>
    )
}

export default InstrumentCard

非常感谢任何帮助或解释!提前谢谢!

编辑:我尝试过但失败的事情是使用 map 并创建一个变量“让 newIndex = (i*5) 并尝试像 newIndex + 1 一样使用来获取其他索引,并尝试在一个内生成 5 个引导列但是,这不起作用,至少对于 map 功能,因为除了其他问题之外,没有办法阻止 map 超出仪器的实际数量。

我也尝试为我的卡片添加最大宽度和高度,但是,这仍然允许它们按行增长和缩小,这是我们不希望的

我觉得某种引导程序 Col 和 Row 生成会很理想,但我不知道如何在 return 语句中使用 javascript 让它每行只生成 5 个元素。

【问题讨论】:

    标签: reactjs bootstrap-4 next.js react-bootstrap


    【解决方案1】:

    我已经找到解决办法了!

    所以,基本上在我的 InstrumentList 中,我完全放弃使用卡片组,而只使用 Row 元素。在 instrument.map 中,我用 Col 元素围绕每张乐器卡,然后对其应用一些自定义样式,因为我想要正好五列,而 12 不能除以 5,所以我不得不稍微捏造一下。

    这是我最终的 InstrumentList

    import InstrumentCard from './InstrumentCard'
    import styles from '../styles/InstrumentList.module.scss'
    import {CardDeck, Row, Col} from 'react-bootstrap'
    
    
    const InstrumentList = ({instruments, onDel, onLoc, onShuf, onRep, onRepButClick, setRepInstrumentID}) => {
        
        return (
                <Row key={1} className={styles.container} style={{display: 'flex', flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'flex-start' }}>
                    {instruments.map((instrument, i) => (
                        <Col className={styles.properCol}>
                        <InstrumentCard
                            key={i} 
                            id={i}
                            instr = {instrument}
                            name={instrument.name} 
                            description={instrument.description}
                            imagePath={instrument.image}
                            wikiLink={instrument.wikipedia}
                            tubeLink={instrument.youtube}
                            onDelete = {onDel}
                            isLocked = {instrument.locked}
                            onLock = {onLoc}
                            onShuffle = {onShuf}
                            onReplace = {onRep}
                            onRepButtonClick = {onRepButClick}
                            setReplacementInstrumentID = {setRepInstrumentID}
                            style={{flex: 1}}>
                                
                        </InstrumentCard>
                        </Col>
                    )) }
                   
                </Row>
        )
    }
    
    export default InstrumentList
    

    最终乐器卡

    import {Card, Button, ButtonGroup, Col} from 'react-bootstrap'
    import styles from '../styles/InstrumentCard.module.scss'
    import {useState} from 'react'
    
    const InstrumentCard = ({id, name, description, imagePath, wikiLink, tubeLink, onDelete, isLocked, onLock, onShuffle, setReplacementInstrumentID, onRepButtonClick}) => {
        
        return (
                
                <Card style={{minWidth: '16rem', flexGrow: 1, margin:'1rem', minHeight:'32rem'}}>
                    <div>
                        <button id="lockButton" onClick={() => {onLock(id)}} className={isLocked ? styles.btnOverrideLocked : styles.btnOverride}>{isLocked ? "?" : "?"}</button>
                        <button id="shuffleButton" autoFocus={true} onClick={()=> {onShuffle(id)}} className={styles.btnOverrideShuffle} style={{float: "right"}}><span>?</span></button>
                    </div>
                    <Card.Img variant="top" src={imagePath} style={{padding:'1rem', maxHeight: '10rem', height:'100%', objectFit: 'contain'}} />
                    <Card.Body style={{display: 'flex', flexDirection: 'column', justifyContent: 'space-between'}}>
                        <div>
                        <Card.Title>{name}</Card.Title>
                        <Card.Text>
                            {description}
                        </Card.Text>
                        </div>
                        <div style={{marginTop: '.5rem'}}>
                        <ButtonGroup style={{width: "100%"}}>
                            <Button variant="danger" onClick={()=> onDelete(id)}>Delete</Button>
                            <Button variant="secondary" onClick={()=> {setReplacementInstrumentID(id); onRepButtonClick()}} >Replace</Button>
                        </ButtonGroup>
                        </div>
                    </Card.Body>
                    <Card.Footer>
                        <Card.Link target="_blank" rel="noopener noreferrer" href={wikiLink}>Wikipedia</Card.Link>
                        <Card.Link target="_blank" rel="noopener noreferrer" href={tubeLink}>Youtube</Card.Link>
                    </Card.Footer>
                </Card>
        )
    }
    
    export default InstrumentCard
    

    以及 InstrumentList 的样式

    .container {
        margin: 2rem;
    }
    
    .properCol {
        flex: 0 0 20%;
        max-width: 20%;
        position: relative;
        width: 100%;
        padding: 0;
    }
    
    @media (max-width: 1400px) {
        .properCol {
            flex: 0 0 25%;
            max-width: 25%;
            position: relative;
            width: 25%;
            padding: 0;
        }
    }
    
    @media (max-width: 1150px) {
        .properCol {
            flex: 0 0 33.333333%;
            max-width: 33.333333%;
            position: relative;
            width: 33.333333%;
            padding: 0;
        }
    }
    
    @media (max-width: 900px) {
        .properCol {
            flex: 0 0 50%;
            max-width: 50%;
            position: relative;
            width: 50%;
            padding: 0;
        }
    }
    
    @media (max-width: 620px) {
        .properCol {
            flex: 0 0 100%;
            max-width: 100%;
            position: relative;
            width: 100%;
            padding: 0;
        }
    }
    

    真诚地希望这对将来的其他人有所帮助!我不得不在一段时间内处理的最大的 PITA 之一。干杯!

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2020-08-04
      • 2021-01-30
      • 2021-12-04
      • 1970-01-01
      • 2017-01-03
      • 2017-04-23
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多