jquery随机颜色悬停

前端之家收集整理的这篇文章主要介绍了jquery随机颜色悬停前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 jquery color生成一些随机颜色.我喜欢这些颜色,当用户将鼠标悬停在任何单选按钮标签上时.

按照this site的例子,我想我可能会尝试:

  1. spectrum();
  2.  
  3. function spectrum(){
  4.  
  5. var hue = ('lots of stuff here that generates random hue -- code on example webpage')
  6.  
  7. $('label').hover(function() {
  8. $(this).animate( { color: hue },10000) });
  9.  
  10. spectrum();
  11.  
  12. }

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

解决方法

试试这个:
  1. $(document).ready(function() {
  2. $('label').hover(function() {
  3. var hue = 'rgb('
  4. + (Math.floor(Math.random() * 256)) + ','
  5. + (Math.floor(Math.random() * 256)) + ','
  6. + (Math.floor(Math.random() * 256)) + ')';
  7. $(this).stop().animate( { color: hue },500);
  8. },function() {
  9. $(this).stop().animate( { color: '#000' },500);
  10. });
  11. });

另见我的jsfiddle.

===更新===

  1. function startAnimation(o) {
  2. var hue = 'rgb('
  3. + (Math.floor(Math.random() * 256)) + ','
  4. + (Math.floor(Math.random() * 256)) + ','
  5. + (Math.floor(Math.random() * 256)) + ')';
  6. $(o.currentTarget).animate( { color: hue },500,function() {
  7. startAnimation(o);
  8. });
  9. }
  10.  
  11. $(document).ready(function() {
  12. $('label').hover(
  13. startAnimation,function() {
  14. $(this).stop().animate( { color: '#000' },500);
  15. }
  16. );
  17. });

请参阅我更新的jsfiddle.

猜你在找的jQuery相关文章