简体   繁体   English

jQuery .fadeTo() - 淡出后淡出

[英]jQuery .fadeTo() — Fade back in after fade out

My goal with this project is to have a div that, when hovered over, picks a random background color and then fades out. 我在这个项目中的目标是拥有一个div,当它悬停在上面时,会选择一个随机的背景颜色,然后淡出。 However, I want to be able to go back and hover again over the div and have the process start over again. 但是,我希望能够返回并再次悬停在div上并让过程重新开始。 So far, I've only managed to get the random color and have it fade out, but when I mouse over to hover again, nothing happens. 到目前为止,我只是设法获得随机颜色并让它淡出,但当我鼠标移动再次悬停时,没有任何反应。 Tried removing the opacity and background classes to no avail. 尝试删除不透明度和背景类无济于事。 Here's my code so far: 到目前为止,这是我的代码:

$(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 - Just a bunch of divs to test 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>

Thanks in advance! 提前致谢!

Try substituting .css() for .removeClass() , .addClass() 尝试用.css()代替.removeClass() .addClass()

$foo.css('background-color', "");
$foo.css('opacity', '1');

for 对于

$foo.removeClass('background-color');
$foo.addClass('opacity', '1');

 $(function() { var $foo = $('.foo'); function getRandomColor() { $foo.css("background-color", ""); $foo.css("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); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <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> 

I suggest you chain jQuery methods, and for going to 0 opacity and back, use fadeOut and fadeIn instead, along with a callback to reset the background color. 我建议你链接jQuery方法,并且为了转到0不透明度和返回,使用fadeOutfadeIn ,以及回调来重置背景颜色。 Like so. 像这样。

$(this).fadeOut(1000, function() { 
    $(this).css("background-color","transparent")
}); 
$(this).fadeIn(1000);

The fadeOut has the effect of fading to 0 opacity, in this case during 1 second. fadeOut具有淡入0不透明度的效果,在这种情况下为1秒。 fadeIn does the opposite, the callback resets the color. fadeIn则相反,回调重置颜色。 If you do not want a color reset, do just this instead. 如果您不想重置颜色,请改为执行此操作。

$(this).fadeOut(1000).fadeIn(1000);

Here's a jsFiddle . 这是一个jsFiddle Notice, that I removed the unneccessary class assignments, as you can not affect styling properties via classes, as you've tried to do. 请注意,我删除了不必要的类分配,因为您不能像通过类那样影响样式属性,正如您所尝试的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM