1

I'm trying to change a div background image on hover, I want to do this with JaveScript so that it works cross browser without any issues. The current code I have is:

<div class="staff" style="background-image: url(/wp-content/uploads/2013/08/IMG_1828- 300x237.png);" onmouseover="this.style.background=url(wp-content/uploads/2013/08/IMG_1836-v2-300x237.png);" onmouseout="this.style.background=url(wp-content/uploads/2013/08/IMG_1828-300x237.png);">
</div>

CSS:

.staff{
width: 300px;
height: 237px;
}

Can anybody see what is causing the problem?

5
  • try wrapping url(wp-content/uploads/2013/08/IMG_1836-v2-300x237.png) in single quotes. Commented Aug 7, 2013 at 10:23
  • 1
    Why don't you use css background? that will also be cross browser. Commented Aug 7, 2013 at 10:24
  • You need to wrap your url in single quotes ' ' since it's a string literal in JS Commented Aug 7, 2013 at 10:26
  • 1
    Is there any browser that doesn't support :hover in css (click for a fiddle)? Commented Aug 7, 2013 at 10:27
  • Sorry AnaMaria where would this filter be placed? Commented Aug 7, 2013 at 11:30

8 Answers 8

1
<style>
  .staff{
      width: 300px;
      height: 237px;
      background-image: url(/wp-content/uploads/2013/08/IMG_1828- 300x237.png);
  }

  .staff:hover {
      background-image: url(wp-content/uploads/2013/08/IMG_1836-v2-300x237.png);
  }
</style>
<div class="staff"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

How would you animate CSS while keeping it cross browser compatible? So it fades in/out?
1

You can use simple css too.. and it will work on all browsers

.staff { background: url("url/img.png")}
.staff:hover { background: url("url/hoverimg.png")}

<div class="staff"></div>

Comments

1

Better use CSS than inline CSS and javascript to achieve the effect.

If you still want to do it inline, here is a example.

jsfiddle

onmouseover="javascript: this.style.backgroundImage = 'url(\'image_url\')'";

2 Comments

Perfect works well thanks, do you know how would I add a fade effect to this?
I recommend you to use jquery for the fade effect. It's a javascript library that allows you to do such effects. Better than code it for yourself. Search for fade effects with jquery. Hope it helps.
1

you can use css like this:

.staff{
width: 300px;
height: 237px;
background-image: url("/wp-content/uploads/2013/08/IMG_1828- 300x237.png");
}
.staff:hover{
background-image: url("wp-content/uploads/2013/08/IMG_1836-v2-300x237.png");
}

or javascript like: onmouseover="javascript: this.style.backgroundImage = 'url(\'image_url\')'";

Comments

0

WORKING DEMO

CHANGE I MADE

 onmouseover="this.style.background='url()';"

You have to enclose the url in single qoutes...

I would also suggest you use CSS rather than JS to do this. But since you asked how to do it in JS, this is your answer

Comments

0

Most browser should support :hover in css, so there is no need to use JavaScript for this (relevant fiddle here):

.staff{
    width: 300px;
    height: 237px;
    background-image: url("/wp-content/uploads/2013/08/IMG_1828- 300x237.png");
}
.staff:hover{
    background-image: url("wp-content/uploads/2013/08/IMG_1836-v2-300x237.png");
}

If you really want to use JavaScript, than you have to wrap url(...) in quotes like so:

<div class="staff"
  style="background-image: url(/wp-content/uploads/2013/08/IMG_1828- 300x237.png);"
  onmouseover="this.style.background='url(wp-content/uploads/2013/08/IMG_1836-v2-300x237.png)';"
  onmouseout="this.style.background='url(wp-content/uploads/2013/08/IMG_1828-300x237.png);'">
</div>

Comments

0

I think you can use this script :

 $('.staff').mouseover(function() {
        $(this).attr('style',"background:grey;");
    })
    $('.staff').mouseout(function() {
        $(this).attr('style',"background:white;");
    })

Change the color to the image path.

Comments

-2
   $(document).ready(function(){
  var staff = $(".staff"); //Avoid repeated traverse

   $("img").hover(function(){
   staff.css("background",'url(' + $(this).attr('src') + ')'); 

      //Assuming you want to set current image as background image
});

    $( "img" ).mouseout(function(){
        staff.css("background","none");  
    });
   });

Okay this works for me so when you hover over an image the background of the set div is changed. So when ever you hover over an image .staff background is changed. I believe that is what you want?

3 Comments

-1 for giving a jquery answer. Jquery is not tagged and the OP did not ask for it
But javascript was tagged and Jquery is only essentially a library for javascript is it not?
Going by you logic anybody who know JavaScript should know know Jquery. However in the real world it does not work that way.

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.