简体   繁体   English

如何在mouseleave上淡出此淡入淡出?

[英]How do I fade out this fade on mouseleave?

I want to be able to fade out the div that just got faded in, but I can't get it to work. 我希望能够淡出刚刚淡入的div,但无法使其正常工作。 I have the following code: 我有以下代码:

    $(document).ready(function() {
        $('.upgrade').mouseenter(function() {
            var modal = $(this).data('modal');
            // alert(modal);
            $('.' + modal).fadeIn(200);
        }).mouseleave(function() {
            $('.' + modal).stop().fadeOut(200);
        });
    });

    <div class="upgrade" id="upgrade-1" data-modal="bonus">
       <!-- text -->
    </div>

   <div class="bonus">
      <!-- bonus product text -->
   </div>

You need to delcare modal outside mouseenter function 您需要在鼠标输入功能之外添加delcare modal

$(document).ready(function() {
     var modal="";//declare here
    $('.upgrade').mouseenter(function() {
         modal = $(this).data('modal');
        $('.' + modal).fadeIn(200);
    }).mouseleave(function() {
        $('.' + modal).stop().fadeOut(200);
    });
});

DEMO DEMO

I assume the mistake is not defining modal in the mouseleave event. 我认为错误不是在mouseleave事件中定义modal

Try 尝试

 $(document).ready(function() {
        $('.upgrade').mouseenter(function() {
            var modal = $(this).data('modal');
            // alert(modal);
            $('.' + modal).fadeIn(200);
        }).mouseleave(function() {
            var modal = $(this).data('modal');
            $('.' + modal).stop().fadeOut(200);
        });
    });

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

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