2011-07-19 154 views

回答

0

唯一的方法是使用CSS不透明度。實際上jQuery的animate()也使用它。

+0

這不會影響只是背景的不透明度不幸 – Paulpro

+0

@Molecule,@PaulPRO:在這種情況下,我需要改變不透明度爲1,而不是0.9吧? – Ken

+0

@PaulPRO,我不知道如何改變背景的不透明度。但是,它可能會通過醜陋的黑客來完成,例如添加具有相同大小的額外div,需要背景,並使用絕對定位和zIndex位於目標塊後面。 –

0

您可以使用預製的CSS類所需的動畫,然後使用jQuery來打開該類/關。

這裏的工作小提琴:https://jsfiddle.net/syoels/moL1q6ea/

$('#button1').click(function() { 
 
    $('.square').removeClass('fadeMeIn').addClass('fadeMeOut'); 
 
}); 
 

 
$('#button2').click(function() { 
 
    $('.square').removeClass('fadeMeOut').addClass('fadeMeIn'); 
 
});
.square { 
 
    height: 50px; 
 
    width: 50px; 
 
    margin: 10px; 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
} 
 
.fadeMeOut { 
 
    -webkit-animation: faeOutRGBA 1s linear; 
 
    -moz-animation: faeOutRGBA 1s linear; 
 
    -o-animation: faeOutRGBA 1s linear; 
 
    animation: faeOutRGBA 1s linear; 
 
    background-color: rgba(255, 0, 0, 0); 
 
} 
 
.fadeMeIn { 
 
    -webkit-animation: faeInRGBA 1s linear; 
 
    -moz-animation: faeInRGBA 1s linear; 
 
    -o-animation: faeInRGBA 1s linear; 
 
    animation: faeInRGBA 1s linear; 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
} 
 
@-webkit-keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8) 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
} 
 
@-moz-keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
} 
 
@-o-keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
} 
 
@keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    ; 
 
} 
 
@-webkit-keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0) 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
} 
 
@-moz-keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
} 
 
@-o-keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
} 
 
@keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    ; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="square"></div> 
 
<button id="button1">fade out</button> 
 
<button id="button2">fade in</button>

+0

它實際上可能很多更短 - 但我爲動畫提供了跨瀏覽器支持 – syoels

0

$('#button1').click(function() { 
 
    $('.square').removeClass('fadeMeIn').addClass('fadeMeOut'); 
 
}); 
 

 
$('#button2').click(function() { 
 
    $('.square').removeClass('fadeMeOut').addClass('fadeMeIn'); 
 
});
.square { 
 
    height: 50px; 
 
    width: 50px; 
 
    margin: 10px; 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
} 
 
.fadeMeOut { 
 
    -webkit-animation: faeOutRGBA 1s linear; 
 
    -moz-animation: faeOutRGBA 1s linear; 
 
    -o-animation: faeOutRGBA 1s linear; 
 
    animation: faeOutRGBA 1s linear; 
 
    background-color: rgba(255, 0, 0, 0); 
 
} 
 
.fadeMeIn { 
 
    -webkit-animation: faeInRGBA 1s linear; 
 
    -moz-animation: faeInRGBA 1s linear; 
 
    -o-animation: faeInRGBA 1s linear; 
 
    animation: faeInRGBA 1s linear; 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
} 
 
@-webkit-keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8) 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
} 
 
@-moz-keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
} 
 
@-o-keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
} 
 
@keyframes faeOutRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    ; 
 
} 
 
@-webkit-keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0) 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
} 
 
@-moz-keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
} 
 
@-o-keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
} 
 
@keyframes faeInRGBA { 
 
    0% { 
 
    background-color: rgba(255, 0, 0, 0); 
 
    } 
 
    100% { 
 
    background-color: rgba(255, 0, 0, 0.8); 
 
    } 
 
    ; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="square"></div> 
 
<button id="button1">fade out</button> 
 
<button id="button2">fade in</button>

2
var $obj = $('.selector'); 
$obj.css({volume: 50}); 
$obj.animate({volume: 90}, { 
    duration: 200, // default 400 
    step: function (n, t) { 
     $(t.elem).css({background: 'rgba(255,255,255,'+n/100+')'}); 
    } 
}); 

jquery animate

,也許你也需要:

if (!$obj.is(':animated')) { 
    $obj.animate(...); 
} 
相關問題