简体   繁体   English

通过淡入和淡出以块形式对文本进行动画处理

[英]Animate text change in block with fade-in and fade-out

I have a div which content is changed dynamically by a script and I wanna add fade-in and fade-out animation on text when it's being changed. 我有一个div,其内容由脚本动态更改,并且我想在更改文本时在文本上添加淡入和淡出动画。 How can I do it using CSS or pure JS? 如何使用CSS或纯JS做到这一点? All the solutions I've seen so far involved jQuery, while I'm interested in pure CSS and/or JS. 到目前为止,我所看到的所有解决方案都涉及jQuery,而我对纯CSS和/或JS感兴趣。

Fade Out: 淡出:

var fadeout = function(elem) {
var o = 1;
var timer = setInterval(function () {
    if (o <= 0.0) {
        clearInterval(timer);
    }
    elem.style.opacity = o;
    elem.style.filter = 'alpha(opacity=' + o * 100 + ")";
    o -= 0.1;
 }, 25);
};

Fade In: 淡入:

var fadein = function(elem) {
var o = 0;
var timer = setInterval(function () {
    if (o >= 1.0) {
        clearInterval(timer);
    }
    elem.style.opacity = o;
    elem.style.filter = 'alpha(opacity=' + o * 100 + ")";
    o += 0.1;
 }, 25);
};

Also this fiddle seems amazing: http://jsfiddle.net/gabrieleromanato/cMp7s/ 这个小提琴也很神奇: http : //jsfiddle.net/gabrieleromanato/cMp7s/

Pure CSS: http://fvsch.com/code/transition-fade/test5.html 纯CSS: http//fvsch.com/code/transition-fade/test5.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM