MicroTut: The jQuery Hover Method

In: tutorial

27 Feb 2010

When building a navigation menu, or any other jQuery script, it is often necessary to have a robust method with which to define a mouse over and mouse out state. This is where the hover() method comes along. Here is how it is used:

$('.selectorClass').hover(
function(){
	$(this).stop().fadeTo('slow',0.4);
},
function(){
	$(this).stop().fadeTo('slow',1);
});

This assigns the first function to be executed when the mouse moves above the elements on the page, which share the selectorClass class name, and the second one is executed when the mouse moves away.

You can use “this” inside of the functions, to access the element that triggered the event.

Hover actually binds the first function to the mouseenter event, and the second one to mouseleave, so you could alternatively write this:

$('.selectorClass').mouseenter(function(){
	$(this).stop().fadeTo('slow',0.4);
}).mouseleave(function(){
	$(this).stop().fadeTo('slow',1);
});

As of version 1.4 of jQuery, it is now possible to pass a single function to hover, which will be called on both mouseenter and mouseleave. This way you can shorten your code even more by writing only one function:

$('.selectorClass').hover(function(){
	this.check = this.check || 1;
	$(this).stop().fadeTo('slow',this.check++%2==0 ? 1 : 0.4);
});

The example above increments this.check every time the function is run. Depending on whether the number is even or not, it chooses the opacity level to pass to fadeTo() (1 being completely visible).

This is also a great place to use the jQuery toggle functions like .slideToggle() and .toggleClass().

Go to Source

8 Responses to MicroTut: The jQuery Hover Method

Avatar

avomatic

March 10th, 2010 at 8:08 pm

most vehicles have increments at 100F , 180f, 220, and 240. vehicle should operate between 180 and 220 regardless of outside temperature.

Avatar

ELfaGeek

March 15th, 2010 at 1:37 pm

Assuming you use Internet Explorer, you can't.

Delete all temporary files and start over OR switch to Firefox.

Avatar

Pelagius

March 20th, 2010 at 8:09 am

I think it's prostitution – some forms of sex could definitely be handled within 6 minutes. I'm no expert, but that's what I think.

Avatar

Sam

March 22nd, 2010 at 6:31 pm

Search engines simply perform a WHOIS search. Try it yourself…

Avatar

Tenar

March 26th, 2010 at 4:09 pm

Divide all your pages into two column using tables. The left column could be 20% and the right column 80%. Copy the menu code and paste it the left columns of each page. If you have server side includes then simply include the menu file in the left column. Thats the only way to go about doing it without javascript or frames.

Avatar

SDD

May 1st, 2010 at 2:25 am

No. ATMs are generally set up to dispense only 20s.

Avatar

Kaytee

May 8th, 2010 at 7:16 am

It might help if you say what's wrong with it exactly.

Avatar

skyhawkscaw

May 15th, 2010 at 3:42 am

get a csi(crime invesgator) its not sinful
go the court system, and get checked

Comment Form

About this blog

This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.