提问者:小点点

为什么我的flex不能在我的CSS中工作? 我试过用网格,但还是不行


const PictureBox = () => {
    **this is an array of pictures**
    const allPictures = [
        { before: Sofa1, after: Sofa2 },
        { before: Sofa1, after: Sofa2 },
        { before: Sofa1, after: Sofa2 },
        { before: Sofa1, after: Sofa2 }
    ];
   
    **setting the state**
    const [x, setX] = useState(0)

    **functions that slide left and right based on the state**
    const goLeft = () => {
        x === 200 * (allPictures.length - 1) ? setX(0) : setX(x + 200)
        x === 200 * (allPictures.length - 1) ? setX(0) : setX(x + 200)
    }
    const goRight = () => {
        x === 0 ? setX(200 * (allPictures.length - 1)) : setX(x - 200)
        x === 0 ? setX(200 * (allPictures.length - 1)) : setX(x - 200)
    }

    return (
        <div className="slider"  >
            <div>
                 {allPictures.map((img, index) => {
                return (
                   
                    <div className="slide" style={{ transform:`translateX(${x}%)`}} key={index}>
                        <img src={img.before}  alt="hello" height='100%' width='50%' />
                        <img src={img.after}  alt="hello" height='100%' width='50%' />
                    </div>
                )
            })}
            </div>
           
            <button id='goleft' onClick={goLeft}>left</button>
            <button id='goright' onClick={goRight} >right</button>
        </div>
    )
}


export default PictureBox;

样式:

.slider {
    height: 450px;
    min-width: 100%;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    position: relative;
    display: flex;
    flex-direction: row;
    flex: 1;
    border-radius: 12px; 
    /* overflow: hidden; */
}

.slide {
    min-width: 100%;
    height: 100%; 
    transition: 0.5s;
    box-sizing: border-box; 
    border: 1px double skyblue;
    background: rgb(2, 67, 95);
}

共2个答案

匿名用户

.slider{
     height: 450px;
    min-width: 100%;
     box-sizing: border-box;
    margin: 0;
    padding:0;
    position: relative;
    display:flex;
    flex: 1;
    border-radius: 12px;
    /* overflow: hidden; */
}


.slide{
    display: inline-flex;
    flex: 1;
    min-width: 100%;
    height: 100%;
    transition: 0.5s;
    box-sizing: border-box;
    border: 1px double skyblue;
    background: rgb(2, 67, 95);
}

匿名用户

下面是一些值得探索的东西

  1. flex:1css应该在子级而不是父级上,因此请将其写入。幻灯片
  2. 图像以添加问题著称,您可以检查图像是否存在不尊重fleximg{max-width:100%;max-height:100%}
  3. 的问题