我一直在尝试创建一个基于CSS3动画的滑块,我对动画和关键帧背后的逻辑感到困惑。
这就是我所做的:http://jsfiddle.net/fatgamer85/LzGR7
我为滑块和滑块容器内的另一个容器创建了一个容器,用于存放div或图像。在这种情况下,我决定为启动器添加一些图像。
<div class="slider">
<div class="slide"><span class="image"></span></div>
<div class="slide"><span class="image"></span></div>
<div class="slide"><span class="image"></span></div>
<div class="slide"><span class="image"></span></div>
<div class="slide"><span class="image"></span></div>
</div>
然后使用CSS我绝对定位了这些容器和图像。
*, body, html{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.slider{
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.slide{
position: absolute;
width: 100%;
height: 100%;
}
.slide .image{
float: left;
position: absolute;
}
然后我继续将动画规则添加到.slide类
.slide
{
animation: myanimation 48s ease-in-out infinite;
-webkit-animation: myanimation 48s ease-in-out infinite;
-o-animation: myanimation 48s ease-in-out infinite;
-moz-animation: myanimation 48s ease-in-out infinite;
-ms-animation: myanimation 48s ease-in-out infinite;
}
使用nth-child伪类分别为每个类添加动画规则。
我观察到图像的显示顺序错误,因此我将z-index分别添加到这些类
中.slide:nth-child(1){
animation-delay: 0s;
-webkit-animation-delay: 0s;
-moz-animation-delay: 0s;
-o-animation-delay: 0s;
-ms-animation-delay: 0s;
z-index:1;
}
.slide:nth-child(2){
animation-delay: 8s;
-webkit-animation-delay: 8s;
-moz-animation-delay: 8s;
-o-animation-delay: 8s;
-ms-animation-delay: 8s;
z-index:2;
}
..... and so on...
并开始将图像添加到范围
.slide:nth-child(1) .image {
background-image: url('http://i.imgur.com/9yvKmZY.jpg');
background-repeat: no-repeat;
background-size: cover;
}
.slide:nth-child(2) .image {
background-image: url('http://i.imgur.com/j8mBdLD.jpg');
background-repeat: no-repeat;
background-size: cover;
}
..... and so on...
最后添加了关键帧规则
@-webkit-keyframes myanimation {
0%{
opacity: 1;
}
25% {
opacity: 0;
}
}
一切看起来都很精致但是......但是当Image开始制作动画时问题就出现了。 我还没有完全掌握动画的概念,我想......
因此,幻灯片以自己的思维为动力。有时它甚至不会显示图像的正确顺序,也不会完全跳过图像。
有谁能告诉我我做错了什么或我哪里出错了?
以下是滑块的全屏示例:http://fiddle.jshell.net/fatgamer85/LzGR7/23/show/light/
答案 0 :(得分:1)
如果您不了解幻灯片,有一件事会让您失去很多时间。
如果您将动画推迟到将来,第一个cicle与其他cicle不同。大多数元素都具有来自静态属性的属性,而不是来自动画。
为了避免这种问题,请将动画延迟设置为过去。这样,第一个动画周期就等于其他动画周期。
另外,给自己一些帮助很好。在这种情况下,我在幻灯片的左侧设置了数字。通过这种方式,您可以看到真正发生的事情。
此外,我已经修改了一些关键帧,但是在webkit中错误的是。
CSS
*, body, html{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.slider{
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.slide{
position: absolute;
width: 100%;
height: 100%;
animation: myanimation 48s ease-in-out infinite;
-webkit-animation: myanimation 48s ease-in-out infinite;
-o-animation: myanimation 48s ease-in-out infinite;
-moz-animation: myanimation 48s ease-in-out infinite;
-ms-animation: myanimation 48s ease-in-out infinite;
font-size: 100px;
color: red;
}
.slide .image{
float: left;
position: absolute;
}
.slide:nth-child(1){
animation-delay: 0s;
-webkit-animation-delay: 0s;
-moz-animation-delay: 0s;
-o-animation-delay: 0s;
-ms-animation-delay: 0s;
z-index:5;
}
.slide:nth-child(2){
animation-delay: -40s;
-webkit-animation-delay: -40s;
-moz-animation-delay: -40s;
-o-animation-delay: -40s;
-ms-animation-delay: -40s;
z-index:4;
}
.slide:nth-child(3){
animation-delay: -30s;
-webkit-animation-delay: -30s;
-moz-animation-delay: -30s;
-o-animation-delay: -30s;
-ms-animation-delay: -30s;
z-index:3;
}
.slide:nth-child(4){
animation-delay: -20s;
-webkit-animation-delay: -20s;
-moz-animation-delay: -20s;
-o-animation-delay: -20s;
-ms-animation-delay: -20s;
z-index:2;
}
.slide:nth-child(5){
animation-delay: -10s;
-webkit-animation-delay: -10s;
-moz-animation-delay: -10s;
-o-animation-delay: -10s;
-ms-animation-delay: -10s;
z-index:1;
}
@keyframes myanimation {
0%{ opacity: 0; }
5%{opacity: 1; }
20%{opacity: 1; }
25% {opacity: 0;}
100% {opacity: 0;}
}
@-webkit-keyframes myanimation {
0%{ opacity: 0; }
5%{opacity: 1; }
20%{opacity: 1; }
25% {opacity: 0;}
100% {opacity: 0;}
}
@-o-keyframes myanimation {
0%{ opacity: 1; }
25% { opacity: 0.75; }
50%{ opacity: 0.5; }
75% { opacity: 0.25; }
100%{ opacity: 0; }
}
@-moz-keyframes myanimation {
0%{ opacity: 1; }
25% { opacity: 0.75; }
50%{ opacity: 0.5; }
75% { opacity: 0.25; }
100%{ opacity: 0; }
}
@-ms-keyframes myanimation {
0%{ opacity: 0; }
5%{opacity: 1; }
20%{opacity: 1; }
25% {opacity: 0;}
100% {opacity: 0;}
}
@-webkit-keyframes myanimation {
0%{ opacity: 0; }
5%{opacity: 1; }
20%{opacity: 1; }
25% {opacity: 0;}
100% {opacity: 0;}
}
.slide:nth-child(1) .image {
background-image: url('http://i.imgur.com/9yvKmZY.jpg');
background-repeat: no-repeat;
background-size: cover;
}
.slide:nth-child(2) .image {
background-image: url('http://i.imgur.com/j8mBdLD.jpg');
background-repeat: no-repeat;
background-size: cover;
}
.slide:nth-child(3) .image {
background-image: url('http://i.imgur.com/9VdDjQi.jpg');
background-repeat: no-repeat;
background-size: cover;
}
.slide:nth-child(4) .image {
background-image: url('http://i.imgur.com/dqCWOgW.jpg');
background-repeat: no-repeat;
background-size: cover;
}
.slide:nth-child(5) .image {
background-repeat: no-repeat;
background-image: url('http://i.imgur.com/0hUMMuT.jpg');
background-size: cover;
}
快乐的编码!
在上面的演示中,第5张图像在动画开始时会显示一点时间。这可以通过调整关键帧来纠正:
@keyframes myanimation {
0%{opacity: 1; }
15%{opacity: 1; }
20%{opacity: 0; }
95% {opacity: 0;}
100% {opacity: 1;}
}
如果你用一张纸画出来,你会明白为什么;但很难解释
关于你的小提琴,你犯2个错误。问题在于你认为条形图在某种程度上“在框架内部”,并且与“时间”相关联。这不是真的,你正在使用一个40秒的循环,并且该栏必须使用该动画时间(不是9秒,帧数在这9秒的分数中)。
你最好的选择是只有1巴,并让它骑得更快(10秒)。
CSS
*, body, html{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.slider{
position: relative;
overflow: hidden;
width: 100%;
height: 400px;
}
.slide{
position: absolute;
width: 100%;
height: 100%;
animation: bg_anim 50s ease-in-out infinite;
-webkit-animation: bg_anim 50s ease-in-out infinite;
-o-animation: bg_anim 50s ease-in-out infinite;
-moz-animation: bg_anim 50s ease-in-out infinite;
-ms-animation: bg_anim 50s ease-in-out infinite;
}
.slide .image{
float: left;
position: absolute;
}
.slide:nth-child(1){
animation-delay: 0s;
-webkit-animation-delay: 0s;
-moz-animation-delay: 0s;
-o-animation-delay: 0s;
-ms-animation-delay: 0s;
z-index:5;
}
.slide:nth-child(2){
animation-delay: -40s;
-webkit-animation-delay: -40s;
-moz-animation-delay: -40s;
-o-animation-delay: -40s;
-ms-animation-delay: -40s;
z-index:4;
}
.slide:nth-child(3){
animation-delay: -30s;
-webkit-animation-delay: -30s;
-moz-animation-delay: -30s;
-o-animation-delay: -30s;
-ms-animation-delay: -30s;
z-index:3;
}
.slide:nth-child(4){
animation-delay: -20s;
-webkit-animation-delay: -20s;
-moz-animation-delay: -20s;
-o-animation-delay: -20s;
-ms-animation-delay: -20s;
z-index:2;
}
.slide:nth-child(5){
animation-delay: -10s;
-webkit-animation-delay: -10s;
-moz-animation-delay: -10s;
-o-animation-delay: -10s;
-ms-animation-delay: -10s;
z-index:1;
}
.text{
right: 0px;
color: #fff;
font-size: 28px;
float: right;
z-index:1;
}
.text:nth-child(1){
z-index:5;
}
.text:nth-child(2){
z-index:4;
}
.text:nth-child(3){
z-index:3;
}
.text:nth-child(4){
z-index:2;
}
.text:nth-child(5){
z-index:1;
}
.bar{
position: absolute;
width: 45%;
height: 400px;
right: -20px;
float: right;
z-index: 99;
animation: bar_anim 10s infinite ease-in-out;
-webkit-animation: bar_anim 10s infinite ease-in-out;
-o-animation: bar_anim 10s infinite ease-in-out;
-moz-animation: bar_anim 10s infinite ease-in-out;
-ms-animation: bar_anim 10s infinite ease-in-out;
animation-delay: -1.5s;
-webkit-animation-delay: -1.5s;
-moz-animation-delay: -1.5s;
-o-animation-delay: -1.5s;
-ms-animation-delay: -1.5s;
}
.slide:nth-child(1) > .bar{
animation-delay: 0s;
-webkit-animation-delay: 0s;
-moz-animation-delay: 0s;
-o-animation-delay: 0s;
-ms-animation-delay: 0s;
}
.slide:nth-child(2) > .bar{
animation-delay: -7.2s;
-webkit-animation-delay: -7.2s;
-moz-animation-delay: -7.2s;
-o-animation-delay: -7.2s;
-ms-animation-delay: -7.2s;
z-index:4;
}
.slide:nth-child(3) > .bar{
animation-delay: -5.4s;
-webkit-animation-delay: -5.4s;
-moz-animation-delay: -5.4s;
-o-animation-delay: -5.4s;
-ms-animation-delay: -5.4s;
z-index:3;
}
.slide:nth-child(4) > .bar{
animation-delay: -3.6s;
-webkit-animation-delay: -3.6s;
-moz-animation-delay: -3.6s;
-o-animation-delay: -3.6s;
-ms-animation-delay: -3.6s;
z-index:2;
}
.slide:nth-child(5) > .bar{
animation-delay: -1.8s;
-webkit-animation-delay: -1.8s;
-moz-animation-delay: -1.8s;
-o-animation-delay: -1.8s;
-ms-animation-delay: -1.8s;
z-index:1;
}
@keyframes bar_anim {
0% { right: -4000px; }
10% { right: 0px; }
15% { right: -20px;}
80% { right: -20px;}
95% { right: 0px; }
100% { right: -4000px; }
}
@-o-keyframes bar_anim {
0%{ right: -4000px; }
10%{ right: 0px; }
15%{ right: -20px; }
80% { right: -20px; }
95% { right: 0px; }
100% { right: -4000px; }
}
@-moz-keyframes bar_anim {
0%{ right: -4000px; }
10%{ right: 0px; }
15%{ right: -20px; }
80% { right: -20px; }
95% { right: 0px; }
100% { right: -4000px; }
}
@-ms-keyframes bar_anim {
0%{ right: -4000px; }
10%{ right: 0px; }
15%{ right: -20px; }
80% { right: -20px; }
95% { right: 0px; }
100% { right: -4000px; }
}
@-webkit-keyframes bar_anim {
0%{ right: -4000px; }
10%{ right: 0px; }
15%{ right: -20px; }
80% { right: -20px; }
95% { right: 0px; }
100% { right: -4000px; }
}
@keyframes bg_anim {
0%{ opacity: 1; }
15%{
opacity: 1;
}
20%{
opacity: 0;
}
95% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-webkit-keyframes bg_anim {
0%{
opacity: 1;
}
15%{
opacity: 1;
}
20%{
opacity: 0;
}
95% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-o-keyframes bg_anim {
0%{
opacity: 1;
}
15%{
opacity: 1;
}
20%{
opacity: 0;
}
95% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-moz-keyframes bg_anim {
0%{
opacity: 1;
}
15%{
opacity: 1;
}
20%{
opacity: 0;
}
95% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-ms-keyframes bg_anim {
0%{
opacity: 1;
}
15%{
opacity: 1;
}
20%{
opacity: 0;
}
95% {
opacity: 0;
}
100% {
opacity: 1;
}
}
或者,如果需要,为每张幻灯片设置1个跨度,但随后保持动画时间和延迟。
答案 1 :(得分:0)
根据您的代码,您已正确掌握动画(关键帧)。我认为问题在于z-index,你应该删除它。你错过了关于幻灯片nth-child的一个小细节。您必须将nth-childs不透明度设置为0.我会将理解留给您:)
.slide:nth-child(3){
animation-delay: 16s;
-webkit-animation-delay: 16s;
-moz-animation-delay: 16s;
-o-animation-delay: 16s;
-ms-animation-delay: 16s;
opacity:0;
}