Make mouseout one time

Asked

Viewed 60 times

1

I wonder how I can make this script work one time, because I can’t do it:

// intenção de sair
function addEvent(obj, evt, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evt, fn, false);
    }
    else if (obj.attachEvent) {
        obj.attachEvent("on" + evt, fn);
    }
}

// gatilho 
addEvent(document, 'mouseout', function(evt) {

    if (evt.toElement == null && evt.relatedTarget == null ) {
        $('#lightbox_overlay').slideDown();
    };

});

// fechando o popup           
$('#lightbox_overlay, #close').on('click', function(event) { 
    $('.lightbox').slideUp();                            
});
  • Put and did not work if($('#lightbox_overlay'). is(":Visible")) Return; addEvent(Document, 'mouseout', Function(evt) { if (evt.toElement == null && evt.relatedTarget == null ) { $('#lightbox_overlay'). slideDown(); } });

  • I tried that too and it didn’t work addEvent(Document, 'mouseout', Function(evt) { if($('#lightbox_overlay'). is(":Visible")) Return; { $('#lightbox_overlay'). slideDown(); }

  • You want the obj.addEventListener(evt, fn, false); roll only once?

  • that’s right, just once, when the mouse leaves, appear only once. I’ve tried using Jquery’s . one() but it didn’t roll.

  • Try trading the mouseout for mouseleave with the code I said.

  • Check it out: https://jsfiddle.net/96z8dp3n/

  • this is my : https://jsfiddle.net/albertobrasilbr/jxq7muat/1/

Show 2 more comments

1 answer

0


Using the option {once: true} indicates that the event will be triggered at most 1 time before it is automatically removed (documentation):

obj.addEventListener(evt, fn, {once : true});

Unfortunately IE11 does not support, but is supported by 93% of browsers in Brazil and 86% globally (see in Can I Use).

  • It worked ! It worked ! When you signal with Once : true (only that time) it will actually run only that time. The instruction is stored in some browser cache ? I ask you this, because after performing the function this one time, when leaving with the mouse from the browser window, the action will not actually repeat itself, but I do not know if there is some resquicio in the cache or not. Thanks for the help of the instruction ! https://jsfiddle.net/albertobrasilbr/jxq7muat/17/

  • I can’t say, but I think the JS destroys the event.

  • Thanks for the help !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.