2

Maybe an easy question, but what is good practice in Rails when you want to change an image when the mouse hover over the image (the image is not a link, just an image)?

allegutta
  • 5,626
  • 10
  • 38
  • 56
  • 2
    There are multiple possible ways, from using the CSS `:hover` selector to Javascript. None of them involve your backend (Rails), though — it's all happening on the client side in the browser. – janfoeh Feb 17 '15 at 13:32
  • I think you should just jquery just change the image url when hover the mouse over image, you should add second image url in data-attr of the image and change url on hovring – Thorin Feb 17 '15 at 13:33
  • @Ansar Could you sketch up a rough example on how you would do that? – allegutta Feb 17 '15 at 13:41
  • I have added a jsfiddle Look at that – Thorin Feb 17 '15 at 14:04
  • 2
    Much better to just do it with css rather than than javascript. – Max Williams Feb 17 '15 at 14:33
  • @Max Williams Would you do that by changing `background` in CSS on `hover`? – allegutta Feb 17 '15 at 15:07
  • Yeah. I would also make it not an img tag in the first place, instead making it a div and doing the standard and hover images with css. – Max Williams Feb 17 '15 at 15:09
  • If you have a lot of these, with dynamically generated image urls, you can still do it with divs, and use style tags to set the normal and hover images. This starts to get a bit html-heavy though. – Max Williams Feb 17 '15 at 15:10
  • Interesting, so it is better to do this with the use of a `
    ` than the `image_tag` in rails?
    – allegutta Feb 17 '15 at 15:14
  • 2
    Not necessarily, it's a complicated question. See http://stackoverflow.com/questions/492809/when-to-use-img-vs-css-background-image and http://stackoverflow.com/questions/17090853/div-background-image-or-using-img-tag The short version is probably "If the image is content (eg that someone has uploaded) then use an img tag, if it's just for the look and feel of the website then use css" – Max Williams Feb 17 '15 at 15:19
  • @Max Williams Thanks, I went with the `
    ` solution. Thanks for great links. If you make it an answer, I will accept it.
    – allegutta Feb 17 '15 at 18:48

4 Answers4

2

Try this :

HTML:

<img id="demo_image" src="smiley.gif" alt="Smiley face" width="42" height="42" onmouseover="changeImage(this)" onmouseout="changeImage(this)" data-image2="http://mw2.google.com/mw-panoramio/photos/small/17287086.jpg">

JS:

function changeImage(img) {
    img = $(img);
   var in_url = img.attr('src'),
     out_url =  img.attr('data-image2');
   img.attr('src', out_url).attr('data-image2', in_url);
}

http://jsfiddle.net/X3zEq/9/

Thorin
  • 2,004
  • 1
  • 19
  • 30
2

There are two ways to do this:

1) Use an img tag, and use javascript to change the img src attribute on hover

  • Pros - images will print out or display in other situations where the stylesheet isn't used
  • Cons - requires javascript, which adds complexity to your system, slows the page down, and may not work in some contexts. If some content is reloaded eg with ajax, you'll need to make sure the events are reapplied or whatever.

2) Use a div (or other non-img element), and show the image via the background property, set in your css. You can then add an additional :hover rule to show the alternate image on hover.

  • Pros - everything done with css. Quick and guaranteed to work even after partial reload of the page.
  • Cons - won't print, unless you use that stylesheet to print as well. Harder to do if the image src is driven from your database.

Note 1) - you can't do a "hybrid" approach of using css to change the background of the img tag, as the background of an image tag is behind the image, and so can't be seen: you'd be changing the "layer" behind the image

Note 2) - if you use js with an img tag, put the alternate image path in a data attribute, eg "data-hover-src="/images/pic2.png". Then you can manage it with a simple standard javascript function.

Note 3) - if the image src is database-generated (eg it's for an image a user has uploaded), you can still do it by css, but you need to do it by putting the css into a style attribute on the element.

The decision on which path to go down is a little complicated, but could be boiled down to "if the images are content, eg that users have uploaded, use an img tag. If the images are part of the look and feel of your website, use css". However, you can use either approach in either situation with a bit of work.

See also: When to use IMG vs. CSS background-image? and Div Background Image or Using IMG Tag

Community
  • 1
  • 1
Max Williams
  • 32,435
  • 31
  • 130
  • 197
0

Let's assume this is your a tag, add a class and an id(if needed) to it

<%= image_tag "xyz.jpg", class: "change-image" %>

Now in js file

$(".change-image").hover(function(){
  $(this).attr("src") = // url of the new image
});
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
0

Lets there is an image

 <%= image_tag "image.jpg", id: "img" %>

Now in js using jQuery

$(function() {
  $("#img")
    .mouseover(function() { 
       var src = $(this).attr("src").replace("image.jpg", "over.jpg");
       $(this).attr("src", src);
    })
    .mouseout(function() {
        var src = $(this).attr("src").replace("over.jpg", "image.jpg");
       $(this).attr("src", src);

    });
 });
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74