2015-10-13 36 views
1

我试图循环浏览一些单词,并以“文本类型”方式对它们进行动画处理。这对第一个单词很有效,但是一旦我更新动画段落的内容,动画就不存在了。基本上,一旦你更新了内容,你怎么能触发CSS动画再次运行?使用Jquery更改内容后再次运行CSS动画

var text = ["string1", "string2", "string3"]; 
 
$('.css-typing').text(text[0]); 
 
var i = 1; 
 

 
myInt = setInterval(function() { 
 
     $('.css-typing').text(text[i]); 
 
     i++; 
 
    },5000);
.css-typing { 
 
    width: 30em; 
 
    color: #a7a37e; 
 
    font-size: 3em; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    -webkit-animation: type 5s steps(50, end); 
 
    animation: type 5s steps(50, end); 
 
    display: block; 
 
    margin-left: 0; 
 
} 
 
@keyframes type { 
 
    from { 
 
    width: 0; 
 
    } 
 
} 
 
@-webkit-keyframes type { 
 
    from { 
 
    width: 0; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p class="css-typing">start</p>

回答

0

你可以使用这个逻辑

 var el  = $('#test'), 
    newone = el.clone(true);      //clone the existing 
    el.before(newone);        //add it before the current 
    $("." + el.attr("class") + ":last").remove(); //remove the existing 

DEMO:

var text =["string1", "string2", "string3"]; 
 
$('.css-typing').text(text[0]); 
 
var i = 1; 
 
myInt = setInterval(function(){ 
 

 
$('#test').text(text[i]); 
 
    
 
    var el  = $('#test'), 
 
newone = el.clone(true); 
 
el.before(newone); 
 
$("." + el.attr("class") + ":last").remove(); 
 
    
 
    i++; 
 
},6000);
.css-typing 
 
{ 
 
    width: 30em; 
 
    color: #a7a37e; 
 
    font-size: 3em; 
 
    white-space:nowrap; 
 
    overflow:hidden; 
 
    -webkit-animation: type 5s steps(50, end); 
 
    animation: type 5s steps(50, end); 
 
    display:block; 
 
    margin-left:auto; 
 
    margin-right:auto; 
 
} 
 

 

 
@keyframes type{ 
 
    from { width: 0; } 
 

 

 
@-webkit-keyframes type{ 
 
    from { width: 0; } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p id="test" class="css-typing"></p>

JSFiddle:http://jsfiddle.net/kishoresahas/qdjro8pw/

+0

这样本不工作? – OliverJ90

+0

我认为这是因为https脚本引用..试试这个http://jsfiddle.net/kishoresahas/qdjro8pw/ –

+0

仍然没有看到它我害怕。 – OliverJ90

0

这是你想要的吗?现在数组中的所有文本元素都具有打字效果。好主意。

$(function(){ 
 
var text = ["string1", "string2", "string3"]; 
 
var i = 0; 
 

 
if(i == 0) { 
 
\t $('.css-typing').text(text[0]).addClass("typeClass"); 
 
\t ++i; 
 
}else{ 
 

 
} 
 
\t 
 
myInt = setInterval(function() { 
 
\t 
 
\t if(!$('.css-typing').hasClass("typeClass") && i >= 1 && i < text.length) { 
 

 
\t \t $('.css-typing').text(text[i]).toggleClass("typeClass") 
 
\t \t ++i; 
 
\t } 
 
\t else{ 
 
\t \t $('.css-typing').removeClass("typeClass") 
 

 
\t } 
 
},2000); 
 
\t })
.css-typing { 
 
    width: 30em; 
 
    color: #a7a37e; 
 
    font-size: 3em; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    /* -webkit-animation: type 5s steps(50, end); 
 
    animation: type 5s steps(50, end);*/ 
 
    display: block; 
 
    margin-left: 0; 
 
} 
 
.typeClass{ 
 
    \t animation: type 5s steps(50, end);; 
 
} 
 
@keyframes type { 
 
    from { 
 
    width: 0; 
 
    } 
 
} 
 
@-webkit-keyframes type { 
 
    from { 
 
    width: 0; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 
 
<p class="css-typing">start</p>

与动画填充模式新的CSS:前锋

.css-typing { 
    width: 0; 
    color: #a7a37e; 
    font-size: 3em; 
    white-space: nowrap; 
    overflow: hidden; 
    /* -webkit-animation: type 5s steps(50, end); 
    animation: type 5s steps(50, end);*/ 
    display: block; 
    margin-left: 0; 
} 
.typeClass{ 
    -o-animation: type 5s steps(50, end) forwards; 
    -webkit-animation: type 5s steps(50, end) forwards; 
    animation: type 5s steps(50, end) forwards; 
} 
@keyframes type { 
    from { 
    width: 0; 
    } 
    to{ 
    width:30em; 
    } 
} 
@-webkit-keyframes type { 
    from { 
    width: 0; 
    } 
    to{ 
    width:30em; 
    } 
} 
+0

我不明白这是如何工作的片段,但分离出一个浏览器(铬,最新)的第一个动画的作品,然后它只是闪烁通过其余的数组。 – OliverJ90

+0

也许尝试在您用于动画的浏览器的前缀中添加前缀。顺便说一句,我更新了代码,所以第一个字符串在开始时被动画化。并在没有字符串时停止动画 –

+0

尝试过,这很奇怪。这段代码只适用于chrome,而不是safari,可能是由于前缀,但在浏览器(chrome,safari)中根本无法使用前缀。时髦的生意。 – OliverJ90

相关问题