Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery hover once?

Tags:

jquery

What's the jquery way to make hover function execute once and then stop?

.one() didnt work..

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

thank you

like image 872
Stewie Griffin Avatar asked Feb 16 '11 23:02

Stewie Griffin


People also ask

Is hover deprecated in jQuery?

hover() is deprecated #66.

How can make hover effect in jQuery?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector). hover(Function_in, Function_out);

What is the jQuery equivalent of Onmouseover?

jQuery mouseover() Method Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element. The mouseenter event is only triggered when the mouse pointer enters the selected element. See the example at the end of the page for a demonstration.


1 Answers

Hover binds the handlers for Mouse Enter and Mouse Leave event and is not an event on its own. Hence, in order to have the same effect as Hover, you will have two binds that will trigger once.

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

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

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

The other option would be to use Hover and then unbind it once you are done.

$('.button-color-2').hover(function() {
dosmth();
$(this).unbind('mouseenter mouseleave')
});
like image 176
Piyush Mattoo Avatar answered Sep 21 '22 01:09

Piyush Mattoo