A Simple Mouseover Hover Effect with jQuery

Posted on July 3, 2009  |  Category: Tutorials

jquery A Simple Mouseover Hover Effect with jQueryJavascript is pretty useful but it does demand a lot of coding. Luckily for all of us, there are libraries out there that make it extremely easy to use the power of Javascript with a lot less code. I have been having a lot of fun with jQuery latetly and wanted to share a quick and easy tutorial on how to create a mouseover hover effect.

First you need to download jQuery. Grab the Production minified version off of their site. You will also need two images. I used the two below. I named them button.png and button-hover.png.

bavota bw A Simple Mouseover Hover Effect with jQuerybavota A Simple Mouseover Hover Effect with jQuery

Now we can start to create the effect.

To make it all work, we first need to add the jQuery library script between the <head> tags.

<script type='text/javascript' src='http://yoursite.com/jquery.js'></script>

Now lets create the function that will make it all work.

<script>
$(document).ready(function(){
	$(".button").hover(function() {
		$(this).attr("src","button-hover.png");
			}, function() {
		$(this).attr("src","button.png");
	});
});
</script>

Last but not least, we need to add our image to the page.

<img src="button.png" alt="My button" class="button" />

The one thing you need to make sure is that the classname in the jQuery function (.button) matches the classname of the image tag (button).

With everything in place you should have a file that looks like this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery Hover Effect</title>
<script type='text/javascript' src='http://yoursite.com/jquery.js'></script>
<script>
$(document).ready(function(){
	$(".button").hover(function() {
		$(this).attr("src","button-hover.png");
			}, function() {
		$(this).attr("src","button.png");
	});
});
</script>
</head>

<body>
<img src="button.png" alt="My button" class="button" />
</body>
</html>

Try out the effect below.

bavota bw A Simple Mouseover Hover Effect with jQuery

NOTE: If you are using jQuery with WordPress, you need to replace all the dollar signs ($) with the word jQuery due to other Javascript libraries that use the dollar sign.


Tags: , , , , , , ,

Short URL: http://bavotasan.com/?p=725

22 Responses to “A Simple Mouseover Hover Effect with jQuery”

  1. Do you think that Java script is the right choice..? even though thank you for tip

    #3248
  2. Thanks for sharing this – it works great :) I’m curious though, when hovering over the initial grey button image would it be possible to replace it with some plain text (no image) rather than the red button image? Any help or pointers would be greatly appreciated. Many thanks :)

    #3276
  3. ignacio

    many many thanks ! It’s working perfectly !

    #3370
  4. This is just what I’ve been looking for. I’m code-phobic though, any idea on how to make the image into a link? Just <a href….?

    #3377
  5. Is that possible to make this work in IE6…?

    Thanks!

    #3511
  6. Hmm, javascript isn’t the best way to induce a hover effect, w3schools’ latest survey revealed 5% of people still browse the web with javascript turned off!

    Also, only ridiculously ancient browsers can’t render a basic css hover effect and if they are that old, they certainly won’t be able to render a jQuery .hover function!

    The best way to achieve the effect you want is definitely with a bit of CSS, like so:
    .button a, .button a:visited {width:89px;height:97px;background:(img/button.png);display:block}
    .button a:visited:hover, .button a:hover, .button a:active {background:(img/button_hover.png)}

    Then the (X)HTML;
    <div class="button"><a href="#"></a></div>

    Mat.
    http://www.matthewhill.name

    #3566
    • We’ve found that using CSS is preferable, but there are cases when the client wants the page to print just like it shows on-screen without extra steps by the user. In those cases, the CSS method won’t work because the buttons are using the background property and the majority of browsers default to “not print background images”.

      Maybe there is a CSS trick that I’m missing that addresses the printing issue, but I haven’t run across it.

      #7672
    • @Jeff,

      ever heard of @print css?

      #8230
  7. Web Hosting

    Very interesting post. I used CSS for hover effects. My result was compatible with all major browsers including IE6, but the loading time of the second picture was really big. I will try with this javascript. Thanks for your great post!

    #3615
  8. I wanted to use this post as an intro to show something simple. Next up is using jQuery to create a fade in and out effect.

    #3627
  9. Carl

    Hey, very interesting.
    what if we want to publish more than one button or image with thata effect, do we have to write a script function for every image?
    Thanks.

    #4048
  10. Thank you sooooo much for this! I needed something that wasn’t going to give me the ugly black background issue because of the filters issue – this is perfect! You rock :)

    #5841
  11. Louise

    I like the code, very simple. But, how can I add a setting to slow down the hover effect…it is instant right now and I’d like to see a bit more animation by slowing the transition between the 2 images?

    Louise

    #6079
  12. Kevin

    I am sorry but I can’t understand why you would use jQuery to perform a simple image replacement hover effect. You need to reference jquery and an external js file calling the effect. It makes no sense. Just do it with CSS. If a browser is not CSS compatible, do you really expect it be able to perform this effect with jQuery?

    #6193
  13. Wow, wonderful effect, thank you very much!

    #7742
  14. It´s working perfectly! Thx in advance. Long time searching for this effect – easy to handle

    #7831
  15. This can be done with css but using javascript is good method too

    #7932

Leave a Reply

To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.