background-attachment 屬性用來設定背景圖片究竟是固定在原位,還是會隨著頁面其餘內容一起滾動,常用於營造視差或固定背景的視覺效果。
object.style.backgroundAttachment = "fixed"各大主流瀏覽器(IE、Firefox、Chrome、Safari、Opera)都支援 background-attachment 屬性。需要注意的是,任何版本的 Internet Explorer(包含 IE8)都不支援屬性值「inherit」,IE9 以上才可能支援。
body {
background-image: url(image/bg.jpg);
background-attachment: fixed;
}
套用後,當使用者捲動頁面時,這張背景圖會固定在原處,形成低調而有趣的視覺層次。