Any advantages of hovering over mouse cursor in jQuery?

I am new to jQuery API. I use mouseover, but I have never used hover before. So I wonder if hover should be used instead.

+7
jquery
source share
5 answers

Well .hover() binds the two handlers for the mouseenter and mouseleave , so this is a more convenient way and also easier to understand the purpose.

mouseenter is different from mouseover because mouseenter not if the over / cursor introduces a child of the element to which the handler is bound.
It runs only once when cursors enter an element. mouseover always starts, even if the cursor is over a child.

The best way to see the difference is to look at the mouseleave() example.

In addition, mouseover and mouseout are real JavaScript events, while mouseenter and mouseleave are events provided by jQuery (afaik).

In the end, it depends on what you want to achieve. There is no right or wrong, and all these methods have their purpose. If you do not show some code, there is nothing to say.


If you mean :hover in CSS, and you can achieve the desired effect with it, go for it. If there is a solution other than JS for any problem, always select it.

+10
source share

hover just eliminates the need to execute both mouseenter and mouseleave , performing both functions in the same function.

+2
source share

I prefer to use css hover because it seems like I'm writing less code to do the same.

+1
source share

They are the same, except that the hover processes both mouseenter and mouseleave

See: http://api.jquery.com/hover/

So you can use it like this

 $('selector').hover(function () { // Do stuff on mouse enter }, function () { // Do other stuff on mouse leave } ) 
0
source share

As you study, here is another equivalent to using freezes:

 $('selector').bind('mouseenter mouseleave', function(){ if (event.type == "mouseenter") { // MouseEnter code } else { // MouseLeave code } }) 
0
source share

All Articles