我正在使用这个数学公式来实现鼠标悬停时的背景色动画:
var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
它产生一个随机的RGB颜色。确实很好,但我想要不同的东西。
有人知道我可以使用哪个好的数学方法,让这个随机颜色仅在某个颜色范围内,如红色范围或绿色范围外?
感谢任何帮助。
@Avinash,这是我目前正在使用的完整代码。我应该如何包含你的解决方案?
$(document).ready(function () {
//bg color animation
original = $('.item,.main-menu-button').css('background-color');
$('.item,.main-menu-button').hover(function () { //mouseover
var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')'; //random hover color
$(this).stop().animate({
'backgroundColor': col
}, 1000);
}, function () { //mouseout
$(this).stop().animate({
'backgroundColor': '#111'
}, 500); //original color as in css
});
});
它不起作用。我最好将其保留为原样。感谢你们的帮助。