我希望通过这个项目,实现一个div,在鼠标悬停时随机选择背景颜色并淡出。然而,我想能够再次悬停在div上,并重新开始这个过程。到目前为止,我只能得到随机颜色并让它淡出,但当我再次悬停时,什么也没有发生。尝试删除透明度和背景类,但无济于事。以下是我的代码:
$(function() {
var $foo = $('.foo');
function getRandomColor() {
$foo.removeClass('background-color');
$foo.addClass('opacity', '1');
var length = 6;
var chars = '0123456789ABCDEF';
var hex = '#';
while(length--) hex += chars[(Math.random() * 16) | 0];
return hex;
}
$foo.mouseenter(function(){
$(this).css('background-color', getRandomColor());
});
$foo.mouseleave(function(){
$(this).fadeTo( 1000, 0 );
});
});
HTML - 只是一堆div用于测试
<div class="foo box">Hello world</div>
<div class="foo box">Hello world</div>
<div class="foo box">Hello world</div>
<div class="foo box">Hello world</div>
<div class="foo box">Hello world</div>
<div class="foo box">Hello world</div>
提前感谢您!
$(this).fadeOut(1000, function() { $(this).css("background-color","transparent")}); $(this).fadeIn(1000);
谢谢! - EFH