gpt4 book ai didi

javascript - 在 javascript 函数中使用 jquery animate

转载 作者:行者123 更新时间:2023-11-30 16:06:25 25 4
gpt4 key购买 nike

我只有一个 divbackground color 覆盖了整个名为“colorPanel”的 View (使用html ID)。

这段代码没有运行,我不明白为什么。

当我将 col 更改为 $col 时,它仍然无法运行。

我对 jquery 有什么不了解的地方?

function animateColors(i){
var val = 1/0.2*(i) * 210;
var col = 'hsla('+val+', 90%, 60%, 1)';
$("#colorPanel").animate({"background-color": col}, 200);
i = (i<180)? i+4 : 0;
animateColors(i);
}

animateColors();

最佳答案

Already @brut & @tiantang suggest the valid point as comment. please consider that.

You arent passing anything to the initial call to animateColors(). Is this a typeo? If not i is undefined for the initial run of the program. Also there is no terminating condition for your recursive function.

From the documentation, "The .animate() method allows us to create animation effects on any numeric CSS property." and "For example, width, height, or left can be animated but background-color cannot be, unless the jQuery.Color plugin is used". Depending on your requirements, you could just use a CSS transition.

根据这些评论,我将对您的代码进行更改。

$(document).ready(function(){
function animateColors(i){
var val = 1/0.2*(i) * 210;
var col = 'hsla('+val+', 90%, 60%, 1)';
$("#colorPanel").css({"background-color": col});
i = (i<180)? i+4 : 0;
// animateColors(i); // when using this too much recursion error occur
setTimeout( function(){animateColors(i)}, 500);
}
animateColors(1);
});
#colorPanel {
height: 20px; /*your wish*/
width: 100%; /*your wish*/
transition: all ease 0.5s; /*for animation effect*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="colorPanel"></div>

希望对您有所帮助。

关于javascript - 在 javascript 函数中使用 jquery animate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36974396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com