今天咱們學習如何使用CSS建立一個炫酷的圖片輪播組件。它的原理簡單的說就是經過單擊標籤元素(label)來切換背景圖像和動畫效果。核心是使用與標籤關聯的單選按鈕和使用通用兄弟選擇器來定位每張圖片。瀏覽器
咱們要實現的最終效果是這樣的:ide
HTML由三個主要部分組成:單選按鈕和標籤、包含四個面板的容器及其每張圖片的「切片」以及標題。設置爲class="cr-bgimg"的容器將爲每一個圖片劃分四個面板,其中,在每一個面板裏經過background-position屬性將背景圖片定位在合適的位置上。因此,第一個面板將有四個切片,每一個切片都有一個圖片做爲背景,而且位於容器最左邊的位置。第二個面板也有四個切片,它位於第一個面板的右側,用於顯示圖片的第二個切片部分:學習
<section class="cr-container"> <!-- 單選按鈕和label標籤 --> <input id="select-img-1" name="radio-set-1" type="radio" class="cr-selector-img-1" checked/> <label for="select-img-1" class="cr-label-img-1">1</label> <input id="select-img-2" name="radio-set-1" type="radio" class="cr-selector-img-2" /> <label for="select-img-2" class="cr-label-img-2">2</label> <input id="select-img-3" name="radio-set-1" type="radio" class="cr-selector-img-3" /> <label for="select-img-3" class="cr-label-img-3">3</label> <input id="select-img-4" name="radio-set-1" type="radio" class="cr-selector-img-4" /> <label for="select-img-4" class="cr-label-img-4">4</label> <div class="clr"></div> <!-- 面板 --> <div class="cr-bgimg"> <div> <span>Slice 1 - Image 1</span> <span>Slice 1 - Image 2</span> <span>Slice 1 - Image 3</span> <span>Slice 1 - Image 4</span> </div> <div> <span>Slice 2 - Image 1</span> <span>Slice 2 - Image 2</span> <span>Slice 2 - Image 3</span> <span>Slice 2 - Image 4</span> </div> <div> <span>Slice 3 - Image 1</span> <span>Slice 3 - Image 2</span> <span>Slice 3 - Image 3</span> <span>Slice 3 - Image 4</span> </div> <div> <span>Slice 4 - Image 1</span> <span>Slice 4 - Image 2</span> <span>Slice 4 - Image 3</span> <span>Slice 4 - Image 4</span> </div> </div> <!-- 標題 --> <div class="cr-titles"> <h3> <span>Serendipity</span> <span>What you've been dreaming of</span> </h3> <h3> <span>Adventure</span> <span>Where the fun begins</span> </h3> <h3> <span>Nature</span> <span>Unforgettable eperiences</span> </h3> <h3> <span>Serenity</span> <span>When silence touches nature</span> </h3> </div> </section>
h3元素中包含兩個span元素,一個是主標題,一個是子標題。動畫
爲了代碼的簡潔,文章的CSS中都省略了瀏覽器前綴,可是你獲取到的代碼是包含完整的。url
咱們的第一個目標是實現點擊label元素的時候觸發對應的單選按鈕,方法很簡單,就是將label元素的for屬性值與單選按鈕的ID值對應起來就能夠了。spa
第二步,咱們要將全部的背景圖片定位在正確的位置上。第三步,在單擊label標籤時,顯示對應的圖片切片和標題。code
咱們一步一步看,首先設置最外層section元素的樣式,給它設置一個淡淡的陰影效果。圖片
.cr-container{ width: 600px; height: 400px; position: relative; margin: 0 auto; border: 20px solid #fff; box-shadow: 1px 1px 3px rgba(0,0,0,0.1); }
由於後面咱們要使用通用兄弟選擇器「~」來選中對應的圖片切片和標題,因而將全部的label元素放在代碼的最上面。經過設置z-index屬性確保label元素顯示在圖片和文字的上面,而且經過margin-top屬性使它們距離總體上邊框350px。ip
.cr-container label{ font-style: italic; width: 150px; height: 30px; cursor: pointer; color: #fff; line-height: 32px; font-size: 24px; float:left; position: relative; margin-top: 350px; z-index: 1000; }
接着,添加一個小圓圈來美化label元素,咱們建立一個僞元素,並將其放在文字的中心位置。ci
.cr-container label:before{ content:''; width: 34px; height: 34px; background: rgba(130,195,217,0.9); position: absolute; left: 50%; margin-left: -17px; border-radius: 50%; box-shadow: 0px 0px 0px 4px rgba(255,255,255,0.3); z-index:-1; }
爲了在面板與面板之間建立一個分隔線,咱們使用label元素的另外一個僞元素,並經過漸變背景將它從圖片的上邊緣延伸到下邊緣。
.cr-container label:after{ width: 1px; height: 400px; content: ''; background: linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); position: absolute; bottom: -20px; right: 0px; }
最後一個面板的右側不該該有分隔線,因此咱們將它的寬度設置爲0。
.cr-container label.cr-label-img-4:after{ width: 0px; }
如今,label標籤的樣式就完成了,咱們能夠將全部的單選按鈕隱藏掉。
.cr-container input{ display: none; }
當咱們點擊一個label元素時,它對應的單選按鈕就是被選中。反過來,就可使用通用兄弟選擇器來選擇選中按鈕對應的label元素,並改變它的文字顏色。
.cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1, .cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2, .cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3, .cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4{ color: #68abc2; }
咱們還能夠改變小圓圈的顏色並添加一個陰影效果。
.cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1:before, .cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2:before, .cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3:before, .cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4:before{ background: #fff; box-shadow: 0px 0px 0px 4px rgba(104,171,194,0.6); }
圖片的容器將佔據全部的寬度,而且絕對定位。稍後使用這個容器將背景圖片設置爲當前選定的圖片。咱們還須要一張默承認見的圖片,所以,再添加一些背景屬性:
.cr-bgimg{ width: 600px; height: 400px; position: absolute; left: 0px; top: 0px; z-index: 1; background-repeat: no-repeat; background-position: 0 0; }
由於咱們有四個面板,每一個面板的寬度爲150像素(600除以4)。面板設置爲左浮動後會並排顯示,同時設置它們溢出隱藏,由於咱們不但願在滑動時看到切片出來:
.cr-bgimg div{ width: 150px; height: 100%; position: relative; float: left; overflow: hidden; background-repeat: no-repeat; }
每一個切片也被設置爲絕對定位,而且經過left:-150px將它們顯示在面板的外面並隱藏起來:
.cr-bgimg div span{ position: absolute; width: 100%; height: 100%; top: 0px; left: -150px; z-index: 2; text-indent: -9000px; }
再來,讓咱們來處理容器和每一個切片的背景圖片:
.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg, .cr-bgimg div span:nth-child(1){ background-image: url(../images/1.jpg); } .cr-container input.cr-selector-img-2:checked ~ .cr-bgimg, .cr-bgimg div span:nth-child(2){ background-image: url(../images/2.jpg); } .cr-container input.cr-selector-img-3:checked ~ .cr-bgimg, .cr-bgimg div span:nth-child(3){ background-image: url(../images/3.jpg); } .cr-container input.cr-selector-img-4:checked ~ .cr-bgimg, .cr-bgimg div span:nth-child(4){ background-image: url(../images/4.jpg); }
咱們還須要根據不一樣的面板爲切片的背景圖片提供不一樣的定位:
.cr-bgimg div:nth-child(1) span{ background-position: 0px 0px; } .cr-bgimg div:nth-child(2) span{ background-position: -150px 0px; } .cr-bgimg div:nth-child(3) span{ background-position: -300px 0px; } .cr-bgimg div:nth-child(4) span{ background-position: -450px 0px; }
當咱們單擊一個label標籤時,咱們將全部的切片滑到右邊:
.cr-container input:checked ~ .cr-bgimg div span{ animation: slideOut 0.6s ease-in-out; } @keyframes slideOut{ 0%{ left: 0px; } 100%{ left: 150px; } }
可是除了咱們選中的這個label標籤,它對應的圖片切片是從-150px滑到0px:
.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg div span:nth-child(1), .cr-container input.cr-selector-img-2:checked ~ .cr-bgimg div span:nth-child(2), .cr-container input.cr-selector-img-3:checked ~ .cr-bgimg div span:nth-child(3), .cr-container input.cr-selector-img-4:checked ~ .cr-bgimg div span:nth-child(4) { transition: left 0.5s ease-in-out; animation: none; left: 0px; z-index: 10; }
最後,設置h3標籤中主副標題的樣式,當咱們點擊了某個label標籤後,它對應的標題的透明度會從0過渡到1:
.cr-titles h3{ position: absolute; width: 100%; text-align: center; top: 50%; z-index: 10000; opacity: 0; color: #fff; text-shadow: 1px 1px 1px rgba(0,0,0,0.1); transition: opacity 0.8s ease-in-out; } .cr-titles h3 span:nth-child(1){ font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif; font-size: 70px; display: block; letter-spacing: 7px; } .cr-titles h3 span:nth-child(2){ letter-spacing: 0px; display: block; background: rgba(104,171,194,0.9); font-size: 14px; padding: 10px; font-style: italic; font-family: Cambria, Palatino, "Palatino Linotype", "Palatino LT STD", Georgia, serif; } .cr-container input.cr-selector-img-1:checked ~ .cr-titles h3:nth-child(1), .cr-container input.cr-selector-img-2:checked ~ .cr-titles h3:nth-child(2), .cr-container input.cr-selector-img-3:checked ~ .cr-titles h3:nth-child(3), .cr-container input.cr-selector-img-4:checked ~ .cr-titles h3:nth-child(4){ opacity: 1; }
就是這樣!切片式的輪播效果就這樣實現了。固然經過這個效果咱們還能夠延伸出更多的切片效果,例如:
效果二
效果三
效果四
要獲取以上輪播效果的代碼能夠: