简体   繁体   English

阻止执行.mouseout事件

[英]Prevent .mouseout event from executing

I made a listing of images and description in this HTML structure: 我在此HTML结构中列出了图像和说明:

<div class="canDo">
    <p>
        <img src="http://placehold.it/100x100" />
        <span>Description 1</span>
    </p>
    <p>
        <img src="http://placehold.it/100x100" />
        <span>Description 2</span>
    </p>
    <p>
        <img src="http://placehold.it/100x100" />
        <span>Description 3</span>
    </p>

    <p></p>
</div>

I hide the <span> with CSS and add the description into the last <p> using a jQuery function. 我用CSS隐藏<span>并使用jQuery函数将描述添加到最后一个<p> The HTML structure is chosen to work with my responsive layout as well. 选择HTML结构也可以与我的响应式布局一起使用。

$(document).ready( function() {
    $(".canDo img").mouseover( function() {
        $(".canDo img").removeClass('active').addClass('fade');
        $(this).addClass('active').removeClass('fade');
        $(".canDo p:last-child").fadeOut(function() {
            var msg = $('.canDo img.active').next('span').html()
            $(".canDo p:last-child").text(msg).fadeIn();
        });
    })
    .mouseout( function() {
        setTimeout(function(){
            $(".canDo img").removeClass('active fade')
            $(".canDo p:last-child").fadeOut();
        }, 3000);
    });
});

The problem I'm having is that when I hover the first item and then the second item (and keep the mouse on that second one) the mouseout function from the first item is executed, making the fading effect and the text disappear. 我遇到的问题是,当我将鼠标悬停在第一项然后第二项(并将鼠标悬停在第二项上)上时,将执行第一项的mouseout功能,从而使淡入淡出效果和文本消失。

How can I prevent that from happening? 如何防止这种情况发生?

I also made a jsFiddle . 我还做了一个jsFiddle

try this... I think it's what you want. 试试这个...我想这就是你想要的。 let me know if it's not. 让我知道是否不是。

http://jsfiddle.net/bpd2Ldy1/ http://jsfiddle.net/bpd2Ldy1/

so... what I did was assign the result of the setTimeout function (which returns a specific timeout id) to the variable tm . 所以...我所做的就是将setTimeout函数的结果(返回特定的超时ID)分配给变量tm If it is set (meaning something is fading out after 3 seconds), and the user mouses-over another little box, it will clear and stop the timeout. 如果已设置(表示3秒钟后某些东西逐渐消失),并且用户将鼠标悬停在另一个小盒子上,它将清除并停止超时。 that allows for it to not conflict with the new mouseover event. 这样就不会与新的mouseover事件发生冲突。 if nothing .canDo is moused-over, the timeout will complete uninterrupted after 3 seconds. 如果没有鼠标悬停.canDo ,则超时将在3秒后不中断地完成。

$(document).ready( function() {
    $(".canDo img").mouseover( function() {
        $(".canDo img").removeClass('active').addClass('fade');
        $(this).addClass('active').removeClass('fade');

        if (typeof(tm) != 'undefined') {
            clearTimeout(tm);
        }
        $(".canDo p:last-child").stop().fadeOut(function() {
            var msg = $('.canDo img.active').next('span').html()
            $(".canDo p:last-child").text(msg).stop().fadeIn();
        });

    })
    .mouseout( function() { 
        tm = window.setTimeout(function(){
            $(".canDo img").removeClass('active fade')
            $(".canDo p:last-child").stop().fadeOut("slow");  
        }, 3000); 
    });
});

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

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