tags:

views:

56

answers:

2

I have a <div> setup as such:

#menu_one
{
    height:50px;
    width:150px;
    background-color:#666666;
}
</style>

<div id="menu_one"></div>

What I want to do is have this <div> fade into a image that will be of the same size on mouse over then fade out when the mouse exits.

Would it be better to have a background image of the grey and then fade between the two images?

How would I accomplish this?

A: 

You create a hidden image somewhere. When the user mouses over the div, you position the image over it and fade it in.

Translating the previous sentence in jQuery code is left as an exercise for the reader :)

Kaze no Koe
+1  A: 

You can use absolute positioning and z-index:

<style>
#menu_one
{
    position: absolute;
    top: 0px;
    height:50px;
    width:150px;
    background-color:#666666;
    z-index: 0;
}
#imgDiv
{
    position: absolute;
    top: 0px;
    height:50px;
    width:150px;
    z-index: -1;
}
</style>

<script>
$(function(){
    $("#menu_one").mouseover(function() {
        $("#menu_one").fadeOut();
    });
    $("#imgDiv").mouseout(function() {
        $("#menu_one").fadeIn();
    });    
});
</script>

<div id="menu_one"></div>
<div id="imgDiv"><img src="image.png"/>
kgiannakakis
Your solution seems a lot simpler than this one that I have been playing with: http://jqueryfordesigners.com/image-cross-fade-transition/ Any reason not to do it your way?
ian
I have only tested my solution in Firefox. It also needs style, script and html code to work. The solution you've linked to is bundled as a plug-in, doesn't require extra html elements nor additional css. However it only caters for fading an image into another one. I guess if you need to fade a div into an image you could easily do the modifications, but you have to try that yourself.
kgiannakakis