如何循环这个 css 滑块动画?

How to loop this css slider animation?

我正在尝试创建一个 css 滑块,到目前为止,我只成功地让它正确迭代了一次。有没有办法让它在不改变最终结果的情况下无限循环?

<div class="slider">
<div class="slides">
<div class="slider-1"></div>
<div class="slider-2"></div>
<div class="slider-3"></div>
<div class="slider-4"></div>
</div>
</div>

Css

.slider{
  width:700px;
  height:300px;
  margin:50px auto;
  border:1px solid;
  overflow:hidden;
}
.slides{
  width:400%;
  height:100%;
  -webkit-animation:slide-1 2s linear 4s 1 forwards, slide-2 2s linear 8s 1 forwards, slide-3 2s linear 12s 1 forwards, slide-4 2s linear 16s 1 forwards;
  -moz-animation:slide-1 2s linear 4s 1 forwards, slide-2 2s linear 8s 1 forwards, slide-3 2s linear 12s 1 forwards, slide-4 2s linear 16s 1 forwards;
  animation:slide-1 2s linear 4s 1 forwards, slide-2 2s linear 8s 1 forwards, slide-3 2s linear 12s 1 forwards, slide-4 2s linear 16s 1 forwards;
}
.slider-1, .slider-2, .slider-3, .slider-4{
  width:25%;
  height:100%;
  float:left;
}
.slider-1{
  background:#222;
}
.slider-2{
  background:#444;
}
.slider-3{
  background:#666;
}
.slider-4{
  background:#888;
}

@-webkit-keyframes slide-1{
  from{margin-left:0px;}
  to{margin-left:-100%;}
}
@-webkit-keyframes slide-2{
  from{margin-left:-100%;}
  to{margin-left:-200%;}
}
@-webkit-keyframes slide-3{
  from{margin-left:-200%;}
  to{margin-left:-300%;}
}
@-webkit-keyframes slide-4{
  from{margin-left:-300%;}
  to{margin-left:0%;}
}
@-moz-keyframes slide-1{
  from{margin-left:0px;}
  to{margin-left:-100%;}
}
@-moz-keyframes slide-2{
  from{margin-left:-100%;}
  to{margin-left:-200%;}
}
@-moz-keyframes slide-3{
  from{margin-left:-200%;}
  to{margin-left:-300%;}
}
@-moz-keyframes slide-4{
  from{margin-left:-300%;}
  to{margin-left:0%;}
}
@keyframes slide-1{
  from{margin-left:0px;}
  to{margin-left:-100%;}
}
@keyframes slide-2{
  from{margin-left:-100%;}
  to{margin-left:-200%;}
}
@keyframes slide-3{
  from{margin-left:-200%;}
  to{margin-left:-300%;}
}
@keyframes slide-4{
  from{margin-left:-300%;}
  to{margin-left:0%;}
}

http://jsfiddle.net/1kcbpqfu/

在我看来,这是最好的解决方案之一。

/* ANIMATION */
@keyframes cycle {
 0%  { top:0px; }
 4%  { top:0px; } 
 16% { top:0px; opacity:1; z-index:0; } 
 20% { top:325px; opacity:0; z-index:0; } 
 21% { top:-325px; opacity:0; z-index:-1; }
 92% { top:-325px; opacity:0; z-index:0; }
 96% { top:-325px; opacity:0; }
 100%{ top:0px; opacity:1; }
 
}
@keyframes cycletwo {
 0%  { top:-325px; opacity:0; }
 16% { top:-325px; opacity:0; }
 20% { top:0px; opacity:1; }
 24% { top:0px; opacity:1; } 
 36% { top:0px; opacity:1; z-index:0; } 
 40% { top:325px; opacity:0; z-index:0; }
 41% { top:-325px; opacity:0; z-index:-1; } 
 100%{ top:-325px; opacity:0; z-index:-1; }
}
@keyframes cyclethree {
 0%  { top:-325px; opacity:0; }
 36% { top:-325px; opacity:0; }
 40% { top:0px; opacity:1; }
 44% { top:0px; opacity:1; } 
 56% { top:0px; opacity:1; } 
 60% { top:325px; opacity:0; z-index:0; }
 61% { top:-325px; opacity:0; z-index:-1; } 
 100%{ top:-325px; opacity:0; z-index:-1; }
}
@keyframes cyclefour {
 0%  { top:-325px; opacity:0; }
 56% { top:-325px; opacity:0; }
 60% { top:0px; opacity:1; }
 64% { top:0px; opacity:1; }
 76% { top:0px; opacity:1; z-index:0; }
 80% { top:325px; opacity:0; z-index:0; }
 81% { top:-325px; opacity:0; z-index:-1; }
 100%{ top:-325px; opacity:0; z-index:-1; }
}
@keyframes cyclefive {
 0%  { top:-325px; opacity:0; }
 76% { top:-325px; opacity:0; }
 80% { top:0px; opacity:1; }
 84% { top:0px; opacity:1; }
 96% { top:0px; opacity:1; z-index:0; }
 100%{ top:325px; opacity:0; z-index:0; }
}

/* ANIMATION BAR */
@keyframes fullexpand {
    0%, 20%, 40%, 60%, 80%, 100% { width:0%; opacity:0; }
    4%, 24%, 44%, 64%, 84% { width:0%; opacity:0.3; }
   16%, 36%, 56%, 76%, 96% { width:100%; opacity:0.7; }
   17%, 37%, 57%, 77%, 97% { width:100%; opacity:0.3; }
   18%, 38%, 58%, 78%, 98% { width:100%; opacity:0; } 
}

/* Common */
html, body {
 background:#eaeaea url(../img/bg.png) repeat;
 font-size:12px;
 font-family:"Open Sans", serif;
 min-width:960px;
 margin:0;
 padding:0;
 color:#aaa;
}

.content h1 {
 font-size:48px;
 color:#000;
 text-shadow:0px 1px 1px #f4f4f4;
 text-align:center;
 padding:60px 0 30px; 
}

/* LAYOUT */
.container {
 margin:0 auto;
 overflow:hidden;
 width:960px;
}

/* CONTENT SLIDER */
#content-slider {
 width:100%;
 height:360px;
 margin:10px auto 0;
}
/* SLIDER */
#slider {
 background:#000;
 border:5px solid #eaeaea;
 box-shadow:1px 1px 5px rgba(0,0,0,0.7);
 height:320px;
 width:680px;
 margin:40px auto 0;
 overflow:visible;
 position:relative;
}
#mask {
 overflow:hidden;
 height:320px;
}
#slider ul {
 margin:0;
 padding:0;
 position:relative;
}
#slider li {
 width:680px;
 height:320px;
 position:absolute;
 top:-325px;
 list-style:none;
}

#slider li.firstanimation {
 -moz-animation:cycle 25s linear infinite; 
 -webkit-animation:cycle 25s linear infinite;  
}
#slider li.secondanimation {
 -moz-animation:cycletwo 25s linear infinite;
 -webkit-animation:cycletwo 25s linear infinite;  
}
#slider li.thirdanimation {
 -moz-animation:cyclethree 25s linear infinite;
 -webkit-animation:cyclethree 25s linear infinite;  
}
#slider li.fourthanimation {
 -moz-animation:cyclefour 25s linear infinite;
 -webkit-animation:cyclefour 25s linear infinite;  
}
#slider li.fifthanimation {
 -moz-animation:cyclefive 25s linear infinite;
 -webkit-animation:cyclefive 25s linear infinite;  
}

#slider .tooltip {
 background:rgba(0,0,0,0.7);
 width:300px;
 height:60px;
 position:relative;
 bottom:75px;
 left:-320px;
 -moz-transition:all 0.3s ease-in-out;
 -webkit-transition:all 0.3s ease-in-out;  
}
#slider .tooltip h1 {
 color:#fff;
 font-size:24px;
 font-weight:300;
 line-height:60px;
 padding:0 0 0 20px;
}
#slider li#first:hover .tooltip, 
#slider li#second:hover .tooltip, 
#slider li#third:hover .tooltip, 
#slider li#fourth:hover .tooltip, 
#slider li#fifth:hover .tooltip {
 left:0px;
}
#slider:hover li, 
#slider:hover .progress-bar {
 -moz-animation-play-state:paused;
 -webkit-animation-play-state:paused;
}

/* PROGRESS BAR */
.progress-bar { 
 position:relative;
 top:-5px;
 width:680px; 
 height:5px;
 background:#000;
 -moz-animation:fullexpand 25s ease-out infinite;
 -webkit-animation:fullexpand 25s ease-out infinite;
}
<div class="content">
 <h1>Pure CSS3 Cycle Slider</h1>
</div>

<div class="container">
 <div id="content-slider">
     <div id="slider">
         <div id="mask">
            <ul>
            <li id="first" class="firstanimation">
            <a href="#">
            <img src="http://www.alessioatzeni.com/CSS3-Cycle-Image-Slider/images/img_1.jpg" alt="Cougar"/>
            </a>
            <div class="tooltip">
            <h1>Cougar</h1>
            </div>
            </li>

            <li id="second" class="secondanimation">
            <a href="#">
            <img src="http://www.alessioatzeni.com/CSS3-Cycle-Image-Slider/images/img_2.jpg" alt="Lions"/>
            </a>
            <div class="tooltip">
            <h1>Lions</h1>
            </div>
            </li>
            
            <li id="third" class="thirdanimation">
            <a href="#">
            <img src="http://www.alessioatzeni.com/CSS3-Cycle-Image-Slider/images/img_3.jpg" alt="Snowalker"/>
            </a>
            <div class="tooltip">
            <h1>Snowalker</h1>
            </div>
            </li>
                        
            <li id="fourth" class="fourthanimation">
            <a href="#">
            <img src="http://www.alessioatzeni.com/CSS3-Cycle-Image-Slider/images/img_4.jpg" alt="Howling"/>
            </a>
            <div class="tooltip">
            <h1>Howling</h1>
            </div>
            </li>
                        
            <li id="fifth" class="fifthanimation">
            <a href="#">
            <img src="http://www.alessioatzeni.com/CSS3-Cycle-Image-Slider/images/img_5.jpg" alt="Sunbathing"/>
            </a>
            <div class="tooltip">
            <h1>Sunbathing</h1>
            </div>
            </li>
            </ul>
            </div>
            <div class="progress-bar"></div>
        </div>
    </div>
</div>

据我所知,你不能嵌套超过一个动画并添加动画迭代计数属性。它将单独重复每个动画。

但是,如果你只做一个动画,你可以设置它无限重复,像这样:

.slider{
    width:700px;
    height:300px;
     margin:50px auto;
    border:1px solid;
    overflow:hidden;
}
.slides{
    width:400%;
     height:100%;
     -webkit-animation:slide 16s infinite;
     -moz-animation:slide 16s infinite;
     animation:slide 16s infinite;
}
.slider-1, .slider-2, .slider-3, .slider-4{
    width:25%;
    height:100%;
    float:left;
}
.slider-1{
    background:#222;
}
.slider-2{
    background:#444;
}
.slider-3{
    background:#666;
}
.slider-4{
    background:#888;
}

@-webkit-keyframes slide{
    0%,100% {
        margin-left:0%;
    }

    12% {
        margin-left:0%;
    }

    24% {
        margin-left:-100%;
    }

    36% {
        margin-left:-100%;
    }

    48% {
        margin-left:-200%;
    }

    60% {
        margin-left:-200%;
    }

    72% {
        margin-left:-300%;
    }

    84% {
        margin-left:-300%;  
    }
}
@-moz-keyframes slide{
    0%,100% {
        margin-left:0%;
    }

    12% {
        margin-left:0%;
    }

    24% {
        margin-left:-100%;
    }

    36% {
        margin-left:-100%;
    }

    48% {
        margin-left:-200%;
    }

    60% {
        margin-left:-200%;
    }

    72% {
        margin-left:-300%;
    }

    84% {
        margin-left:-300%;  
    }
}
@keyframes slide{
    0%,100% {
        margin-left:0%;
    }

    12% {
        margin-left:0%;
    }

    24% {
        margin-left:-100%;
    }

    36% {
        margin-left:-100%;
    }

    48% {
        margin-left:-200%;
    }

    60% {
        margin-left:-200%;
    }

    72% {
        margin-left:-300%;
    }

    84% {
        margin-left:-300%;  
    }
}
<div class="slider">
    <div class="slides">
        <div class="slider-1"></div>
        <div class="slider-2"></div>
        <div class="slider-3"></div>
        <div class="slider-4"></div>
    </div>
</div>

这是一个 JSFiddle:http://jsfiddle.net/1kcbpqfu/2/

编辑:百分比由

计算
Time_of_property_change/Time_of_whole_animation

这意味着,如果我想要一个 10 秒的动画,帧之间的过渡在 3 秒内持续 2 秒,并在 8 秒内回到拳头位置,CSS 动画帧应该如下所示:

0%, 100% { /* Beggining and end of animation */
    margin-left: 0%;
}

30% { /* Beggining of transition
    margin-left: 0%; /* Until 3 seconds, keep same frame */
}

50% { /* End of transition */
    margin-left: -100%; /* in 5 seconds, finish animation

80% { /* In 8 seconds, start transition to first frame */
    margin-left: -100%;
}