views:

466

answers:

3

Hi,

You know when you use the CSS background-position property for rollovers, right?

Roll over element 1, the a:hover property changes background to position-x/y
Roll over element 2, the a:hover property changes background to position-x/y
And so forth.

What I need to achieve this time, is:

(lets say its 4 list elements we have)

Roll over li 1, background on li 2, 3 and 4 changes.
Roll over li 2, background on li 2, 3 and 4 changes.
Roll over li 3, background on li 1, 2,and 4 changes.
And so forth...

Now, I have thought long and hard and Im pretty sure in thinking that this CANT be done with CSS and I will need to look at using jquery, am I correct?

Thanks in advance.
Rob

A: 

Yes you are correct

Tim Büthe
No he's not. It can be done using CSS. See my answer for proof. It relies upon using `:hover` on the parent element.
Eric
Well, your example looks promising, but how would a IE6 example look like?
Tim Büthe
+1  A: 

Assuming that you mean that the non-hovered lis have the same background:

jQuery

Here's some sample jquery code that ought to work. Just add the class "nav" to the <li>s

$(document).ready(function()
{
    $("li.nav").hover(
    function()
    {
     $("li.nav").css("background-image","url(newimage)");
     $(this).css("background-image","url(oldimage)"); 
    },
    function()
    {
     $("li.nav").css("background-image","url(oldimage)");
    });
});

CSS

However, it is possible using CSS:

HTML:

<ul class="nav">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
</ul>

CSS:

ul.nav:hover li
{
    background-image: newimage;
}
ul.nav li:hover
{
    background-image: oldimage;
}

Remember, IE only supports :hover on <a> tags in quirks mode.

Eric
Thank you. I have solved the problem by using a variation of the above.I put the ul inside a wrapping (relative) div and gave that the background and a :hover attribute. Then, I set the li's as block elements and positioned them (absolutely). When you hover over each li(a) the image rolls over as expected and the background on the rest appears the same.It doesn't quite work in IE6, but enough for the page to be navigational.Is it possible for me to link the url of the the final result once its online? to serve as reference to any future visitors to the question.
prevailrob
Do you mean "is it allowed", or "how do you do it"?
Eric
Is it allowed. I suppose it can be posted as a comment to the original question?
prevailrob
Or you can edit the original question. Add "The solved problem can be found here" or something to the bottom. Or add it as a comment to my answer, and delete your older comments. I'll delete this if you do.
Eric
A: 

Correct. For example, with the following HTML (assuming you want different offsets for each element, hence giving them all an id):

<ul>
 <li id="foo">Item 1</li>
 <li id="bar">Item 2</li>
 <li id="baz">Item 3</li>
 <li id="yup">Item 4</li>
</ul>

you can do:

$(document).ready( function() {
    $('#foo').mouseenter( function() {
        $('#foo').css( 'background-position-y', '20px' );
            ...
    })

    $('#foo').mouseleave( function() {
        $('#foo').css( 'background-position-y', '0px' );
        ...
    })
});
Ölbaum
It can be simplified if you are doing something more systematic, like if hovering the first `<li>` offsets all backgrounds by `20px`, the second by `40px`, etc.
Ölbaum