首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作为滑动旋转木马的视差背景图像

作为滑动旋转木马的视差背景图像
EN

Stack Overflow用户
提问于 2013-02-06 23:11:10
回答 1查看 3K关注 0票数 1

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

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

HTML的结构是:

代码语言:javascript
复制
<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中添加指向下一个图像的链接。

对如何做到这一点有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-08 00:59:35

js将背景更改为数组中的随机背景:

代码语言:javascript
复制
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));
}

您可以像这样创建按钮:

代码语言:javascript
复制
<input type="button" onclick="changeBG();" >

或者像这样:

代码语言:javascript
复制
<a href="javascript:changeBG();">ButtonText</a>

这应该会将de背景图像更改为4张图片中的任意一张...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14732278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档