jquery随机颜色悬停

标签 jquery colors hover

我正在使用jquery color生成一些随机颜色。我希望当用户将鼠标悬停在任何单选按钮标签上时显示这些颜色。

按照 this site 上的示例进行操作,我想我可以尝试一下:

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(); 

}

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

最佳答案

试试这个:

$(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);
    });
});

另请参阅我的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);
        }
    );
});

查看我的更新jsfiddle .

关于jquery随机颜色悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7379353/

相关文章:

javascript - 使用存储在变量中的 jQuery 句柄

r - 具有 ggplot2 较低级别图的多种颜色缩放

CSS: hover-issues specifically on iOS 5.1

css - 显示过渡元素

javascript - JS 将函数的结果传递给其他函数

javascript - 提交按钮不发送任何内容

jquery - Jssor slider : How to target specific slide with text/image link?

python - 使用 matplotlib 绘制 RGB 值

colors - 显示对应于行颜色的seaborn clustermap的图例

html - 类名 :hover #myID {style}