0

I want to unbind the mouseover event when the element is clicked.

HTML markup:

<div id="foto"></div>
<div id="flotante"></div>
<div id="flotante2"></div>    

jQuery code:

$('#foto').on({
    mouseover: function(){
        $('#flotante').delay(2000).show('slow');
    },
    mouseleave: function(){
        $('#flotante').hide('slow');
    },
    click: function(){
        // I want the mouseover event to stop executing.        
         $('#flotante2').show();
    }
});

You can see an example here: http://jsfiddle.net/K85DN/

Thanks a lot for your help

4
  • $('#foto').off('mouseover') inside the click callback ? Commented Nov 6, 2013 at 3:48
  • I tried it and it didn't work. The mouseover event still fires. Commented Nov 6, 2013 at 3:52
  • jsfiddle.net/K85DN/1 working well for me Commented Nov 6, 2013 at 3:57
  • jsfiddle.net/K85DN/2 Commented Nov 6, 2013 at 3:59

1 Answer 1

1

Some changes

  1. Use namespaced event handlers since we need to remove them.
  2. Use setTimeout() to delay the display since we will have to cancel it if the mouse leaves the element before 2 seconds
  3. Use .stop() to handle the animation queue

Try

var $flotante = $('#flotante');
$('#foto').on({
    'mouseover.display': function () {
        var timer = setTimeout(function () {
            $flotante.stop(true, true).show('slow');
        }, 2000)
        $flotante.data('timer', timer);
    },
    'mouseleave.display': function () {
        clearTimeout($flotante.data('timer'))
        $flotante.stop(true, true).hide('slow');
    },
    click: function () {
        $(this).off('mouseover.display mouseleave.display');
        clearTimeout($flotante.data('timer'));
        $flotante.stop(true, true).hide('slow');
        $('#flotante2').show();
    }
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

@RaulEscarcega it is fixed now... try the demo again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.