4

I am very new to JQuery but already like it a lot.

I am trying to change a background of a div when the user rollsovers it. But I can not get the bg to update. So far this is what I have:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#fish{background-image:url(images/fish_off.jpg);width:459px;height:474px;}
</style>

 <script type="text/javascript" src="javascript/jquery.js"></script>     

 <script type="text/javascript">                                         
$('#fish').hover(
  function(){$('#fish').css({background: "url(images/fish_on.jpg)"})},
  function(){$('#fish').css({background: "url(images/fish_off.jpg)"})}
);


 </script>      

</head>

<body>

<div id="fish"></div>

</body>
</html>

Here is my test page and based off the info below I am still having issues:

Test Page

4 Answers 4

5

use .mouseenter() or if you want to apply a class on mouseout as well use .hover() http://api.jquery.com/mouseenter/ http://api.jquery.com/hover/

$("div").hover(
  function () {
      $(this).css("background", "#ddd");
  }, 
  function () {
      $(this).css("background", "#ccc");
  }
);

you can also do this purely through css if you just want a rollver

    #fish { background: #fff; 
    #fish:hover { background: #ccc; }

check out this working fiddle on using hover http://jsfiddle.net/faLdY/

Applying background images via jquery is slightly different checkout this similar question Switching a DIV background image with jQuery

note the way the css is applied in your example page you are doing it incorrectly.

Change

$('#fish').hover(
  function(){$('#fish').css({background: "url(images/fish_on.jpg)"})},
  function(){$('#fish').css({background: "url(images/fish_off.jpg)"})}
);

to this

$('#fish').hover(
  function(){$('#fish').css("background-image", "url(images/fish_on.jpg)")},
  function(){$('#fish').css("background-image", "url(images/fish_off.jpg)")}
);

EDIT:

Here is a working fiddle doing exactly what you asked http://jsfiddle.net/C7KxR/

this is linked directly to your images, btw. I hope thats alright.

Sign up to request clarification or add additional context in comments.

7 Comments

I would normally use css but I am also going to show a hidden div figured I would do them both at the same time
fair enough. If you want to show a hidden div perhaps look into toggle, or slideToggle() or fadeToggle() and also the jquery-ui library if you want more control over the animation.
I applied your feedback but am still not seeing the change. I added a link above to the page in my post. Not sure what I am doing wrong?
@Denoteone I think your problem is with the way you are applying the .css, I have updated my answer after reviewing your test page.
Thank you for all your help. Could the issue have to do with my reference to the jquery library?
|
1

change

$('#fish').click(function()
{
  $('#fish').css("background-image", "url(images/fish_on.jpg)");  
});

to

$('#fish').hover(function()
{
  $('#fish').css("background-image", "url(images/fish_on.jpg)");  
}, function()
{
  $('#fish').css("background-image", "url(images/fish_ooff.jpg)");  
});

3 Comments

Just so I under stand the second function() is to swap the image back to normal...correct?
Yes. .hover() takes 2 functions, one on mouseenter, the second on mouseleave. see api.jquery.com/hover
Yes, that's correct. Hover is just mainly an alias for mouseenter and mouseleave.
1
$('#fish').hover(
  function(){$('#fish').css({background: "url(images/fish_on.jpg)"})},
  function(){$('#fish').css({background: "url(images/fish_off.jpg)"})}
);

You want to use .hover which takes two callbacks. One for mousein and one for mouseout.

Comments

0

you need mouseover event:

$('#fish').mouseover(...

Comments

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.