2011-07-06 37 views
2

我有一个网站,在我的一个网页中,我使用了一个看起来像编辑器的div。我可以在这个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中工作正常。问题在于,当我在整个页面中滚动时,它正在移动。

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

预先感谢

+0

您可以链接到页面,我不知道我理解这个问题。 –

+0

你忘了结束'style =“...”'。你写了'style =“...' – kongr45gpen

+0

这个页面有点复杂,我有一个例子,我发现这个链接http://www.quackit.com/html/codes/html_background_codes.cfm和我想要做同样的事情,就像他为固定背景图像做的一样。我的问题是,当我滚动整个页面而不是只在这个div内时,这个图像正在移动。 – novellino

回答

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