5

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

2
  • 3
    Have a peek at Code Review (but be careful to check their FAQ for what's on topic though). Commented Jan 13, 2016 at 13:25
  • Use css classes instead to change background url - and add/remove class on hover, much cleaner and easier to manage imo Commented Jan 13, 2016 at 13:30

5 Answers 5

21

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"));
});
Sign up to request clarification or add additional context in comments.

3 Comments

+1 The first part of this answer is better architecture in general. The less magic assumptions in your code, the better.
@Matt Thanks buddy. :) And you were lightning fast and votes were too, before I answered. I got a stupid phone call. LoL.
@Matt Oh! How stupid of me to miss that! Thanks for the edit.
8

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.

Comments

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>

Comments

1

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);

Comments

-2

better way to implement this through css

.image-container:hover{
    url: imagepath;

  }

2 Comments

What about dynamic images, say 100 images. And url:? Does it even work?
Man, are you sure you need the wrong answer in here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.