CSS过渡:淡化背景颜色,重置后

Ben*_*iFB 28 html css css3

我有一个div列表,允许我的用户通过发布新内容动态添加新的div.如果用户发布新内容,我想通过将新div的背景颜色淡化为另一种颜色并将其淡化来在屏幕上突出显示它.我非常接近:

http://jsfiddle.net/pUeue/1953/

我正在使用这个CSS来触发转换:

.backgroundAnimated{
    background-color: #AD310B !important;
    background-image:none !important;
   -webkit-transition: background-color 5000ms linear;
   -moz-transition: background-color 5000ms linear;
   -o-transition: background-color 5000ms linear;
   -ms-transition: background-color 5000ms linear;
    transition: background-color 5000ms linear;
    -webkit-animation-direction: alternate; /* Chrome, Safari, Opera */
    animation-direction: alternate;

    -webkit-animation-iteration-count: 2; /* Chrome, Safari, Opera */
    animation-iteration-count: 2;
 }
Run Code Online (Sandbox Code Playgroud)

我可以褪色到另一种颜色,但它不会消退.我想知道是否有人有建议要做到这一点?我意识到有很多方法可以做到这一点,但我希望完全包含在CSS中(除了通过jQuery动态添加CSS类).

谢谢你的任何建议......

Lar*_*arz 56

您可以使用动画关键帧.无需额外的JavaScript.

$('input[type=button]').click( function() {
 $('#newContent').addClass('backgroundAnimated');
});
Run Code Online (Sandbox Code Playgroud)
@-o-keyframes fadeIt {
  0%   { background-color: #FFFFFF; }
  50%  { background-color: #AD301B; }
  100% { background-color: #FFFFFF; }
}
@keyframes fadeIt {
  0%   { background-color: #FFFFFF; }
  50%  { background-color: #AD301B; }
  100% { background-color: #FFFFFF; }
}

.backgroundAnimated{    
    background-image:none !important; 
         -o-animation: fadeIt 5s ease-in-out; 
            animation: fadeIt 5s ease-in-out; 
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Old stuff</div>
<div>Old stuff</div>
<div>Old stuff</div>
<div id="newContent">New stuff, just added</div>

<input type="button" value="test" />
Run Code Online (Sandbox Code Playgroud)

  • @althaus是的,只需将animation-iteration-count属性设置为您希望它运行的次数,或者无限(如果您希望它继续)。https://developer.mozilla.org/en-US/docs/Web/CSS/animation-iteration-count (2认同)

cpo*_*rt1 5

嗯.. 如果你想把它保留在 css 中,你可以在你的点击事件中添加一个 setTimeout ,然后添加另一个类。

$('input[type=button]').click( function() {
 $('#newContent').addClass('backgroundAnimated');
    setTimeout(function(){
        $('#newContent').addClass('nextBackgroundAnimated');
    }, 5000);
});
Run Code Online (Sandbox Code Playgroud)

CSS::

.backgroundAnimated{
        background-color: #AD310B !important;
        background-image:none !important;
    -webkit-transition: background-color 5000ms linear;
    -moz-transition: background-color 5000ms linear;
    -o-transition: background-color 5000ms linear;
    -ms-transition: background-color 5000ms linear;
    transition: background-color 5000ms linear;
        -webkit-animation-direction: alternate; /* Chrome, Safari, Opera */
    animation-direction: alternate;

        -webkit-animation-iteration-count: 2; /* Chrome, Safari, Opera */
    animation-iteration-count: 2;
}
.nextBackgroundAnimated{
        background-color: white !important;
        background-image:none !important;
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/pUeue/1954/