javascript - 使用 Jquery 更改内容后 CSS 动画再次运行

标签 javascript jquery css animation

我正在尝试循环一些单词并以“文本类型”的方式为它们设置动画。这对于第一个单词效果很好,但是一旦我更新动画段落的内容,动画就不会出现。基本上,更新内容后如何触发 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>

最佳答案

你可以使用这个逻辑

     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

演示:

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/

关于javascript - 使用 Jquery 更改内容后 CSS 动画再次运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33104979/

相关文章:

html - 在样式按钮中控制链接样式

css - 移动设备上 "fixed"元素的视口(viewport)宽度问题

c# - Asp.net MVC 4 Ajax.BeginForm 提交 + mvc.grid - 页面重新加载

android - HTML 站点图像背景在 iPhone 上显示错误

javascript - 从字符串路径动态更新 JavaScript 对象

javascript - 更改所选项目的文本以包括其父 optgroup

javascript - 错误 : Invalid value for <g> attribute transform ="scale(NaN) translate(NaN, NaN)"

javascript - 如何将 $.get 的结果传递给另一个函数的返回

javascript - 在 Progressive Web App 中使用系统应用程序打开文件

javascript - js。拼接返回删除的项目?