Jquery,背景颜色过渡循环

发布于 2024-09-16 14:48:27 字数 104 浏览 2 评论 0原文

我希望对背景颜色进行动画处理,以便它在无限循环中循环显示各种预定义颜色。

我不是程序员,而且是 jquery 新手,如果有人能帮助我解决这个问题,我将非常感激

I am looking to animate the background-color so it cycles through varios predefined colors in an endless loop.

I am not a programmer and new to jquery, if someone could help me figure this out I would really appreciate ist

thx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-09-23 14:48:27

基于 Byron 的解决方案并使用彩色动画插件

// The array of colours and current index within the colour array
// This array can be hex values or named colours
var colours = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var currIndex = 0;

// The element to animage the background-color of
var elem = $('#element-id');

// Infinitely animate the background-color of your element every second
var loop = setInterval(function(){
    elem.animate({backgroundColor: colours[currIndex++]});

    // Set the current index in the colour array, making sure to loop
    // back to beginning once last colour is reached
    currIndex = currIndex == colours.length ? 0 : currIndex;
}, 1000);

您可以看到它在此处执行

Building on Byron's solution and making use of the color animations plugin:

// The array of colours and current index within the colour array
// This array can be hex values or named colours
var colours = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var currIndex = 0;

// The element to animage the background-color of
var elem = $('#element-id');

// Infinitely animate the background-color of your element every second
var loop = setInterval(function(){
    elem.animate({backgroundColor: colours[currIndex++]});

    // Set the current index in the colour array, making sure to loop
    // back to beginning once last colour is reached
    currIndex = currIndex == colours.length ? 0 : currIndex;
}, 1000);

You can see it in action here.

°如果伤别离去 2024-09-23 14:48:27

window.setInterval('functionToChangeColour()', 5000);这将每 5000 毫秒运行一次您的函数,按照您希望的方式更改颜色。

您可以分配一个对象 var obj = setInterval('functionToChangeColour()', 5000);如果您喜欢使用 window.clearInterval(obj) ,稍后可以清除间隔

window.setInterval('functionToChangeColour()', 5000); This will run your function every 5000 milliseconds changing the colour the way you'd like it to change.

You can assign an object var obj = setInterval('functionToChangeColour()', 5000); to then later clear the interval if you like using window.clearInterval(obj)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文