繁体   English   中英

如何在 div 中固定背景?

[英]How can I have fixed background in a div?

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

我的代码与此类似:

 <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 内滚动时,背景保持固定并且工作正常。 问题是当我滚动整个页面时它正在移动。

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

提前致谢

来源: 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");
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM