2011-07-06 83 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"); 
}