3

我有一个网站,在我的一个页面中,我使用了一个看起来像编辑器的 div。我可以在这个 div 和上面的内容中有一个背景。当我在 div 中滚动时,我希望这个背景保持在一个固定的位置。

我的代码与此类似:

 <div id="myEditorArea"  style="vertical-align: top; width: 240px; height: 320px;      background-image: url('image.png'); background-repeat: no-repeat; background-position: center center; background-attachment:fixed; overflow:scroll; text-align: left;">

 bla bla bla

 </div>

当我在 div 内滚动时,背景保持固定并且工作正常。问题是当我滚动整个页面时它正在移动。

有谁知道可能是什么问题?

提前致谢

4

2 回答 2

1

这是background-attachment: fixed; http://www.w3schools.com/css/tryit.asp?filename=trycss_background-attachment的示例。那应该是你需要的。

于 2011-07-20T18:43:56.790 回答
0

来源:https ://codyhouse.co/gem/alternate-fixed-scroll-backgrounds/

html:

<div class="cd-fixed-bg cd-bg-1">
        <h1><!-- title goes here --></h1>
    </div> 

CSS:

body, html, main {
    /* important */
     height: 100%;
}

.cd-fixed-bg {
min-height: 100%;
background-size: cover;
background-attachment: fixed;
background-repeat: no-repeat;
background-position: center center;
}

.cd-fixed-bg.cd-bg-1 {
background-image: url("../img/cd-background-1.jpg");
}
于 2016-09-22T19:14:57.593 回答