欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用CSS3來實(shí)現(xiàn)滾動(dòng)視差效果的教程

OSChina   發(fā)布時(shí)間:2015-08-24 18:39:06   作者:Junn   我要評(píng)論
這篇文章主要介紹了使用CSS3來實(shí)現(xiàn)滾動(dòng)視差效果的教程,主要使用到了background-attachment屬性,需要的朋友可以參考下

“視差(parallax)”效果現(xiàn)在在互聯(lián)網(wǎng)上越來越流行了。如果你還沒聽說過什么是視差效果,它其實(shí)就是利用圖片形成不同的層,分別以不同的速度,不同的方向移動(dòng)產(chǎn)生的效果。這會(huì)產(chǎn)生出很奇妙的視覺效果,能有力的吸引住瀏覽者的目光。


觀看演示


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

簡言之,使用JavaScript來實(shí)現(xiàn)視差效果會(huì)讓頁面的滾動(dòng)出現(xiàn)性能問題,出現(xiàn)卡頓。

background-attachment屬性回顧
background-attachment -- 定義背景圖片隨滾動(dòng)軸的移動(dòng)方式
取值: scroll | fixed | inherit
scroll: 隨著頁面的滾動(dòng)軸背景圖片將移動(dòng)
fixed: 隨著頁面的滾動(dòng)軸背景圖片不會(huì)移動(dòng)
inherit: 繼承
初始值: scroll
繼承性: 否
適用于: 所有元素
background:背景.attachment:附著.
示例

CSS Code復(fù)制內(nèi)容到剪貼板
  1. body    
  2. {   
  3.  background-image:url('list-orange.png');   
  4.  background-attachment:fixed;   
  5.  background-repeat:repeat-x;   
  6.  background-position:center center;   
  7. }  

屏幕的背景圖片為一條橙色線.隨著滾動(dòng)軸移動(dòng),橙色線的視覺位置不變.
CSS background-attachment 屬性示例

使用background-attachment: fixed實(shí)現(xiàn)視差效果

為什么只有一小部分人知道,這種效果實(shí)際上可以用CSS實(shí)現(xiàn)。

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

background-attachment的缺省值是scroll,也就是背景圖片和內(nèi)容的位置是相對(duì)靜止的。這我們大家都見過,當(dāng)我們上下滾動(dòng)一個(gè)網(wǎng)頁時(shí),背景和內(nèi)容一起滾動(dòng)。

當(dāng)把background-attachment設(shè)置成fixed時(shí),事情會(huì)變得有趣。fixed是說背景圖片不隨內(nèi)容一起滾動(dòng),而是跟窗口保持靜止。也就是說,當(dāng)你拖動(dòng)滾動(dòng)條時(shí),背景圖片沒有變化。這就能夠產(chǎn)生漂亮的視差效果。

讓我看一個(gè)實(shí)際實(shí)現(xiàn):

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <!-- Four containers for setting the background images -->   
  2. <div class="parallax">   
  3.   <div class="bg__foo">foo</div>   
  4.   <div class="bg__bar">bar</div>   
  5.   <div class="bg__baz">baz</div>   
  6.   <div class="bg__bazz">bazz</div>   
  7. </div>   
  8. // setting base styles to image containers   
  9. [class*="bg__"] {   
  10.   height: 50vh;   
  11.   
  12.   text-indent: -9999px;   
  13.   
  14.   /* fix background */  
  15.   background-attachmentfixed;   
  16.   
  17.   /* center it */  
  18.   background-positioncenter center;   
  19.   
  20.   /* Scale it nicely to the element */  
  21.   background-size: cover;   
  22.   
  23.   /* just make it look a bit better  */  
  24.   &:nth-child(2n) {   
  25.     box-shadow: inset 0 0 1em #111;   
  26.   }   
  27. }   
  28.   
  29. .bg__foo {   
  30.   background-imageurl(   
  31.     http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax1.jpg   
  32.   );   
  33. }   
  34.   
  35. .bg__bar {   
  36.   background-imageurl(   
  37.     http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax2.jpg   
  38.   );   
  39. }   
  40.   
  41. .bg__baz {   
  42.   background-imageurl(   
  43.     http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax3.jpg   
  44.   );   
  45. }   
  46.   
  47. .bg__bazz {   
  48.   height: 100vh;   
  49.   
  50.   background-imageurl(   
  51.     http://www.webhek.com/wordpress/wp-content/uploads/2014/07/parallax1.jpg   
  52.   );   
  53. }  

關(guān)于這種技術(shù)的瀏覽器兼容情況,你可以參考這里,基本上,現(xiàn)代瀏覽器和IE9+的瀏覽器都支持。


觀看演示


對(duì)我個(gè)人而言,我更喜歡CSS技術(shù)實(shí)現(xiàn)的視差效果,而不是用JavaScript。用CSS實(shí)現(xiàn),是受瀏覽器原生支持,沒有編程邏輯,沒有對(duì)DOM額外的操作,使得整個(gè)方案非常的簡潔漂亮。

即使是CSS實(shí)現(xiàn)的視差效果,也會(huì)給瀏覽器帶來負(fù)擔(dān)。

background-attachment: fixed會(huì)導(dǎo)致瀏覽器更多的渲染,也會(huì)影響瀏覽器滾動(dòng)的效率。所以,開發(fā)時(shí)一定要多做測(cè)試,視性能情況而決定實(shí)現(xiàn)的效果。

相關(guān)文章

最新評(píng)論