tags:

views:

216

answers:

4

Hi

I have a list in a div and I would like to change the background image of the parent div (#homepage_container) when I hover over a list item.

here's the site:-

http://www.thebalancedbody.ca/

Is this possible? I'm guessing I'll have to use javascript.

Thanks

Jonathan

+1  A: 

You have to use JS. Better to learn something like jQuery. With it you will have to do something like

var images = ['img1.jpg', 'img2.jpg', ...]

for (var i = 0; i < li_count; ++i) // li_count is the number of li's
    $('li:eq(' + i + ')').mouseover(function() {$('#homepage_container').css('background-image', images[i]})

Anyway, if you wish to use such kinds of techniques, you have to learn JS. See http://www.w3schools.com/js/default.asp and for basics and http://docs.jquery.com/Tutorials for jQuery.

Guard
Why do JQuery when this is simple with pure javascript?
Joel Potter
That's what I'm looking for but not sure how to implement this so that each list item hover shows a different background. Is that all I'll need at the top once I've loaded jquery, I won't need anything in the list item itself in terms of JS?
Jonathan Lyon
> Why do JQuery when this is simple with pure javascript?Because pure JS DOM manipulation is ugly, while jQuery is nice, simple and powerful> That's what I'm looking for but not sure how to implement this so that each list item hover shows a different background.You can use some kind of array with images in the same order your `li`s are, see updated answer.
Guard
@Jonathan Lyon: yes, it's all you'll need (this edited version). But you'll be using a strong but heavy tool for something small and simple. Also, keeping the hovered-image synchronized with the right `li` might be confusing when changing the menu. (+0)
ANeves
@ANeves, the tool is not heavy, it's lightweight and easy to learn %)
Guard
@Guard, I somehow disagree. He only really needs to attach a function to the `hover` of each `li`, and send an image url to that function. That function can then get `#homepage_container` and change it's `background-image` to whatever the function received as argument. I find this is pretty light in comparison to using JQuery. Which is a mighty tool, and greatly simplifies the medium or advanced use of JavaScript, yes! But for this case, I believe it is overkill.
ANeves
+1  A: 

This is quite simple with pure javascript.

function changeBg(newBg)
{
    var imgdiv = document.getElementById("divwithbackground");
    imgdiv.style.backgroundImage = "url(" + newBg + ")";
}

Or using sprites:

imgdiv.style.backgroundPosition = "new position";

This can be executed on mouseover for any of your li's. Event registration in javascript can be done many ways, but to do it in script, I recommend QuirksMode's method here.

Something like:

function addEventSimple(obj,evt,fn) {
    if (obj.addEventListener)
        obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent)
        obj.attachEvent('on'+evt,fn);
}

And on load:

// get the list items
var ul = document.getElementById("ulId");
var lis = ul.getElementsByTagName("li");

// add event handlers
for (var i = 0; i < lis.length; i++)
{
    addEventSimple(lis[i], "mouseover", (function(j) {
        return function() { 
            // get your background image from the li somehow
            changeBg(lis[j].id + "_bg.png");
        };
    })(i)); // use a closure to capture the current value of "i"
}
Joel Potter
@Joel Potter, enclose it in a function that receives the image location, and show an example on how to set the function on two `li` so that the parent div gets different backgrounds when hovering each of the divs - and that'll answer OP's question.
ANeves
@ANeves: Better?
Joel Potter
+1: yes. I'd have put, on every li, something like `<li onhover="changeBG('wallaby.png')" ...` instead - but not being very wise in JavaScript, I'll trust your approach to be better.
ANeves
That's valid too. I just like to keep everything in script instead of mixing it into the html.
Joel Potter
A: 

If you don't care about IE6, you could do it with CSS:

#homepage_container { background: url(normal.png); }
#homepage_container:hover { background: url(hover.png); }
Oblio
This isn't the same. The OP wants to change the background of a _different_ element on hover, and `#homepage_container` is not inside the `li`'s being hovered.
Joel Potter
Indeed, I guess I should get some sleep. Thanks! :)
Oblio
-1: fix or delete, please, if you know it not to be an answer.
ANeves
A: 

I suggest installing the Jquery Library for this (jquery.com)

Just add this to your header:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;

<script type="text/javascript">

      $(document).ready(function() {

          $('li').hover(function(){

         $('#homepage_container').css('background-image' : 'whatever.png');

            }

      });
</script>
Needs different bgs for different `li`.
ANeves