CSS3过渡 - 淡出效果

时间:2013-04-09 16:10:52

标签: css css3 css-transitions

我正在尝试在纯CSS中实现“淡出”效果。这是fiddle。我确实在线查看了几个解决方案,但是,在阅读documentation online之后,我试图弄清楚为什么幻灯片动画不起作用。有什么指针吗?

这是HTML

<div class="dummy-wrap">
    <div class="success-wrap successfully-saved">Saved</div>
</div>

CSS

.dummy-wrap {
    animation: slideup 2s;
    -moz-animation: slideup 2s;
    -webkit-animation: slideup 2s;
    -o-animation: slideup 2s;
}
.success-wrap {
    width: 75px;
    min-height: 20px;
    clear: both;
    margin-top: 10px;
}
.successfully-saved {
    color: #FFFFFF;
    font-size: 20px;
    padding: 15px 40px;
    margin-bottom: 20px;
    text-align: center;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    background-color: #00b953;
}
@keyframes slideup {
    0% {
        top:0px;
    }
    75% {
        top:0px;
    }
    100% {
        top:-20px;
    }
}
@-moz-keyframes slideup {
    0% {
        top:0px;
    }
    75% {
        top:0px;
    }
    100% {
        top:-20px;
    }
}
@-webkit-keyframes slideup {
    0% {
        top:0px;
    }
    75% {
        top:0px;
    }
    100% {
        top:-20px;
    }
}
@-o-keyframes slideup {
    0% {
        top:0px;
    }
    75% {
        top:0px;
    }
    100% {
        top:-20px;
    }
}

6 个答案:

答案 0 :(得分:126)

这是另一种做法。

淡出效果

.visible {
  visibility: visible;
  opacity: 1;
  transition: opacity 2s linear;
}

fadeOut效果

.hidden {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 2s, opacity 2s linear;
}

UPDATE 1: 我在此处找到了更多最新教程CSS3 Transition: fadeIn and fadeOut like effects to hide show elementsTooltip Example: Show Hide Hint or Help Text using CSS3 Transition以及示例代码。

UPDATE 2:(添加了@ big-money要求的详细信息)

当显示元素时(通过切换到可见类),我们希望可见性:可见立即启动,因此可以仅转换不透明度属性。当隐藏元素时(通过切换到隐藏类),我们想要延迟visibility:hidden声明,这样我们就可以先看到淡出过渡。我们通过声明可见性属性的转换来做到这一点,持续时间为0,延迟为0。您可以看到详细的文章here.

我知道我来不及回答但发布这个答案以节省其他时间。希望它可以帮助你!!

答案 1 :(得分:71)

您可以改为使用转换:

.successfully-saved.hide-opacity{
    opacity: 0;
}

.successfully-saved {
    color: #FFFFFF;
    text-align: center;

    -webkit-transition: opacity 3s ease-in-out;
    -moz-transition: opacity 3s ease-in-out;
    -ms-transition: opacity 3s ease-in-out;
    -o-transition: opacity 3s ease-in-out;
     opacity: 1;
}

答案 2 :(得分:5)

由于 display 不是可设置动画的CSS属性之一; 一个 display:none 淡出动画替换为纯css3动画,只需在最后一帧设置 width:0和height:0 ,然后使用 animation-fill-mode:前进以保持width:0和height:0属性;

@-webkit-keyframes fadeOut {
    0% { opacity: 1;}
    99% { opacity: 0.01;width: 100%; height: 100%;}
    100% { opacity: 0;width: 0; height: 0;}
}  
@keyframes fadeOut {
    0% { opacity: 1;}
    99% { opacity: 0.01;width: 100%; height: 100%;}
    100% { opacity: 0;width: 0; height: 0;}
}

.display-none.on{
    display: block;
    -webkit-animation: fadeOut 1s;
    animation: fadeOut 1s;
    animation-fill-mode: forwards;
}

答案 3 :(得分:3)

您忘记向.dummy-wrap类添加位置属性,并且顶部/左侧/底部/右侧值不适用于静态定位元素(默认值)

http://jsfiddle.net/dYBD2/2/

答案 4 :(得分:3)

.fadeOut{
    background-color: rgba(255, 0, 0, 0.83);
    border-radius: 8px;
    box-shadow: silver 3px 3px 5px 0px;
    border: 2px dashed yellow;
    padding: 3px;
}
.fadeOut.end{
    transition: all 1s ease-in-out;
    background-color: rgba(255, 0, 0, 0.0);
    box-shadow: none;
    border: 0px dashed yellow;
    border-radius: 0px;
}

demo here.

答案 5 :(得分:2)

这是您问题的工作代码 享受编码......

<html>
   <head>

      <style>

         .animated {
            background-color: green;
            background-position: left top;
            padding-top:95px;
            margin-bottom:60px;
            -webkit-animation-duration: 10s;animation-duration: 10s;
            -webkit-animation-fill-mode: both;animation-fill-mode: both;
         }

         @-webkit-keyframes fadeOut {
            0% {opacity: 1;}
            100% {opacity: 0;}
         }

         @keyframes fadeOut {
            0% {opacity: 1;}
            100% {opacity: 0;}
         }

         .fadeOut {
            -webkit-animation-name: fadeOut;
            animation-name: fadeOut;
         }
      </style>

   </head>
   <body>

      <div id="animated-example" class="animated fadeOut"></div>

   </body>
</html>