微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery随机颜色悬停

我正在使用 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.

原文地址:https://www.jb51.cc/jquery/176851.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐