views:

121

answers:

4

I have 4 banner images, and I want to cycle through them using a basic fade animation in an infinite loop. I found this jQuery plugin: http://jquery.malsup.com/cycle/ and have tried to do it this way:

HTML:

<div id="banner">
    <div id="home-banner-1"></div>
    <div id="home-banner-2"></div>
    <div id="home-banner-3"></div>
    <div id="home-banner-4"></div>
</div>

CSS:

#banner {
    margin: 0 auto 20px;
    width: 940px;
}
#home-banner-1 {
    background: url(../Images/home_banner.png) no-repeat 0 0;
    height: 271px;
}
#home-banner-2 {
    background: url(../Images/home_banner.png) no-repeat 0 -272px;
    height: 271px;
}
#home-banner-3 {
    background: url(../Images/home_banner.png) no-repeat 0 -544px;
    height: 271px;
}
#home-banner-4 {
    background: url(../Images/home_banner.png) no-repeat 0 -816px;
    height: 271px;
}

JAVASCRIPT:

<script type="text/javascript" src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"&gt;&lt;/script&gt;
<script type="text/javascript">
    $(document).ready(function() {
        $('#banner').cycle('fade');
    });
</script>

What ends up happening is that NONE of the banners show up due to position: absolute being set on each one during the cycle. What am I missing here? Shouldn't this just work? Is there a better way to do this?

A: 

If position: absolute is set on them, they will be positioned based on the first element in their parent chain that has position: relative set. Often, this ends up being the <body /> element. Try setting position: relative on the parent <div id="banner" />.

Annabelle
Tried setting position: relative on id="banner" without changing anything else in the code and the problem did not go away. Still don't see anything on screen even though Firebug shows the jquery.cycle method running because the divs are constantly having their visibility and z-index changed.
Chris F
A: 

Are you sure you're supposed to be setting the absolute positions? If you look at this demo (http://jquery.malsup.com/cycle/basic.html), the positions are all the same but the z-indexes/opacities/display properties differ.

<div class="slideshow" style="position: relative; ">
        <img src="http://cloud.github.com/downloads/malsup/cycle/beach1.jpg" width="200" height="200" style="position: absolute; top: 0px; left: 0px; width: 200px; height: 200px; z-index: 5; opacity: 0; display: none; ">
        <img src="http://cloud.github.com/downloads/malsup/cycle/beach2.jpg" width="200" height="200" style="position: absolute; top: 0px; left: 0px; width: 200px; height: 200px; z-index: 5; opacity: 0; display: none; ">
        <img src="http://cloud.github.com/downloads/malsup/cycle/beach3.jpg" width="200" height="200" style="position: absolute; top: 0px; left: 0px; width: 200px; height: 200px; z-index: 5; opacity: 0; display: none; ">
        <img src="http://cloud.github.com/downloads/malsup/cycle/beach4.jpg" width="200" height="200" style="position: absolute; top: 0px; left: 0px; width: 200px; height: 200px; z-index: 5; opacity: 0; display: none; ">
        <img src="http://cloud.github.com/downloads/malsup/cycle/beach5.jpg" width="200" height="200" style="position: absolute; z-index: 6; top: 0px; left: 0px; display: block; width: 200px; height: 200px; opacity: 1; ">
    </div>
a.feng
I'm not setting the positions to absolute, jquery.cycle is. I added the "banner" CSS to demonstrate that I am not setting any position values. It seems to me there must be something missing, otherwise this should just work.
Chris F
A: 

I had a similar requirement and ended up using a plugin I found called innerFade. You can check it out at this URL: http://medienfreunde.com/lab/innerfade/

Sonny Boy
I tried this, but get the exact same result as jquery.cycle. The cycling is happening, but no images show up. If I use Firebug to remove the "position: absolute" on each div inside of the banner div, then they all show up, but the effect is broken since the fade-in image is below the fade-out image until the fade-out image disappears, and then the fade-in image pops up to the correct position! I'm going to try using 4 separate images and see if that works.
Chris F
@Chris - Try removing your CSS for the div tags. The jQuery plugin should handle all or most of this for you.
Sonny Boy
Also, you can check out the markup, code, and CSS for my last site as an example: http://www.matchtech.de/
Sonny Boy
@Sonny, sorry, but I'm not sure what you mean by "remove CSS" from my div tags. There's barely anything there. I need the background tag. The height tag can be removed, but only because you set it again for the entire #banner using innerfade. The margin and width tags for #banner are necessary.
Chris F
@Chris - Not FROM your tags, FOR your tags. Remove (or comment) the CSS for the following IDs: #home-banner-1,#home-banner-2,#home-banner-3,#home-banner-4.I suspect the innerFade is working as intended, but your other images aren't showing because you've set the positions to be outside of the visible area.
Sonny Boy
A: 

I was unable to make this work using any of the answers given to date, so my answer to the problem was to remove the sprite and rather than using 4 div tags styled with backgrounds, to use 4 separate img tags pulling 4 separate images. Then everything started to work as advertised.

Chris F