jquery随机颜色悬停

yos*_*shi 6 jquery colors hover

我正在使用jquery颜色来生成一些随机颜色.我喜欢这些颜色,当用户将鼠标悬停在任何单选按钮标签上时.

按照这个网站上的例子,我想我可能会尝试:

spectrum();

function spectrum(){

var hue = ('lots of stuff here that generates random hue -- code on example webpage')

$('label').hover(function() {
   $(this).animate( { color: hue }, 10000) });

spectrum(); 

}
Run Code Online (Sandbox Code Playgroud)

我的悬停选择器不起作用,一切都保持默认颜色.我显然是以某种方式搞砸了这个,但我没有足够的经验来理解出了什么问题.有什么建议?

sce*_*sor 16

试试这个:

$(document).ready(function() {
    $('label').hover(function() {
        var hue = 'rgb('
            + (Math.floor(Math.random() * 256)) + ','
            + (Math.floor(Math.random() * 256)) + ','
            + (Math.floor(Math.random() * 256)) + ')';
       $(this).stop().animate( { color: hue }, 500);
    },function() {
       $(this).stop().animate( { color: '#000' }, 500);
    });
});
Run Code Online (Sandbox Code Playgroud)

另见我的jsfiddle.

===更新===

function startAnimation(o) {
    var hue = 'rgb('
        + (Math.floor(Math.random() * 256)) + ','
        + (Math.floor(Math.random() * 256)) + ','
        + (Math.floor(Math.random() * 256)) + ')';
    $(o.currentTarget).animate( { color: hue }, 500, function() {
        startAnimation(o);
    });
}

$(document).ready(function() {
    $('label').hover(
        startAnimation,
        function() {
            $(this).stop().animate( { color: '#000' }, 500);
        }
    );
});
Run Code Online (Sandbox Code Playgroud)

看我更新的jsfiddle.