Alessandro Dotti Contra

Linux/Unix DevOps

JQuery image rollover

This is a simple javascript snippet to create an image rollover. Whenever the mouse pointer hovers on the specific image, the original image is replaced with a new image. This is handy if you like to achive a black and white to color image transformation effect.

HTML markup

	alt="image description"

We use the class rollover for every image that needs to be rolled. The hover attribute is used to define the image to roll upon hover event.

Code snippet

The core of this effect is the swapImage() function, defined as follow:

function swapImage(image) {
	var current = $(image).attr('src');
	$(image).attr('src', $(image).attr('hover'));
	$(image).attr('hover', current);

The function will then be called in response to the hover event of the relevant images:

<script type="text/javascript">
$(document).ready(function() {
	$('.rollover').hover(function() {
	}, function() {
View demo