理解CSS Clip屬性及用法

來源:互聯網
上載者:User

標籤:style   class   code   http   ext   color   

應用Clip屬性實現的一個簡單:

 樣式寫法:

  1. .my-element { 
  2.     position: absolute; 
  3.     clip: rect(10px  350px  170px  0); /* IE4 to IE7 */ 
  4.     clip: rect(10px, 350px, 170px, 0); /* IE8+ & other browsers */ 

屬性解析:

clip : { shape |  auto  | inherit } ;auto 即瀏覽器預設解析,無clip效果,inderit 繼承父層clip樣式 shape 設定clip 形狀,目前只支援 rect 即:矩形。 參數範例為:
  1. clip: rect(<top>, <right>, <bottom>, <left>); 

 有點不足的是 clip 屬性只能使用於 位置屬性設定為 absolute 或 fixed的元素

使用Clip實現的範例:

 

  1. <style> 
  2. img { 
  3.   position: absolute; 
  4.   left: 10px; 
  5.   top: 60px; 
  6.   display: block; 
  7.   clip: rect(200px, 0, 0, 400px); 
  8.   -webkit-transition: all 0.5s ease-out; 
  9.   -moz-transition: all 0.5s ease-out; 
  10.   transition: all 0.5s ease-out; 
  11.  
  12. span:hover ~ img { 
  13.   clip: rect(0, 400px, 200px, 0); 
  14.  
  15. span { 
  16.   display: inline-block; 
  17.   padding: 10px; 
  18.   margin: 10px; 
  19.   background: #08C; 
  20.   color: white; 
  21.   font-family: "Helvetica", "Arial", sans-serif; 
  22.   font-weight: bold; 
  23.   text-shadow: 0 -1px rgba(0,0,0,0.3); 
  24.   text-align: center; 
  25.   cursor: pointer; 
  26.  
  27. span:hover { 
  28.   background: #09C; 
  29. </style> 
  30. <span>Hover me</span> 
  31. <img src="http://lorempixel.com/400/200/"> 

 2

  1. <style> 
  2. body {   
  3.   overflow: hidden; 
  4.  
  5. ul { 
  6.   padding: 0; 
  7.   margin: 10px; 
  8.   list-style: none; 
  9.   width: 300px; 
  10.   height: 300px; 
  11.   box-shadow: 0 0 10px rgba(0,0,0,0.5); 
  12.  
  13. ul:after { 
  14.   clear: both; 
  15.   content: ""; 
  16.   display: table; 
  17.  
  18. li { 
  19.   position: relative; 
  20.   float: left; 
  21.   width: 100px; 
  22.   height: 100px; 
  23.   background: url(‘ http://subtlepatterns.subtlepatterns.netdna-cdn.com/patterns/pw_maze_white.png‘); 
  24.   cursor: pointer; 
  25.  
  26. li:nth-of-type(3n+1) { 
  27.   clear: both; 
  28.  
  29. img { 
  30.   position: absolute; 
  31.   display: block; 
  32.   clip: rect(0, 100px, 100px, 0); 
  33.   -webkit-transition: all 0.2s ease-out, z-index 0s; 
  34.   -moz-transition: all 0.2s ease-out, z-index 0s; 
  35.   transition: all 0.2s ease-out, z-index 0s; 
  36.   opacity: 0.9; 
  37.  
  38. li:hover img { 
  39.   clip: rect(0, 300px, 300px, 0); 
  40.   z-index: 2;  
  41.   opacity: 1; 
  42.  
  43. li:nth-of-type(3n+1):hover img { 
  44.   left: 310px; 
  45.  
  46. li:nth-of-type(3n+2):hover img { 
  47.   left: 210px; 
  48.  
  49. li:nth-of-type(3n):hover img { 
  50.   left: 110px; 
  51.  
  52. li:nth-of-type(n+4):nth-of-type(-n+6):hover img { 
  53.   top: -100px; 
  54.  
  55. li:nth-of-type(n+7):nth-of-type(-n+9):hover img { 
  56.   top: -200px; 
  57. }  
  58. </style> 
  59. <ul> 
  60.   <li><img src="http://lorempixel.com/300/300/sports/"></li> 
  61.   <li><img src="http://lorempixel.com/300/300/animals/"></li> 
  62.   <li><img src="http://lorempixel.com/300/300/abstract/"></li> 
  63.   <li><img src="http://lorempixel.com/300/300/nightlife/"></li> 
  64.   <li><img src="http://lorempixel.com/300/300/city/"></li> 
  65.   <li><img src="http://lorempixel.com/300/300/food/"></li> 
  66.   <li><img src="http://lorempixel.com/300/300/people/"></li> 
  67.   <li><img src="http://lorempixel.com/300/300/nature/"></li> 
  68.   <li><img src="http://lorempixel.com/300/300/fashion/"></li> 
  69. </ul> 

關於Clip屬性應用的進階使用範例: http://tympanus.net/codrops/2013/01/17/putting-css-clip-to-work-expanding-overlay-effect/

參考網址: http://tympanus.net/codrops/2013/01/16/understanding-the-css-clip-property/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.