將CSS CLIP屬性應用在:擴展覆蓋效果
我們想要展示如何利用CSS3 clip屬性制作一種簡單而整潔的擴展效果,當點擊一個box元素時實現平穩過渡。這個想法是為了實現某種疊加效果,好像它實際上在各個元素的下面。點擊其中一個元素將創建一個切斷圖片的效果,展示將被寬展開的另一層。
怎樣做?
首先,我們要創建一個項目列表,它將看起來像是裝滿不同城市名稱的盒子:
每 一個箱子將包含一個元素(覆蓋層),該元素的位置將被固定。實際上這個元素會鋪滿整個頁面,但我們不會看到它,因為透明度opacity將被設置為0。當 我們點擊一個盒子,我們將使用clip:rect()修剪各自的內部固定元素。然后我們將動態展示所有疊加的寬度和高度,做為整個視窗:
單擊關閉按鈕將反轉這個效果,并且使列表項的大小最小化并消失。
那么,讓我們從HTML開始吧!
標記:
我 們將為這些盒子使用一個無序列表。每個列表項將會有一個圖標類和一個可選的“span”類,將控制盒子的寬度。在里面將添加一些文本和覆蓋的層。疊加的部 分將包含一個列布局的結構。因為我們選擇了一個虛擬的天氣應用程序作為主題,所以我們將顯示未來七天的天氣預報。每個“day”列有一些span,用做工 作日、天氣圖標和溫度:
<ul id="rb-grid" class="clearfix rb-grid">
<li class="rb-span-2 icon-clima-1">
<h3>Lisbon</h3>
<span class="rb-temp">21°C</span>
<div class="rb-overlay">
<span class="rb-close">close</span>
<div class="rb-week">
<div><span class="rb-city">Lisbon</span><span class="icon-clima-1"></span><span>21°C</span></div>
<div><span>Mon</span><spanclass="icon-clima-1"></span><span>19°C</span></div>
<div><span>Tue</span><spanclass="icon-clima-2"></span><span>19°C</span></div>
<div><span>Wed</span><spanclass="icon-clima-2"></span><span>18°C</span></div>
<div><span>Thu</span><spanclass="icon-clima-2"></span><span>17°C</span></div>
<div><span>Fri</span><spanclass="icon-clima-1"></span><span>19°C</span></div>
<div><span>Sat</span><spanclass="icon-clima-1"></span><span>22°C</span></div>
<div><span>Sun</span><spanclass="icon-clima-1"></span><span>18°C</span></div>
</div>
</div>
</li>
<li class="icon-clima-2">
<h3>Paris</h3><span class="rb-temp">11°C</span>
<div class="rb-overlay">
<!-- ... -->
</div>
</li>
<li><!-- ... --></li>
<!-- ... -->
</ul> 讓我們看看CSS:
一個無序列表將會集中在父結點,我們將會去掉列表樣式:
.rb-grid {
list-style: none;
text-align: center;
margin: 0 auto;
} 列表項將有一個不固定的寬度,我們將給他們一個15em的高度。它將浮在左邊:
.rb-grid li {
width: 24%;
height: 15em;
margin: 0.5%;
background: #8CC7DF;
color: #fff;
display: block;
float: left;
padding: 1.6em;
cursor: pointer;
position: relative;
} 網格內的項目有三種不同的寬度,“默認”的那個是24%,下面的其他兩個是:
.rb-grid li.rb-span-2 {
width: 49%;
}
.rb-grid li.rb-span-4 {
width: 99%;
} 讓我們給城市標題加些料:
.rb-grid li h3 {
font-size: 2.6em;
font-weight: 100;
} 為 圖標字體添加一個CSS文件。它是Climacons字體,由Adam Whitcroft 創建。你可以查看climacons.css看看我們包括了哪個圖標。基本上,我們使用圖標類來添加帶偽元素的圖標。在網格里,我們希望他們是被絕對定位 在右下角,有點像被剝奪了權利。
.rb-grid li[class^="icon-"]:before,
.rb-grid li[class*=" icon-"]:before {
font-size: 10em;
position: absolute;
display: block;
width: 100%;
height: 100%;
top: 0;
left: 0;
line-height: 3;
opacity: 0.4;
text-align: right;
pointer-events: none;
} 溫度顯示效果是半透明的,我們為它的opacity添加一個過渡:
.rb-temp {
display: block;
font-size: 2em;
opacity: 0.5;
transition: all 0.3s ease-in-out;
} 當懸浮在一個列表項上,我們簡單增加:
.rb-grid li:hover .rb-temp {
opacity: 1;
} 現 在,讓我們看一看重要的重疊部分。最后的視圖,我們是想要一個全屏范圍的重疊效果,所以我們將把它的寬度和高度設置到100%。我 們不希望它是相對于任何東西,而是希望它在頂部,所以要給它固定了位置。因為這將讓它出現在所有東西的頂部,會有重疊,很多重疊!這時需要在初始時設置 z-index為-1。這將把它們統統放在內容頁面的后面。設置opacity為0會使他們不可見:
.rb-overlay {
opacity: 0;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: all 0.4s ease;
z-index: -1;
pointer-events: none;
cursor: default;
} 這就是初始狀態的疊加。一旦我們點擊列表項,將為clip屬性設置正確的rect()值,動態擴展。
讓我們看看其余的樣式。
每個疊加層有點接近“按鈕”,將被定位在右上角:
.rb-close {
position: absolute;
top: 0.4em;
right: 0.4em;
width: 2em;
height: 2em;
text-indent: -9000px;
cursor: pointer;
z-index: 1000;
}
.rb-close:before {
content: 'x';
font-weight: 100;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
font-size: 3em;
line-height: 0.6;
text-align: center;
text-indent: 0px;
} 列的包裝有rb-week類(雖然我們也包括當前天氣列在內)。需要將它設置為100%的寬度和高度以便我們可以準確的定義它的孩子的高度和寬度:
.rb-week {
width: 100%;
height: 100%;
} “列”會有一個10%的寬度(除了第一個有30%的寬度),他們將浮動在左邊:
.rb-week > div {
width: 10%;
height: 100%;
float: left;
position: relative;
padding: 3% 0;
}
.rb-week > div:first-child {
width: 30%;
} 總共有八個列,10% 乘以7結果是70%,加上我們有30%的左邊第一列。
每個span有一個30%的高度和間距:
.rb-week span {
padding: 5% 0;
font-size: 2em;
font-weight: 100;
display: block;
margin: auto 0;
height: 30%;
width: 100%;
line-height: 0.8;
} 城市名的span有一個特殊的風格,更小的字體權重:
.rb-week span.rb-city {
font-weight: 700;
padding: 1% 10%;
font-size: 1em;
line-height: 1.2;
} 圖標將有一個增加的字體大小,我們需要重置字體權重,因為我們已經改變了它的規則:
.rb-week [class^="icon-"]:before {
font-size: 2.5em;
font-weight: normal;
} 圖標在“當前天氣列”幾乎是透明的:
.rb-week > div:first-child [class^="icon-"] {
opacity: 0.1;
} 現在,讓我們來為每個列表項定義不同的背景顏色。
我們有11個列表項:
/* Colors */
/* Grid */
.rb-grid li:nth-child(1) { background: #3399CC; }
.rb-grid li:nth-child(2) { background: #33CCCC; }
.rb-grid li:nth-child(3) { background: #996699; }
.rb-grid li:nth-child(4) { background: #C24747; }
.rb-grid li:nth-child(5) { background: #e2674a; }
.rb-grid li:nth-child(6) { background: #FFCC66; }
.rb-grid li:nth-child(7) { background: #99CC99; }
.rb-grid li:nth-child(8) { background: #669999; }
.rb-grid li:nth-child(9) { background: #CC6699; }
.rb-grid li:nth-child(10) { background: #339966; }
.rb-grid li:nth-child(11) { background: #666699; } 每個覆蓋層我們有八個列:
/* Overlay Columns */
.rb-grid li:nth-child(1) .rb-week > div:nth-child(1) { background: #3399CC; }
.rb-grid li:nth-child(1) .rb-week > div:nth-child(2) { background: #2D87B4; }
.rb-grid li:nth-child(1) .rb-week > div:nth-child(3) { background: #297AA3; }
.rb-grid li:nth-child(1) .rb-week > div:nth-child(4) { background: #256E93; }
.rb-grid li:nth-child(1) .rb-week > div:nth-child(5) { background: #216283; }
.rb-grid li:nth-child(1) .rb-week > div:nth-child(6) { background: #1D5672; }
.rb-grid li:nth-child(1) .rb-week > div:nth-child(7) { background: #184962; }
.rb-grid li:nth-child(1) .rb-week > div:nth-child(8) { background: #143D52; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(1) { background: #33CCCC; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(2) { background: #2DB4B4; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(3) { background: #29A3A3; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(4) { background: #259393; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(5) { background: #218383; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(6) { background: #1D7272; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(7) { background: #186262; }
.rb-grid li:nth-child(2) .rb-week > div:nth-child(8) { background: #145252; } 最后但并非最不重要,讓我們用小屏幕來檢驗。當空間有限,我們就不希望在網格中顯示盒子了:
@media screen and (max-width: 63.125em) {
.rb-grid li,
.rb-grid li.rb-span-2,
.rb-grid li.rb-span-4 {
width: 100%;
height: 10em;
text-align: left;
}
.rb-grid li[class^="icon-"]:before,
.rb-grid li[class*=" icon-"]:before {
font-size: 6em;
left: auto;
right: 0;
line-height: 2.5;
}
.rb-grid li > div {
text-align: center;
}
} JAVASCRIPT:
var $items = $( '#rb-grid > li' ),
transEndEventNames = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'oTransitionEnd',
'msTransition' : 'MSTransitionEnd',
'transition' : 'transitionend'
},
// transition end event name
transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
// window and body elements
$window = $( window ),
$body = $( 'BODY' ),
// transitions support
supportTransitions = Modernizr.csstransitions,
// current item's index
current = -1,
// window width and height
winsize = getWindowSize(); 當點擊每個條目時,如果不支持過渡效果,就會跳過第一個狀態并且疊加效果會立即擴展。
希望這個詳細講解能對大家有幫助!具體內容可下載源碼。