Jquery hover once?

What jquery way to create a hover function is executed once and then stopped?

.one () does not work.

$(".button-color-2").hover(
  function (){
   dosmth(); 
});

Thank you

+5
source share
4 answers

Hover binds handlers for Mouse Enter and Mouse Leave and is not an event in itself. Therefore, in order to have the same effect as Hover, you will have two bindings that will fire once.

$(".button-color-2").one("mouseenter mouseleave", function(e){
dosmth();
});

If you want to do different things on mouseenter and mouseleave, then link two different handlers

$(".button-color-2").one("mouseenter", function(e){
dosmth1();
}).one("mouseleave", function(e){
dosmth2();
});

Another option is to use a hover and then undo it as soon as you are done.

$('.button-color-2').hover(function() {
dosmth();
$(this).unbind('mouseenter mouseleave')
});
+12
source

If this did not work:

$(".button-color-2").one('hover', function() {
    dosmth();
});

, , :

$('.button-color-2').bind('hover', function(event) {
  dosmth();
  $(this).unbind(event);
});
+1

.one(), .

hover, . $.one() :

$('.button-color-2').one('hover', function()
      dosth();
);
0

, . , , , if, , 1. 1, , ( ). mouseleave reset 0. , :

    $("#mydiv").mouseenter(function(){
        if (counter < 1){
            $(this).effect("pulsate", { times:1 }, 300);
            counter = counter + 1;
            return;
        }
        else{
            return;
        }
    });
    $("#mydiv").mouseleave(function(){
        counter = 0;
        return;
    });

, : http://webpages.marshall.edu/~glasser1/jquery.html

, .

edit: I forgot to add that you need to initialize your variable counter outside the function so that it is global and set to zero.

0
source

All Articles