dnpayne
dnpayne

Reputation: 13

Parallax background image as a sliding carousel

I have been tinkering around with creating a parallax scrolling site over the last couple of days. I have a nice prototype working that scrolls different background images. Looks pretty cool.

I'd like to take it one step further. I basically have a div with a background image assigned to it. The background image scrolls at a different speed. I want to now add the ability to click a left or right arrow and change that background. Basically a carousel. But, I still want to maintain the parallax effect if the user scrolls down.

The structure of the HTML is:

<section id="one">
<div id="mainimagewrapper">
    <div class="image1">
        <div class="image2">
            <div id="textdiv">this is some text</div>
        </div>
    </div>
</div>
</section>

The mainimagewrapper contains the large image that I want to change. The textdiv will be where I add my links to the next images.

Any thoughts on how to do this?

Upvotes: 1

Views: 3245

Answers (1)

Vloxxity
Vloxxity

Reputation: 980

The js changes the background to a random one from the array:

function changeBG()
{
    var images = new Array('http://www.windows-7-wallpapers.com/wallpapers/img24-1920x1200.jpg',
                           'http://www.pptbackgroundstemplates.com/backgrounds/seamless-white-texture-ppt-backgrounds-powerpoint.jpg',
                          'http://awesomewallpapers.files.wordpress.com/2009/08/white2.png',
                          'http://www.wallpaperpimper.com/wallpaper/Flower/Flower_Art/Flower-Art-Winter-White-1-1920x1200.jpg');

    $('#mainimagewrapper').css("background-image", "url("+ images[getRandom(0, images.length - 1)] +")");
}




function getRandom(min, max) {
    if (min > max) {
        return -1;
    }

    if (min == max) {
        return min;
    }

    var r;

    //do {
    r = Math.random();
    //}
    //while (r == 1.0);

    return min + parseInt(r * (max - min + 1));
}

You can create your button like this:

<input type="button" onclick="changeBG();" >

or like this:

<a href="javascript:changeBG();">ButtonText</a>

This should change de background-image to a random one of that 4 pictures...

Upvotes: 1

Related Questions