jquery随机颜色悬停

发布于 2024-12-04 12:37:52 字数 588 浏览 4 评论 0原文

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

按照此网站上的示例,我想我可以尝试:

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

}

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

I'm using jquery color to generate some random colors. Id like these colors to appear when the user hovers over any radio button labels.

Following the example on this site, i thought i might try:

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

}

My hover selector isn't working and everything is staying its default color. I'm obviously bungling this somehow, but I'm not experienced enough to understand what's going wrong. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮坠入山谷 2024-12-11 12:37:52

试试这个:

$(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

Try this:

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

Also see my jsfiddle.

=== UPDATE ===

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

See my updated jsfiddle.

2024-12-11 12:37:52

$('label').hover 将为标签添加 mouseover/mouseout 事件。你无限地这样做,因为你一遍又一遍地调用频谱。
试试这个吧。

$('label').hover(function() {
    (function anim() {
        var hue = //hue stuff
        $(this).animate({
            color: hue
        }, 10000, function() {
            anim();
        });
    })();
}, function() {
    $( this ).animate({
         color: //original color
    }, 1000);
});

$('label').hover will add a mouseover/mouseout event to the label. You are doing this infinitely as, you are calling spectrum over and over again.
Try this instead.

$('label').hover(function() {
    (function anim() {
        var hue = //hue stuff
        $(this).animate({
            color: hue
        }, 10000, function() {
            anim();
        });
    })();
}, function() {
    $( this ).animate({
         color: //original color
    }, 1000);
});
鹿港巷口少年归 2024-12-11 12:37:52

主要问题是jQuery不支持彩色动画。可以找到更多结果 jQuery:输入字段的动画文本颜色?

The main problem that jQuery doesn't support the color animation. More results can be found jQuery: animate text color for input field?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文