简体   繁体   English

带有循环的jQuery字体颜色动画

[英]jQuery font color animation with loop

I need a jQuery color animation without CSS3 functions.我需要一个没有 CSS3 函数的 jQuery 彩色动画。

I coded this:我这样编码:

 function button_selection() { setTimeout(button_selection, 800); setTimeout(function () { $(".color").css("color", "yellow"); }, 200); setTimeout(function () { $(".color").css("color", "blue"); }, 400); setTimeout(function () { $(".color").css("color", "red"); }, 600); setTimeout(function () { $(".color").css("color", "green"); }, 800); } setTimeout(button_selection, 0);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 class="color">Hello world, I love you all.</h1>

In my browser, it works.在我的浏览器中,它有效。 But I think that the code is not really "good".但我认为代码并不是真正“好”。 How would it be possible to write the colors in one line and then just control the speed?如何将颜色写在一行中,然后只控制速度?

Thanks.谢谢。

I would go with this我会用这个

 const colors = ['yellow', 'green', 'blue', 'red'] const INTERVAL = 200; colors.forEach((color, index) => { setTimeout(() => { $('.color').css("color", colors[index]) }, index*INTERVAL) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <h1 class="color">Hello world!<h1>

You could store all the colors in an array and then using an incremental variable to assign the colors with index.您可以将所有颜色存储在一个数组中,然后使用增量变量为颜色分配索引。

And also you could use setInterval instead of setTimeout.你也可以使用 setInterval 而不是 setTimeout。

 function button_selection() { let colors = ["yellow", "blue", "red", "green"]; let index = 0; $(".color").css("color", colors[index]); setInterval(() => { ++index; if (index >= colors.length) index = 0; $(".color").css("color", colors[index]); }, 1000); } setTimeout(button_selection, 0);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 class="color">Hello world, I love you all.</h1>

You don't need jQuery.你不需要jQuery。 Make a counter, increment it every n milliseconds and set the color depending on the counter state.创建一个计数器,每n毫秒递增一次,并根据计数器状态设置颜色。

 const element = document.querySelector('.color'); const colors = ['yellow', 'blue', 'red', 'green']; // The colors you want const colorDuration = 200; // The duration of a color in milliseconds let colorIndex = 0; function switchColor() { element.style.color = colors[colorIndex++ % colors.length]; } switchColor(); setInterval(switchColor, colorDuration);
 <h1 class="color">Hello world, I love you all.</h1>

Crate an array and assign the key/value pairs to the values you want to iterate through.创建一个数组并将键/值对分配给要迭代的值。 Then use $.each() to iterate over that array within your function.然后使用$.each()在函数中迭代该数组。

 function button_selection() { setTimeout(button_selection, 800); let colors = {"yellow": 200, "blue": 400, "red": 600, "green": 800}; $.each( colors, function( key, value ) { setTimeout(function () { $(".color").css("color", key); }, value); }); } setTimeout(button_selection, 0);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 class="color">Hello world, I love you all.</h1>

maybe this works for you:也许这对你有用:

 const CONFIG = { colors: ['yellow', 'blue', 'red', 'green'], speed: 200 }; function button_selection() { let counter = 0; setInterval(() => { $(".color").css("color", CONFIG.colors[counter]); counter = counter < CONFIG.colors.length ? counter + 1 : 0; }, CONFIG.speed); }; button_selection()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1 class="color">Hello world, I love you all.</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM