【发布时间】:2016-02-28 20:19:00
【问题描述】:
我对这个项目的目标是有一个 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>
提前致谢!
【问题讨论】:
标签: javascript jquery html css