views:

247

answers:

3

I'm trying to make an image change depending on the list-item you hover on.

I have a container containing an ul. I have three list-items (a small horizontal navigation). The list items are sitting on the bottom of the container which leaves an empty space on top of the li's. I want the empty space above the li's to change images depending on which list-item I hover over. There will be 3 different images or maybe using css sprites.

Is there any way I can make this happen with jQuery?

A: 

Perhaps this question can help you.

Can I Improve this Jquery Image Replace Code [Stack OverFlow]

Fusspawn
A: 

You could use the jQuery UI Tabs plugin for this. May be a little overkill but it would work. You can change the event used for selecting a tab to be on hover instead, like this:

$('.selector').tabs({ event: 'mouseover' });

Here's a super quick demo that might help (I've tested and it works), though obviously you would want to use your own CSS:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
    <title>Tabs Test</title>
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/themes/redmond/jquery-ui.css" type="text/css" media="screen" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/jquery-ui.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
     //<![CDATA[
     $(function(){
      $('#tabs').tabs({ event: 'mouseover' });
     });
     //]]>
    </script>
</head>
<body>
    <div id="tabs">
     <div id="tabs-1">
      <img src="images/image1.jpg" alt="image1" />
     </div>
     <div id="tabs-2">
      <img src="images/image2.jpg" alt="image2" />
     </div>
     <div id="tabs-3">
      <img src="images/image3.jpg" alt="image3" />
     </div>
     <ul>
      <li><a href="#tabs-1">Nunc tincidunt</a></li>
      <li><a href="#tabs-2">Proin dolor</a></li>
      <li><a href="#tabs-3">Aenean lacinia</a></li>
     </ul>
    </div>
</body>
</html>
Jason Berry
redsquare
Because it's basically the same functionality (hovering over list items to hide/show related content), and if he's not especially knowledgeable it would be an easy implmentation (especially if he's already got the jQuery UI running on the site).Of course I don't really know his exact circumstances given that he hasn't posted any code. There's a hundred different ways he could do it, this is just one.Depending on what he actually wants me preferred method would be a pure CSS solution.
Jason Berry
+1  A: 

HTML:

<div id="container">
    <img id="yourImg" src="spacer.gif" width="..." height="...">
    <ul id="yourUL">
        <li data-imageswap="someimg.png">One</li>
        <li data-imageswap="another.png">Two</li>
        <li data-imageswap="andathird.png">Three</li>
    </ul>
 </div>

Script:

$('li','#yourUL').hover(function(){
    $('#yourImg').attr('src',this.getAttribute('data-imageswap'));
}

You could use something other than that data attribute (like a className or an id) to point to your image, but that should be the gist of it.

Sticking a transparent, 1x1 gif in your image to start will let you scale it to the width and height you need. Alternately, you could start with no image and write it into the DOM if you'd like to start out without it.

ajm