CSS动画延迟计时

时间:2017-04-18 12:43:34

标签: css css-animations

我从JSFiddle中获取了这个示例代码并使用它,但是代码让我感到困惑

.backgroundimg {
    -webkit-background-size: cover;
  	-moz-background-size: cover;
  	-o-background-size: cover;
  	background-size: cover;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;

}

#back5 {
    background: url("http://duananhalotus.com/upload/album/0904234.jpg") no-repeat center fixed;
    z-index: -1;
}

#back4 {
    background: url("http://www.chinadaily.com.cn/world/images/attachement/jpg/site1/20120806/d4bed9d534551189e67329.jpg") no-repeat center fixed;
    z-index: -1;
}

#back3 {
    background: url("https://amazingslider.com/wp-content/uploads/2012/12/dandelion.jpg") no-repeat center fixed;
    z-index: -1;
}

#back2 {
    background: url("https://cdn.pixabay.com/photo/2013/04/06/11/50/image-editing-101040_960_720.jpg") no-repeat center fixed;
    z-index: -1;
}

#back1 {
    background: url("http://www.gettyimages.com/gi-resources/images/Embed/new/embed2.jpg") no-repeat center fixed;
    z-index: -1;
}

@keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

@-webkit-keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

#backgroundchange div:nth-of-type(1) {
  animation-delay: 8s;
  -webkit-animation-delay: 8s;
}
#backgroundchange div:nth-of-type(2) {
  animation-delay: 6s;
  -webkit-animation-delay: 6s;
}
#backgroundchange div:nth-of-type(3) {
  animation-delay: 4s;
  -webkit-animation-delay: 4s;
}
#backgroundchange div:nth-of-type(4) {
  animation-delay: 2s;
  -webkit-animation-delay: 2s;
}

#backgroundchange div:nth-of-type(5) {
  animation-delay: 0;
  -webkit-animation-delay: 0;
}

#backgroundchange div {
animation-name: backgroundchangeFadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 8s;

-webkit-animation-name: backgroundchangeFadeInOut;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 8s;

}
<div class="inner">
                <div id="backgroundchange">
                    <div class="backgroundimg" id="back1"></div>
                    <div class="backgroundimg" id="back2"></div>
                    <div class="backgroundimg" id="back3"></div>
                    <div class="backgroundimg" id="back4"></div>
                    <div class="backgroundimg" id="back5"></div>
                </div>
            </div>

每个图像交叉渐变到下一个图像需要2秒钟,我尝试通过在每个类型的第n个上添加一秒来将其更改为3秒但是不起作用。我需要你的帮助!

3 个答案:

答案 0 :(得分:3)

你需要以3的增量增加你的延迟,从0的最后一个孩子到最后一个增量的第一个孩子 - 下面我们从o到12

然后,您需要将动画长度更改为最长延迟

&#13;
&#13;
.backgroundimg {
    -webkit-background-size: cover;
  	-moz-background-size: cover;
  	-o-background-size: cover;
  	background-size: cover;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;

}

#back5 {
    background: url("http://duananhalotus.com/upload/album/0904234.jpg") no-repeat center fixed;
    z-index: -1;
}

#back4 {
    background: url("http://www.chinadaily.com.cn/world/images/attachement/jpg/site1/20120806/d4bed9d534551189e67329.jpg") no-repeat center fixed;
    z-index: -1;
}

#back3 {
    background: url("https://amazingslider.com/wp-content/uploads/2012/12/dandelion.jpg") no-repeat center fixed;
    z-index: -1;
}

#back2 {
    background: url("https://cdn.pixabay.com/photo/2013/04/06/11/50/image-editing-101040_960_720.jpg") no-repeat center fixed;
    z-index: -1;
}

#back1 {
    background: url("http://www.gettyimages.com/gi-resources/images/Embed/new/embed2.jpg") no-repeat center fixed;
    z-index: -1;
}

@keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

@-webkit-keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

#backgroundchange div:nth-of-type(1) {
  animation-delay: 12s;
  -webkit-animation-delay: 12s;
}
#backgroundchange div:nth-of-type(2) {
  animation-delay: 9s;
  -webkit-animation-delay: 9s;
}
#backgroundchange div:nth-of-type(3) {
  animation-delay: 6s;
  -webkit-animation-delay: 6s;
}
#backgroundchange div:nth-of-type(4) {
  animation-delay: 3s;
  -webkit-animation-delay: 3s;
}

#backgroundchange div:nth-of-type(5) {
  animation-delay: 0;
  -webkit-animation-delay: 0;
}

#backgroundchange div {
animation-name: backgroundchangeFadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 12s;

-webkit-animation-name: backgroundchangeFadeInOut;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 12s;

}
&#13;
<div class="inner">
    <div id="backgroundchange">
        <div class="backgroundimg" id="back1"></div>
        <div class="backgroundimg" id="back2"></div>
        <div class="backgroundimg" id="back3"></div>
        <div class="backgroundimg" id="back4"></div>
        <div class="backgroundimg" id="back5"></div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您还需要将动画持续时间编辑为12

.backgroundimg {
    -webkit-background-size: cover;
  	-moz-background-size: cover;
  	-o-background-size: cover;
  	background-size: cover;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;

}

#back5 {
    background: url("http://duananhalotus.com/upload/album/0904234.jpg") no-repeat center fixed;
    z-index: -1;
}

#back4 {
    background: url("http://www.chinadaily.com.cn/world/images/attachement/jpg/site1/20120806/d4bed9d534551189e67329.jpg") no-repeat center fixed;
    z-index: -1;
}

#back3 {
    background: url("https://amazingslider.com/wp-content/uploads/2012/12/dandelion.jpg") no-repeat center fixed;
    z-index: -1;
}

#back2 {
    background: url("https://cdn.pixabay.com/photo/2013/04/06/11/50/image-editing-101040_960_720.jpg") no-repeat center fixed;
    z-index: -1;
}

#back1 {
    background: url("http://www.gettyimages.com/gi-resources/images/Embed/new/embed2.jpg") no-repeat center fixed;
    z-index: -1;
}

@keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

@-webkit-keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

#backgroundchange div:nth-of-type(1) {
  animation-delay: 12s;
  -webkit-animation-delay: 12s;
}
#backgroundchange div:nth-of-type(2) {
  animation-delay: 9s;
  -webkit-animation-delay: 9s;
}
#backgroundchange div:nth-of-type(3) {
  animation-delay: 6s;
  -webkit-animation-delay: 6s;
}
#backgroundchange div:nth-of-type(4) {
  animation-delay: 3s;
  -webkit-animation-delay: 3s;
}

#backgroundchange div:nth-of-type(5) {
  animation-delay: 0;
  -webkit-animation-delay: 0;
}

#backgroundchange div {
animation-name: backgroundchangeFadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 12s;

-webkit-animation-name: backgroundchangeFadeInOut;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 12s;

}
<div class="inner">
                <div id="backgroundchange">
                    <div class="backgroundimg" id="back1"></div>
                    <div class="backgroundimg" id="back2"></div>
                    <div class="backgroundimg" id="back3"></div>
                    <div class="backgroundimg" id="back4"></div>
                    <div class="backgroundimg" id="back5"></div>
                </div>
            </div>

答案 2 :(得分:1)

这是3秒延迟,考虑更改webkit属性..

&#13;
&#13;
.backgroundimg {
    -webkit-background-size: cover;
  	-moz-background-size: cover;
  	-o-background-size: cover;
  	background-size: cover;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;

}

#back5 {
    background: url("http://duananhalotus.com/upload/album/0904234.jpg") no-repeat center fixed;
    z-index: -1;
}

#back4 {
    background: url("http://www.chinadaily.com.cn/world/images/attachement/jpg/site1/20120806/d4bed9d534551189e67329.jpg") no-repeat center fixed;
    z-index: -1;
}

#back3 {
    background: url("https://amazingslider.com/wp-content/uploads/2012/12/dandelion.jpg") no-repeat center fixed;
    z-index: -1;
}

#back2 {
    background: url("https://cdn.pixabay.com/photo/2013/04/06/11/50/image-editing-101040_960_720.jpg") no-repeat center fixed;
    z-index: -1;
}

#back1 {
    background: url("http://www.gettyimages.com/gi-resources/images/Embed/new/embed2.jpg") no-repeat center fixed;
    z-index: -1;
}

@keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

@-webkit-keyframes backgroundchangeFadeInOut {
  0% {
    opacity:1;
  }
  17% {
    opacity:1;
  }
  25% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

#backgroundchange div:nth-of-type(1) {
  animation-delay: 12s;
  -webkit-animation-delay: 12s;
}
#backgroundchange div:nth-of-type(2) {
  animation-delay: 9s;
  -webkit-animation-delay:9s;
}
#backgroundchange div:nth-of-type(3) {
  animation-delay: 6s;
  -webkit-animation-delay: 6s;
}
#backgroundchange div:nth-of-type(4) {
  animation-delay:3s;
  -webkit-animation-delay: 3s;
}

#backgroundchange div:nth-of-type(5) {
  animation-delay: 0;
  -webkit-animation-delay: 0;
}

#backgroundchange div {
animation-name: backgroundchangeFadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 12s;

-webkit-animation-name: backgroundchangeFadeInOut;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 12s;

}
&#13;
<div class="inner">
                <div id="backgroundchange">
                    <div class="backgroundimg" id="back1"></div>
                    <div class="backgroundimg" id="back2"></div>
                    <div class="backgroundimg" id="back3"></div>
                    <div class="backgroundimg" id="back4"></div>
                    <div class="backgroundimg" id="back5"></div>
                </div>
            </div>
&#13;
&#13;
&#13;