简体   繁体   English

使用setInterval和jQuery中的数组更改多个元素的背景色

[英]change the background-color of multiple elements with setInterval and arrays in jQuery

I need to change the background-color of multiple elements at the same time. 我需要同时更改多个元素的背景颜色。 I tried several versions of this code: 我尝试了此代码的多个版本:

var indexNumber = 0;

function colorChange(){
var colors = ["#0e76bd", "#aa43a0" ,"#5ad1e3"]; 
//var randColor = colors[Math.floor(Math.random()*colors.length)];
$('a, h1, h2, .subhead').animate({'color':colors[indexNumber]}, 1000);
$('.splash, .active').animate({'background-color':colors[indexNumber]}, 1000);
}

setInterval(function() {
colorChange()
indexNumber++
if (indexNumber == 3)
{indexNumber = 0
}}, 1000);

This made the colors change, but rather than all the colors advancing to the next sequence in the array, I get a random result where all elements are different colors. 这使颜色发生了变化,但不是所有颜色都前进到数组中的下一个序列,而是得到一个随机结果,其中所有元素都是不同的颜色。

I want all of my elements to change to the same color at the same time. 我希望所有元素同时更改为相同的颜色。 Is there an easy way to do this? 是否有捷径可寻?

You cannot animate colors using default jQuery animation framework. 您无法使用默认的jQuery动画框架为颜色设置动画。 From the documentation : 文档中

All animated properties should be animated to a single numeric value, except as noted below; 除非另有说明,否则所有动画属性都应设置为单个数值。 most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width, height, or left can be animated but background-color cannot be, unless the jQuery.Color() plugin is used). 大多数非数字属性不能使用基本的jQuery功能进行动画处理(例如,可以对width,height或left进行动画处理,而不能对background-color进行动画处理,除非使用jQuery.Color()插件)。

So the answer is if you want to use jQuery you should also add jQuery UI animation plugin . 因此,答案是,如果要使用jQuery,还应该添加jQuery UI动画插件 Take a look at this demo: http://jsfiddle.net/g3QR3/ 看一下这个演示: http : //jsfiddle.net/g3QR3/

Here is the jQuery color plugin. 这是jQuery颜色插件。

Or instead you could use CSS transitions, that would not require another plugin. 或者,您可以使用CSS过渡,而无需其他插件。

var indexNumber = 0;

function colorChange() {
    var colors = ["one", "two", "three"];
    $('a, h1, h2, .subhead, .splash, .active').removeClass("one two three").addClass(colors[indexNumber]);
}

setInterval(function () {
    colorChange()
    indexNumber++
    if (indexNumber == 3) {
        indexNumber = 0
    }
}

CSS: CSS:

.colors {
    -webkit-transition: color 1s ease;
    transition: color 1s ease;
}
.one {
    color: #0e76bd;
}
...

Demo: http://jsfiddle.net/g3QR3/1/ 演示: http//jsfiddle.net/g3QR3/1/

You don't need jquery UI to do this.. use interval and css3 animation if you want to animate: 您不需要jquery UI来执行此操作。如果要设置动画,请使用interval和css3动画:

CSS CSS

a, h1, h2, .subhead, .splash, .active { -webkit-transition:color 1s ease, background-color 1s ease; transition:color 1s ease, background-color 1s ease;}

JS JS

var _index = 0, colors = ["#0e76bd", "#aa43a0" ,"#5ad1e3"];

setInterval(changeColor, 1000);

function changeColor() {
    $('a, h1, h2, .subhead').css({ 'color': colors[_index] });
    $('.splash, .active').css({'background-color':colors[_index]});
    _index = (_index < colors.length-1) ? _index+1 : 0;
}

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

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