动态文本颜色变化
Dynamic text color changing
我已经编写了这段用于动态更改文本颜色的代码。函数工作正常,但问题是存在递归。通话后我有 js "too much recursion" 错误。有没有可能或其他解决方案可以毫无问题地做到这一点?
function dynamicColor() {
$(".class").animate({
color: 'rgb(255,40,80)'
}, 1000).animate({
color: 'rgb(100,255,40)'
}, 1000).animate({
color: 'rgb(40,100,255)'
}, 1000).animate({
color: 'rgb(255,40,100)',
complete: dynamicColor()
}, 1000);
}
因为你没有赋值引用,所以一直调用它
complete: dynamicColor()
需要
complete: dynamicColor
或
complete: function() { dynamicColor(); }
下一个问题是您已经完成了要设置动画的属性的内部。它不会被调用。
function dynamicColor() {
$(".class").animate({height: "100px"}, 1000)
.animate({height: "200px"}, 1000)
.animate({height: "300px"}, 1000)
.animate({height: "200px"},{duration: 1000,complete: dynamicColor});
}
dynamicColor();
.class {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="class">This is a test</div>
我已经编写了这段用于动态更改文本颜色的代码。函数工作正常,但问题是存在递归。通话后我有 js "too much recursion" 错误。有没有可能或其他解决方案可以毫无问题地做到这一点?
function dynamicColor() {
$(".class").animate({
color: 'rgb(255,40,80)'
}, 1000).animate({
color: 'rgb(100,255,40)'
}, 1000).animate({
color: 'rgb(40,100,255)'
}, 1000).animate({
color: 'rgb(255,40,100)',
complete: dynamicColor()
}, 1000);
}
因为你没有赋值引用,所以一直调用它
complete: dynamicColor()
需要
complete: dynamicColor
或
complete: function() { dynamicColor(); }
下一个问题是您已经完成了要设置动画的属性的内部。它不会被调用。
function dynamicColor() {
$(".class").animate({height: "100px"}, 1000)
.animate({height: "200px"}, 1000)
.animate({height: "300px"}, 1000)
.animate({height: "200px"},{duration: 1000,complete: dynamicColor});
}
dynamicColor();
.class {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="class">This is a test</div>