-6

I want to set background-color with jQUery animate function and i need to pick a random element from array.

$(".menu li").hover(function() {
  $(this).animate({borderBottomColor:"#81C6DD"}, 200)
}, function() {
  $(this).animate({borderBottomColor:"#D8D9DC"}, 200)
});
Andrey
  • 81
  • 2
  • 3
  • 13

2 Answers2

7

Sounds like you don't want to randomize the array, sounds like you want to pick a random element from the array each time. Assuming your array of colors is called colors, use:

var color = colors[Math.floor(colors.length * Math.random())];

Although I question why you'd want to do that. Random colors sounds like a recipe for a really ugly and potentially confusing UI.

MattW
  • 4,480
  • 1
  • 12
  • 11
0

Create an array with lot of color values and use any function in javascript which generate an random number between a range, in our case we need to generate a random number between o to array length of color array. And then use this random number as a key in color array and put this in place of color.

Vijay Verma
  • 3,660
  • 2
  • 19
  • 27