【问题标题】:CSS fading animation timing offCSS淡入淡出动画计时关闭
【发布时间】:2012-04-16 23:38:51
【问题描述】:

所以我正在尝试淡化 3 张图像,其中一张慢慢叠放在一起,这样您就可以在几秒钟内看到每张图像,然后再下一张淡出顶部。我正在关注this tutorial here. 但是由于某种原因,我的图像似乎彼此褪色太快,我无法很好地控制时间。

Here is my preview page. 假设您会看到一只鸟,然后是西红柿,然后是一艘船,但现在它们只是相互重叠并最终落在船上。

我正在关注 Demo 1 和 Demo 3,你知道为什么我的动画关闭了吗?提前致谢! :)

我的 jsfiddle: http://jsfiddle.net/leongaban/TPjWG/

代码:

<style>

@-webkit-keyframes cf4FadeInOut {
 0% {
   opacity:0;
 }
 25% {
   opacity:1;
 }  
 50% {
   opacity:1;
 }
 100% {
   opacity:1;
 }
}

@-moz-keyframes cf4FadeInOut {
 0% {
   opacity:0;
 }
 25% {
   opacity:1;
 }  
 50% {
   opacity:1;
 }
 100% {
   opacity:1;
 }
}

@-ms-keyframes cf4FadeInOut {
 0% {
   opacity:0;
 }
 25% {
   opacity:1;
 }  
 50% {
   opacity:1;
 }
 100% {
   opacity:1;
 }
}       

#cf4a {
    position:relative;
    height:250px;
    width:300px;
    margin:0 auto;
}
#cf4a img {
    position:absolute;
    left:0;
}

#cf4a img {
    -webkit-animation-name: cf4FadeInOut;
    -webkit-animation-timing-function: ease-in-out;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-duration: 30s;

    -moz-animation-name: cf4FadeInOut;
    -moz-animation-timing-function: ease-in-out;
    -moz-animation-iteration-count: 1;
    -moz-animation-duration: 30s;

    -ms-animation-name: cf4FadeInOut;
    -ms-animation-timing-function: ease-in-out;
    -ms-animation-iteration-count: 1;
    -ms-animation-duration: 30s;                
}
#cf4a img:nth-of-type(1) {
    -webkit-animation-delay: 0;     
    -moz-animation-delay: 0;    
    -ms-animation-delay: 0; 
}
#cf4a img:nth-of-type(2) {
    -webkit-animation-delay: 30s;       
    -moz-animation-delay: 30s;  
    -ms-animation-delay: 30s;
}
#cf4a img:nth-of-type(3) {
    -webkit-animation-delay: 60s;       
    -moz-animation-delay: 60s;      
    -ms-animation-delay: 60s;       
}

</style>

<div id="cf4a">

    <a href="http://http://stackoverflow.com/" target="_blank"><img src="img/bird1.jpg" /></a>
    <a href="http://http://stackoverflow.com/" target="_blank"><img src="img/tomato2.jpg" /></a>
    <a href="http://http://stackoverflow.com/" target="_blank"><img src="img/boat3.jpg" /></a>

</div>

【问题讨论】:

    标签: html css animation css-animations


    【解决方案1】:

    我相信您的部分问题是 z-index 在动画期间排序以及动画时间没有重叠(相对于 z-index)。

    使用仅淡出技术(因为您似乎正在对图像进行一次性动画)。我有一个在 Firefox 和 Chrome 中测试的a working demo 解决方案(IE9 不支持动画,所以你必须在最初使用-ms- 扩展时为 IE10 做准备)。由于我只淡出一次,因此最终的 a 标签上不需要动画,因为它最后显示并保留(并且是非 CSS3 动画浏览器的默认设置)。

    演示使用以下 CSS 代码(与原始 HTML 相同):

    CSS

    @-webkit-keyframes cf4FadeOut1 {
     0% {
       opacity:1;
       z-index: 100;
     }  
     80% { /* 6 sec trans on 30s animation */
       opacity:1;
       z-index: 100;
     }
     100% {
       opacity:0;
       z-index: 100;
     }
    }
    
    @-moz-keyframes cf4FadeOut1 {
     0% {
       opacity:1;
       z-index: 100;
     }  
     80% { /* 6 sec trans on 30s animation */
       opacity:1;
       z-index: 100;
     }
     100% {
       opacity:0;
       z-index: 100;
     }
    }
    
    @-ms-keyframes cf4FadeOut1 {
     0% {
       opacity:1;
       z-index: 100;
     }  
     80% { /* 6 sec trans on 30s animation */
       opacity:1;
       z-index: 100;
     }
     100% {
       opacity:0;
       z-index: 100;
     }
    }
    
    @-webkit-keyframes cf4FadeOut2 {
     0% {
       opacity:1;
       z-index: 2;
     }   
     90% { /* 6 sec trans on 60s animation */
       opacity:1;
       z-index: 2;
     }
     100% {
       opacity:0;
       z-index: 2;
     }
    }
    
    @-moz-keyframes cf4FadeOut2 {
     0% {
       opacity:1;
       z-index: 2;
     }   
     90% { /* 6 sec trans on 60s animation */
       opacity:1;
       z-index: 2;
     }
     100% {
       opacity:0;
       z-index: 2;
     }
    }
    
    @-ms-keyframes cf4FadeOut2 {
     0% {
       opacity:1;
       z-index: 2;
     }   
     90% { /* 6 sec trans on 60s animation */
       opacity:1;
       z-index: 2;
     }
     100% {
       opacity:0;
       z-index: 2;
     }
    }
    
    #cf4a {
        position:relative;
        height:250px;
        width:300px;
        margin:0 auto;
    }
    #cf4a a {
        position:absolute;
        left:0;
        z-index: 0;
    }
    
    #cf4a a {
        -webkit-animation-timing-function: ease-in-out;
        -webkit-animation-iteration-count: 1;
        -webkit-animation-delay: 0;
    
        -moz-animation-timing-function: ease-in-out;
        -moz-animation-iteration-count: 1;
        -moz-animation-delay: 0;
    
        -ms-animation-timing-function: ease-in-out;
        -ms-animation-iteration-count: 1;
        -ms-animation-delay: 0;   
    }
    
    #cf4a a:nth-of-type(1) {
         -webkit-animation-name: cf4FadeOut1;
         -moz-animation-name: cf4FadeOut1;
         -ms-animation-name: cf4FadeOut1;
    
         -webkit-animation-duration: 30s;
         -moz-animation-duration: 30s;
         -ms-animation-duration: 30s;           
    }
    
    #cf4a a:nth-of-type(2) {
         -webkit-animation-name: cf4FadeOut2;
         -moz-animation-name: cf4FadeOut2;
         -ms-animation-name: cf4FadeOut2;
    
         -webkit-animation-duration: 60s;
         -moz-animation-duration: 60s;
         -ms-animation-duration: 60s;    
    }
    

    【讨论】:

    • 非常感谢!在我的脑海里,我知道这是可以做到的:D
    【解决方案2】:

    在您的小提琴和演示页面中,您将图像包裹在&lt;a&gt; 标签中。这会导致错误出现在您使用 :nth-of-type() 的过程中。

    您的所有图像都受到 :nth-of-type(1) 的影响 声明,因为它们都是 &lt;a&gt; 标签中的“第一个孩子”元素。

    【讨论】:

    • 啊,是的,感谢您指出这一点 :) 已更正,但仍然存在动画关键帧问题 :( 我想我需要了解如何在完成后开始新动画。
    • 是的,需要对关键帧/延迟时间进行一些调整/实验。即使在演示(“多张图片演示”部分)中,作者也表示“它不是很好”:)
    • 是的,我会继续尝试调整关键帧,可能会提供积分或将其标记为不可修复:(
    【解决方案3】:

    我做了一些改动,让它比以前工作得稍微好一些。但是,我仍然无法解释为什么当图像从最后一个变为第一个时动画无法正常工作。

    <head>
        <title> CSS alpha animation test </title>
        <meta charset="utf-8" />
    
    
    </head>
    
    <body>
    
        <style>
    
        @-webkit-keyframes cf4FadeInOut {
         0% {
           opacity:0;
         }
         45% {
           opacity:0;
         }    
         55% {
           opacity:1;
         }
         100% {
           opacity:1;
         }
        }       
    
        #cf4a {
            position:relative;
            height:250px;
            width:300px;
            margin:0 auto;
        }
        #cf4a img {
            position:absolute;
            left:0;
        }
    
        #cf4a img {
            -webkit-animation-name: cf4FadeInOut;
            -webkit-animation-timing-function: ease-in-out;
            -webkit-animation-iteration-count: infinite;
            -webkit-animation-duration: 30s;     
        }
        #cf4a img:nth-of-type(1) {
             -webkit-animation-delay: 0s;   
        }
        #cf4a img:nth-of-type(2) {
             -webkit-animation-delay: 10s;
        }
        #cf4a img:nth-of-type(3) {
             -webkit-animation-delay: 20s;       
        }
    
        </style>
    
        <div id="cf4a">
    
            <img src="http://leongaban.com/_stack/css_animation/img/bird1.jpg" />
            <img src="http://leongaban.com/_stack/css_animation/img/tomato2.jpg" />
            <img src="http://leongaban.com/_stack/css_animation/img/boat3.jpg" />
    
        </div>
    
    </body>
    
    </html>​​​​​​​​​​​​​​​
    

    请注意,我已经删除了针对不同浏览器的所有其他代码,只留下了 webkit 代码,因为我是在 chrome 上测试的。我还按照他们的示例更改了完成百分比,这有点不同。

    【讨论】:

    • 当我使用 45% {opacity:0;} 时,动画保持白色的时间更长
    • 抱歉耽搁了。在阅读了更多内容之后,关键帧意味着图像的前 45% 是透明的,然后其余部分是不透明的。我对前 45% 的理解意味着在显示图像的 10 秒中 45% 是透明的。所以4.5秒左右。但它仍然没有解释跳转以及为什么第一张图片没有正确显示。
    • @Leon 和 sarcastyx--“跳跃”和其他奇怪的显示问题是由于在动画点期间 z-index 定位图像的不正确造成的(你们俩都没有注意到)在你的动画中考虑)。我已经发布了解决这些问题的解决方案。
    • @ScottS,谢谢。应该已经发现了。奇怪的是,在示例中他们从未提到没有 z-index 图像会跳转。
    【解决方案4】:

    你为什么不尝试使用 jquery。

    function slideSwitch() {
    var $active = $('#slideshow IMG.active');
    
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
    //var $sibs  = $active.siblings();
    //var rndNum = Math.floor(Math.random() * $sibs.length );
    //var $next  = $( $sibs[ rndNum ] );
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
    }
    
    $(function() {
    setInterval( "slideSwitch()", 5000 );
    }); 
    

    【讨论】:

    • 哦,我仍然是 jquery 菜鸟,但谢谢,我稍后会试试这个:)
    猜你喜欢
    • 2019-02-11
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2019-01-01
    • 2014-01-20
    • 2012-12-18
    • 2013-11-27
    • 2015-09-08
    相关资源
    最近更新 更多