如何在div中修复背景?

时间:2022-11-25 14:50:38

I have a site, and in one of my pages I use a div which looks like an editor. I can have a background in this div and the content above it. I want this background to stay in a fixed position while I am scrolling in the div.

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

My code is similar to this:

我的代码与此类似:

 <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>

The background stays fixed and works ok when I scroll inside the div. The problem is that it is moving when I scroll in the whole page.

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

Does anyone know what could be the problem?

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

Thanks in advance

提前致谢

2 个解决方案

#1


0  

Here is an example of background-attachment: fixed; http://www.w3schools.com/css/tryit.asp?filename=trycss_background-attachment. That should be what you need.

这是一个背景附件的例子:fixed; http://www.w3schools.com/css/tryit.asp?filename=trycss_background-attachment。那应该是你需要的。

#2


0  

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

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

html:

HTML:

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

css:

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

#1


0  

Here is an example of background-attachment: fixed; http://www.w3schools.com/css/tryit.asp?filename=trycss_background-attachment. That should be what you need.

这是一个背景附件的例子:fixed; http://www.w3schools.com/css/tryit.asp?filename=trycss_background-attachment。那应该是你需要的。

#2


0  

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

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

html:

HTML:

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

css:

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