首页 > 解决方案 > 如何使用 .animate 更改文本颜色

问题描述

我想在用新信息更新时闪烁一些文本。为此,我需要一个过渡。因为信息将在 60 秒内全部更新我想使用 jQuery .animate 方法。

由于某种原因,它无法更改文本颜色。保证金和其他东西工作正常。有没有办法改变 textcolor 线性?

$('html').click(function() {
  $('div').animate({ color: 'red', 'margin': '200px' }, 700);
});
body {
  background: grey;
  color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="test">My Text</div>

标签: javascriptjqueryhtmlcss

解决方案


您可以通过在单击事件后添加动画类来实现。请检查以下解决方案:

$('html').click(function() {
  $('div').addClass('animated');
});

body {
  background: grey;
  color: #fff;
}

.animated {
 margin:200px;
 color:red;
 transition:all .7s;
}

和小提琴 https://jsfiddle.net/1wfnyuez/


推荐阅读