【发布时间】:2011-04-20 05:45:35
【问题描述】:
我将这个数学用于悬停时的背景颜色动画:
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
});
});
它不起作用。我最好保持原样。感谢大家的帮助。
【问题讨论】:
-
使用HSL然后转换成RGB怎么样? en.wikipedia.org/wiki/HSL_and_HSV(编辑:css3 支持 HSL)
标签: jquery math animation colors