Javascript 作为滑动转盘的视差背景图像

Javascript 作为滑动转盘的视差背景图像,javascript,carousel,parallax,Javascript,Carousel,Parallax,在过去的几天里,我一直在尝试创建一个视差滚动网站。我有一个很好的原型工作,滚动不同的背景图像。看起来很酷 我想更进一步。我基本上有一个div,它有一个背景图像。背景图像以不同的速度滚动。现在我想添加单击左箭头或右箭头并更改背景的功能。基本上是旋转木马。但是,如果用户向下滚动,我仍然希望保持视差效果 HTML的结构是: <section id="one"> <div id="mainimagewrapper"> <div class="image1">

在过去的几天里,我一直在尝试创建一个视差滚动网站。我有一个很好的原型工作,滚动不同的背景图像。看起来很酷

我想更进一步。我基本上有一个div,它有一个背景图像。背景图像以不同的速度滚动。现在我想添加单击左箭头或右箭头并更改背景的功能。基本上是旋转木马。但是,如果用户向下滚动,我仍然希望保持视差效果

HTML的结构是:

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

这是一些文本
mainimagewrapper包含我要更改的大图像。textdiv将是我添加到下一个图像的链接的地方


有什么想法吗?

js将背景从阵列更改为随机背景:

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));
}
您可以这样创建按钮:

<input type="button" onclick="changeBG();" >
<a href="javascript:changeBG();">ButtonText</a>

或者像这样:

<input type="button" onclick="changeBG();" >
<a href="javascript:changeBG();">ButtonText</a>


这会将背景图像更改为4张图片中的随机一张…

js会将背景从阵列更改为随机一张:

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));
}
您可以这样创建按钮:

<input type="button" onclick="changeBG();" >
<a href="javascript:changeBG();">ButtonText</a>

或者像这样:

<input type="button" onclick="changeBG();" >
<a href="javascript:changeBG();">ButtonText</a>


这会将背景图像更改为这4张图片中的任意一张…

感谢您的回复。我很好奇这是否仍然有效,因为我正在使用一个带有标记的div作为链接,而不是像您所示的实际表单按钮。让我今天测试一下,然后我会回来报告。谢谢,我可以测试一下。不幸的是,这并没有改变背景图像。我删除了视差滚动的脚本,只是想看看是否有干扰,但它仍然无法工作。我把你的代码扔进了我的文档的头部,并把链接扔进了#mainimagewrapper分区。我很困惑。你如何定义后台atm?我忘了复制随机函数:3你能用该函数再试一次吗?谢谢你的回复。我很好奇这是否仍然有效,因为我正在使用一个带有标记的div作为链接,而不是像您所示的实际表单按钮。让我今天测试一下,然后我会回来报告。谢谢,我可以测试一下。不幸的是,这并没有改变背景图像。我删除了视差滚动的脚本,只是想看看是否有干扰,但它仍然无法工作。我把你的代码扔到我的文档头上,然后把链接丢到了#mainimagewrapper div中。我被难住了。你如何定义后台atm?我忘了复制随机函数:3你能用该函数再试一次吗?