javascript - 动画背景不固定

标签 javascript jquery html css jquery-plugins

大家好,我正在为我的网站制作新背景。我正在尝试为其添加动画效果。我目前在这里工作:http://wow-x.com/back/

如您所见,当您向下滚动页面时,名为“背景”的 div 不会保持“固定”状态……我似乎无法让它保持固定状态。

我的 CSS 看起来像这样:

body {
    background-color: #0a0a0a;
    color: #a2a2a2;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    line-height: 1.4;
    margin: 0;
    padding: 0;
    background-image: url(http://wow-x.com/board/images/blackevo4-pure/background.jpg);
    background-repeat: no-repeat;
    background-position: center top;
    background-attachment:fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

#background {
    background: url(http://wow-x.com/board/images/blackevo4-pure/back1.jpg);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    min-width: 100%;
    min-height: 100%;
    opacity:0.1;
    z-index:-1;
}

#midground {
    background: url(http://wow-x.com/board/images/blackevo4-pure/midground.png) repeat 20% 20%;
    background-attachment:fixed;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index:-2;
    height:100%;
    width:100%;
}

#foreground {
    background: url(http://wow-x.com/board/images/blackevo4-pure/foreground.png) repeat 90% 110%;
    background-attachment:fixed;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index:-3;
    height:100%;
    width:100%;
}

最佳答案

如果您试图保持 #background div 固定,请将其位置从 position: absolute; 更改为 position: fixed;

position 属性设置为 absolute 意味着元素将相对于其第一个定位的祖先进行定位。在您的例子中,这是正文本身,因此图像会随页面一起滚动。

position 属性设置为 fixed 意味着元素将相对于窗口定位。所以它的位置将独立于滚动位置。

关于javascript - 动画背景不固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17780191/

相关文章:

javascript - Jquery 复选框在更改时呈现数据

javascript - $(this).hasClass ('has-success' ) 始终返回 false

javascript - 我的按钮不起作用

Javascript 将函数绑定(bind)到鼠标滚轮

html - 为什么这个伪元素在使用缩放变换时会移动到父元素之上?

javascript - 滚动两个 DIV,一个显示,另一个隐藏

javascript - 在 Closure Compier 中跳过全局变量冲突

javascript - javascript 和 ruby​​ 整数之间的区别(使用左移按位运算符)

javascript - 使用 async/await 与使用 Promises 有何不同?

javascript - jquery\js物理引擎推荐