【问题标题】:Why doesn't my flex work in my css? I've tried to use grid but it still doesn't work为什么我的 flex 在我的 CSS 中不起作用?我尝试使用网格,但它仍然无法正常工作
【发布时间】:2020-06-23 11:54:59
【问题描述】:
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);
}

【问题讨论】:

  • 请将该代码放在问题正文中^
  • @JenniferYaffe 我为您添加了问题的样式。请删除不必要的评论:)

标签: css reactjs


【解决方案1】:
.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);
}

【讨论】:

  • 试过了,效果很好....从来不知道 inline-flex
【解决方案2】:

这里有一些值得探索的东西

  1. flex:1 css 应该在孩子而不是父母身上,所以写在 .slide 上
  2. 图像因添加问题而臭名昭著,您可以检查图像是否存在不尊重 flex 的问题img{max-width:100%;max-height:100%}

【讨论】:

  • 如果你去掉图像并放置文本,如果你去掉转换,这是否有效?
  • 我摆脱了图像和转换,它仍然无法正常工作......
  • 我在不同的应用程序中使用了相同的代码,它在那里工作......只是在这里它不工作不知道为什么
  • 也许你有某种父 CSS 或者你正在覆盖某些东西?我建议使用 chrome 开发人员工具并尝试检查不同的元素以查看发生了什么
猜你喜欢
  • 2020-01-01
  • 2022-08-20
  • 2022-11-14
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多