【问题标题】:How to change this code correctly?如何正确更改此代码?
【发布时间】:2018-08-08 12:59:46
【问题描述】:

这是我第一次想使用滑动图像,我在 codepen 上找到了完美的幻灯片,但无法管理它只有 2 或 3 张图像可以滑动。找不到,如何正确设置时间。如果我只需要在每张图像 4 秒内滑动 2 或 3 张图像,它是如何工作的?

@import url('https://fonts.googleapis.com/css?family=Bubblegum+Sans|Ubuntu');
html,
body {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    font-family: 'Bubblegum Sans', cursive;
}

slider {
    display: block;
    width: 100%;
    height: 100%;
    background-color: #1f1f1f;
    overflow: hidden;
    position: absolute;
}

slider > * {
    position: absolute;
    display: block;
    width: 100%;
    height: 100%;
    background: #1f1f1f;
    animation: slide 12s infinite;
    overflow: hidden;
}

slide:nth-child(1) {
    left: 0;
    z-index: 9;
    animation-delay: -1s;
    background-image: url(https://preview.ibb.co/fm57uG/img_1.jpg);
    background-size: cover;
    background-position: center;
}

slide:nth-child(2) {
    animation-delay: 2s;
    background-image: url(https://preview.ibb.co/kkT5gw/img_2.jpg);
    z-index: 8;
    background-size: cover;
    background-position: center;
}

slide:nth-child(3) {
    animation-delay: 5s;
    background-image: url(https://preview.ibb.co/dVT5gw/img_3.jpg);
    background-size: cover;
    background-position: center;
    z-index: 7;
}

slide:nth-child(4) {
    left: 0%;
    animation-delay: 8s;
    background-image: url(https://preview.ibb.co/m7FU8b/img_4.jpg);
    background-size: cover;
    background-position: center;
    z-index: 6;
}

slide p {
    font-size: 70px;
    text-align: center;
    display: inline-block;
    width: 100%;
    margin-top: 340px;
    color: #fff;
}

@keyframes slide {
    0% {
        left: 100%;
        width: 100%;
    }
    5% {
        left: 0%;
    }
    25% {
        left: 0%;
    }
    30% {
        left: -100%;
        width: 100%;
    }
    30.001% {
        left: -100%;
        width: 0%;
    }
    100% {
        left: 100%;
        width: 0%;
    }
}
<slider>
        <slide>
            <p>Slide 1</p>
        </slide>
        <slide>
            <p>Slide 2</p>
        </slide>
        <slide>
            <p>Slide 3</p>
        </slide>
        <slide>
            <p>Slide 4</p>
        </slide>
    </slider>

提前致谢!

【问题讨论】:

    标签: css slider slideshow


    【解决方案1】:

    3 张幻灯片的示例:

    HTML

    <slider>
            <slide>
                <p>Slide 1</p>
            </slide>
            <slide>
                <p>Slide 2</p>
            </slide>
            <slide>
                <p>Slide 3</p>
            </slide>
        </slider>
    

    CSS

    slide:nth-child(1) {
        left: 0;
        z-index: 9;
        animation-delay: -1s;
        background-image: url(https://preview.ibb.co/fm57uG/img_1.jpg);
        background-size: cover;
        background-position: center;
    }
    
    slide:nth-child(2) {
        animation-delay: 3s;
        background-image: url(https://preview.ibb.co/kkT5gw/img_2.jpg);
        z-index: 8;
        background-size: cover;
        background-position: center;
    }
    
    slide:nth-child(3) {
        animation-delay: 7s;
        background-image: url(https://preview.ibb.co/dVT5gw/img_3.jpg);
        background-size: cover;
        background-position: center;
        z-index: 7;
    }
    

    诀窍是跟随动画延迟。第一张幻灯片立即开始(-1s),第二张幻灯片添加 4 秒,得到(3s),第三张幻灯片再次添加 4s(7s),依此类推。如果您需要 2 张幻灯片,请从 HTML 中删除第三张幻灯片并从 CSS 中删除第三个子项。

    注意:我只为幻灯片子项放置 CSS,请确保您也拥有其余部分。

    【讨论】:

    • 我按照你的解释做了,但它不像原来那样工作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2018-02-22
    • 1970-01-01
    • 2012-05-22
    • 2021-03-20
    相关资源
    最近更新 更多