網頁固定背景交替實現視差效果

來源:互聯網
上載者:User

標籤:style   http   color   io   os   使用   java   ar   strong   

 

視差滾動效果近年來很受大家的歡迎,但實現這個功能一般都需要javaScript,而且實現做起來有一定的難度。就這個問題,我們跟大家分享另外一種比較簡單的視差滾動效果技術,只需要CSS就可以實現了。一起來看看吧。

 

該滾動效果特點是使用固定背景與色塊內容交替,當訪客向下滾動時,背景是fixed固定的。可學習HTML進階教程瞭解。

從可以看出,內容向上滾動時,第一張背景是固定的,不會跟著向上滾動。即:固定背景 + 色塊內容 + 固定背景 + 色塊內容…這樣的交替方式,從而產生視差效果。

 

相容性
  • IE9+
  • Firefox
  • Chrome
  • Safari
  • Opera
使用教程

HTML代碼

HTML代碼結構很清晰,使用main標籤包住所有內容,其中.cd-fixed-bg樣式就是固定背景,.cd-scroll-b

標題

內容...

標題

內容...

 

 

 

CSS樣式
主要是通過css的background-attachment:fixed定位屬性來讓背景固定,具體請看下面的樣式代碼:
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");
}
.cd-fixed-bg.cd-bg-2 {
background-image: url("../img/cd-background-2.jpg");
}
.cd-fixed-bg.cd-bg-3 {
background-image: url("../img/cd-background-3.jpg");
}
.cd-fixed-bg.cd-bg-4 {
background-image: url("../img/cd-background-4.jpg");
}
.cd-scrolling-bg {
min-height: 100%;
}

好了,就此介紹完這個固定背景滾動效果,喜歡的可以動手試一試,或許在你的下一個網頁設計項目上嘗試使用。想瞭解更多HTML/CSS教程可登陸e良師益友網。

網頁固定背景交替實現視差效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.