katie hudson
katie hudson

Reputation: 2893

JQuery - replace image on hover

I have an images folder. Within this folder, I have several images of two types; one png and one gif. The displayed image is the png version. On an image hover, I need to replace it with its gif version. And when hover is out, put the png version back in place.

I currently have the following which works

$(".image-container").mouseover(function () {
    var imgName = $(this).find('img').attr('src');
    var img2 = imgName.substring(0, imgName.lastIndexOf("."));
    $(this).find('img').attr("src", img2+".gif");
}).mouseout(function () {
    var imgName = $(this).find('img').attr('src');
    var img2 = imgName.substring(0, imgName.lastIndexOf("."));
    $(this).find('img').attr("src", img2+".png");
});

It works, but I dont like the way I am repeating things. Is there any way to make this more efficient?

Thanks

Upvotes: 5

Views: 21581

Answers (5)

choz
choz

Reputation: 17858

Just another solution,

You can also use event handler delegation and pass parameters to it.

E.g.

function replaceImageHandler(ev)
{
    var $img = $(this).find('img');
    var imgSrc = $img.attr('src');
    var toImg = imgSrc.substring(0, imgSrc.lastIndexOf(".")) + '.' + ev.data.ext;
    $img.attr('src', toImg)
}

$(".image-container")
    .on('mouseover', { ext: 'gif'}, replaceImageHandler)
    .on('mouseout', { ext: 'png'}, replaceImageHandler);

You can also operate this if you want to attach more event handlers.

E.g.

function replaceImageHandler(ev)
{
    var $img = $(this).find('img');
    $img.attr('src', ev.data.src);
}

$(".image-container")
    .on('click', { src: 'zoom-my-image.gif'}, replaceImageHandler)
    .on('mouseover', { src: 'my-image-1.gif'}, replaceImageHandler)
    .on('mouseout', { src: 'my-image-2.png'}, replaceImageHandler);

Upvotes: 1

guest271314
guest271314

Reputation: 1

Use css :hover

.image-container {
  display:block;
  width: 50px;
  height: 50px;
  background: url(http://lorempixel.com/50/50/nature)
              , url(http://lorempixel.com/50/50/cats);
  background-size: 100% 100%, 0% 0%;
}

.image-container:hover {
  background-size: 0% 0%, 100% 100%;
}
<div class="image-container"></div>

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

I would say, use data-* attributes in a better way. I would suggest you to have something like this:

<img src="img.png" data-src="img.png" data-hover="img.gif" alt="" class="image-container" />

And in the jQuery:

$(".image-container").mouseover(function () {
  $(this).attr('src', $(this).data("hover"));
}).mouseout(function () {
  $(this).attr('src', $(this).data("src"));
});

Or in short, you can also use .replace(). You don't need regex for this simple replacement:

$(".image-container").mouseover(function (e) {    
    $(this).attr("src", $(this).attr("src").replace(".png", ".gif"));
}).mouseout(function (e) {
    $(this).attr("src", $(this).attr("src").replace(".gif", ".png"));
});

Upvotes: 20

Matt
Matt

Reputation: 44058

If you expand your selector to include img you can just use $(this) to reference it. And if you're just replacing gif with png (and vice-versa), it is easy to do that with regex.

$(".image-container img").mouseover(function (e) {    
    $(this).attr('src', $(this).attr('src').replace(/\.png$/, '.gif'));
}).mouseout(function (e) {
    $(this).attr('src', $(this).attr('src').replace(/\.gif$/, '.png'));
});

Edit: Please also see Praveen Kumar's answer as well for a great suggestion of declaring filenames in data- attributes.

Upvotes: 8

Raki
Raki

Reputation: 535

better way to implement this through css

.image-container:hover{
    url: imagepath;

  }

Upvotes: -2

Related Questions