使用CSS3來實現滾動視差效果

來源:互聯網
上載者:User
這篇文章主要介紹了使用CSS3來實現滾動視差效果的教程,主要使用到了background-attachment屬性,需要的朋友可以參考下

“視差(parallax)”效果現在在互連網上越來越流行了。如果你還沒聽說過什麼是視差效果,它其實就是利用圖片形成不同的層,分別以不同的速度,不同的方向移動產生的效果。這會產生出很奇妙的視覺效果,能有力的吸引住瀏覽者的目光。

在web設計中,最常見的實現視差效果的方式是使用jQuery外掛程式。但這種方法有一些弊端。這些外掛程式大多都是在window對象的scroll事件上放置監聽器。這會導致JavaScript需要處理大量的事件觸發(處理scroll事件很容易造成瀏覽器效能問題,使用時需要非常小心。)移動不同的層,計算背景的位置,設定圖片的屬性,這都引起了大量的DOM操作。

簡言之,使用JavaScript來實現視差效果會讓頁面的滾動出現效能問題,出現卡頓。

background-attachment屬性回顧
background-attachment -- 定義背景圖片隨捲軸的移動方式
取值: scroll | fixed | inherit
scroll: 隨著頁面的捲軸背景圖片將移動
fixed: 隨著頁面的捲軸背景圖片不會移動
inherit: 繼承
初始值: scroll
繼承性: 否
適用於: 所有元素
background:背景.attachment:附著.
樣本

body    {    background-image:url('list-orange.png');    background-attachment:fixed;    background-repeat:repeat-x;    background-position:center center;   }

螢幕的背景圖片為一條橙色線.隨著捲軸移動,橙色線的視覺位置不變.
CSS background-attachment 屬性樣本

使用background-attachment: fixed實現視差效果

為什麼只有一小部分人知道,這種效果實際上可以用CSS實現。

為了實現視差效果,多個背景圖片必須放置在不同的元素上。這些背景圖需要定義成background-attachment: fixed。通過設定background-attachment,我們可以改變背景映像的效果和位置。

background-attachment的預設值是scroll,也就是背景圖片和內容的位置是相對靜止的。這我們大家都見過,當我們上下滾動一個網頁時,背景和內容一起滾動。

當把background-attachment設定成fixed時,事情會變得有趣。fixed是說背景圖片不隨內容一起滾動,而是跟視窗保持靜止。也就是說,當你拖動捲軸時,背景圖片沒有變化。這就能夠產生漂亮的視差效果。

讓我看一個實際實現:

<!-- Four containers for setting the background images -->   <p class="parallax">     <p class="bg__foo">foo</p>     <p class="bg__bar">bar</p>     <p class="bg__baz">baz</p>     <p class="bg__bazz">bazz</p>   </p>   // setting base styles to image containers   [class*="bg__"] {     height: 50vh;     text-indent: -9999px;     /* fix background */  background-attachment: fixed;     /* center it */  background-position: center center;     /* Scale it nicely to the element */  background-size: cover;     /* just make it look a bit better  */  &:nth-child(2n) {       box-shadow: inset 0 0 1em #111;     }   }   .bg__foo {     background-image: url(       http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax1.jpg     );   }   .bg__bar {     background-image: url(       http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax2.jpg     );   }   .bg__baz {     background-image: url(       http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax3.jpg     );   }   .bg__bazz {     height: 100vh;     background-image: url(       http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax1.jpg     );   }

關於這種技術的瀏覽器安全色情況,你可以參考這裡,基本上,現代瀏覽器和IE9+的瀏覽器都支援。

對我個人而言,我更喜歡CSS技術實現的視差效果,而不是用JavaScript。用CSS實現,是受瀏覽器原生支援,沒有編程邏輯,沒有對DOM額外的操作,使得整個方案非常的簡潔漂亮。

即使是CSS實現的視差效果,也會給瀏覽器帶來負擔。

background-attachment: fixed會導致瀏覽器更多的渲染,也會影響瀏覽器滾動的效率。所以,開發時一定要多做測試,視效能情況而決定實現的效果。

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

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.