I have a few images and their rollover images. Using jQuery, I want to show/hide the rollover image when the onmousemove/onmouseout event happen. All my image names follow the same pattern, like this:
Original Image:
Image.gif
Rollover Image:
Imageover.gif
I want to insert and remove the "over" portion of image source in the onmouseover and onmouseout event, respectively.
How can I do it using jQuery?
A generic solution that doesn't limit you to "this image" and "that image" only may be to add the 'onmouseover' and 'onmouseout' tags to the HTML code itself.
HTML
JavaScript
Depending on your setup, maybe something like this would work better (and requires less HTML modification).
HTML
JavaScript / jQuery
Adapted from Richard Ayotte's code - To target an img in a ul/li list (found via wrapper div class here), something like this:
To set up on ready:
I've made something like the following code :)
It works only, when you have a second file named with _hover, for example,
facebook.png
andfacebook_hover.png
If the solution you are looking for is for an animated button, then the best you can do to improve in performance is the combination of sprites and CSS. A sprite is a huge image that contains all the images from your site (header, logo, buttons, and all decorations you have). Each image you have uses an HTTP request, and the more HTTP requests the more time it will take to load.
The
0px 0px
coordinates will be the left upper corner from your sprites.But if you are developing some photo album with Ajax or something like that, then JavaScript (or any framework) is the best.
Have fun!