CSS3實(shí)現(xiàn)炫酷的切片式圖片輪播效果

今天我們學(xué)習(xí)如何使用CSS創(chuàng)建一個(gè)炫酷的圖片輪播組件。它的原理簡單的說就是通過單擊標(biāo)簽元素(label)來切換背景圖像和動(dòng)畫效果。核心是使用與標(biāo)簽關(guān)聯(lián)的單選按鈕和使用通用兄弟選擇器來定位每張圖片。
我們要實(shí)現(xiàn)的最終效果是這樣的:
輪播的構(gòu)成
HTML由三個(gè)主要部分組成:單選按鈕和標(biāo)簽、包含四個(gè)面板的容器及其每張圖片的“切片”以及標(biāo)題。設(shè)置為class="cr-bgimg"的容器將為每個(gè)圖片劃分四個(gè)面板,其中,在每個(gè)面板里通過background-position屬性將背景圖片定位在合適的位置上。所以,第一個(gè)面板將有四個(gè)切片,每個(gè)切片都有一個(gè)圖片作為背景,并且位于容器最左邊的位置。第二個(gè)面板也有四個(gè)切片,它位于第一個(gè)面板的右側(cè),用于顯示圖片的第二個(gè)切片部分:
<section class="cr-container"> <!-- 單選按鈕和label標(biāo)簽 --> <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> <!-- 標(biāo)題 --> <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元素中包含兩個(gè)span元素,一個(gè)是主標(biāo)題,一個(gè)是子標(biāo)題。
CSS
為了代碼的簡潔,文章的CSS中都省略了瀏覽器前綴,但是你獲取到的代碼是包含完整的。
我們的第一個(gè)目標(biāo)是實(shí)現(xiàn)點(diǎn)擊label元素的時(shí)候觸發(fā)對(duì)應(yīng)的單選按鈕,方法很簡單,就是將label元素的for屬性值與單選按鈕的ID值對(duì)應(yīng)起來就可以了。
第二步,我們要將所有的背景圖片定位在正確的位置上。第三步,在單擊label標(biāo)簽時(shí),顯示對(duì)應(yīng)的圖片切片和標(biāo)題。
我們一步一步看,首先設(shè)置最外層section元素的樣式,給它設(shè)置一個(gè)淡淡的陰影效果。
.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); }
因?yàn)楹竺嫖覀円褂猛ㄓ眯值苓x擇器“~”來選中對(duì)應(yīng)的圖片切片和標(biāo)題,于是將所有的label元素放在代碼的最上面。通過設(shè)置z-index屬性確保label元素顯示在圖片和文字的上面,并且通過margin-top屬性使它們距離整體上邊框350px。
.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; }
接著,添加一個(gè)小圓圈來美化label元素,我們創(chuàng)建一個(gè)偽元素,并將其放在文字的中心位置。
.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; }
為了在面板與面板之間創(chuàng)建一個(gè)分隔線,我們使用label元素的另一個(gè)偽元素,并通過漸變背景將它從圖片的上邊緣延伸到下邊緣。
.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; }
最后一個(gè)面板的右側(cè)不應(yīng)該有分隔線,所以我們將它的寬度設(shè)置為0。
.cr-container label.cr-label-img-4:after{ width: 0px; }
現(xiàn)在,label標(biāo)簽的樣式就完成了,我們可以將所有的單選按鈕隱藏掉。
.cr-container input{ display: none; }
當(dāng)我們點(diǎn)擊一個(gè)label元素時(shí),它對(duì)應(yīng)的單選按鈕就是被選中。反過來,就可以使用通用兄弟選擇器來選擇選中按鈕對(duì)應(yīng)的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; }
我們還可以改變小圓圈的顏色并添加一個(gè)陰影效果。
.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); }
圖片的容器將占據(jù)所有的寬度,并且絕對(duì)定位。稍后使用這個(gè)容器將背景圖片設(shè)置為當(dāng)前選定的圖片。我們還需要一張默認(rèn)可見的圖片,因此,再添加一些背景屬性:
.cr-bgimg{ width: 600px; height: 400px; position: absolute; left: 0px; top: 0px; z-index: 1; background-repeat: no-repeat; background-position: 0 0; }
因?yàn)槲覀冇兴膫€(gè)面板,每個(gè)面板的寬度為150像素(600除以4)。面板設(shè)置為左浮動(dòng)后會(huì)并排顯示,同時(shí)設(shè)置它們溢出隱藏,因?yàn)槲覀儾幌M诨瑒?dòng)時(shí)看到切片出來:
.cr-bgimg div{ width: 150px; height: 100%; position: relative; float: left; overflow: hidden; background-repeat: no-repeat; }
每個(gè)切片也被設(shè)置為絕對(duì)定位,并且通過left:-150px將它們顯示在面板的外面并隱藏起來:
.cr-bgimg div span{ position: absolute; width: 100%; height: 100%; top: 0px; left: -150px; z-index: 2; text-indent: -9000px; }
再來,讓我們來處理容器和每個(gè)切片的背景圖片:
.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); }
我們還需要根據(jù)不同的面板為切片的背景圖片提供不同的定位:
.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; }
當(dāng)我們單擊一個(gè)label標(biāo)簽時(shí),我們將所有的切片滑到右邊:
.cr-container input:checked ~ .cr-bgimg div span{ animation: slideOut 0.6s ease-in-out; } @keyframes slideOut{ 0%{ left: 0px; } 100%{ left: 150px; } }
但是除了我們選中的這個(gè)label標(biāo)簽,它對(duì)應(yīng)的圖片切片是從-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; }
最后,設(shè)置h3標(biāo)簽中主副標(biāo)題的樣式,當(dāng)我們點(diǎn)擊了某個(gè)label標(biāo)簽后,它對(duì)應(yīng)的標(biāo)題的透明度會(huì)從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; }
就是這樣!切片式的輪播效果就這樣實(shí)現(xiàn)了。當(dāng)然通過這個(gè)效果我們還可以延伸出更多的切片效果,例如:
效果二
效果三
效果四
總結(jié)
以上所述是小編給大家介紹的CSS3實(shí)現(xiàn)炫酷的切片式圖片輪播效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
CSS3實(shí)現(xiàn)帶視差背景漸變效果的平滑圖片輪播幻燈片特效源碼
這是一款基于CSS3實(shí)現(xiàn)帶視差背景漸變效果的平滑圖片輪播幻燈片特效源碼。畫面中心的幻燈片點(diǎn)擊左右切換按鈕、或底部的焦點(diǎn)即可實(shí)現(xiàn)畫面的切換。且圖片平滑輪播切換過程中伴2019-12-04html5+css3制作全屏響應(yīng)式圖片滑動(dòng)輪播預(yù)覽特效源碼
html5 css3制作全屏響應(yīng)式圖片輪播布局,按鈕控制圖片水平滑動(dòng)預(yù)覽查看,圖片支持鼠標(biāo)懸停文字動(dòng)畫展示特效。有需要的朋友可以直接下載使用2018-08-15html5+css3實(shí)現(xiàn)的3D傾斜圖片輪播切換特效源碼
這是一款基于html5+css3實(shí)現(xiàn)的3D傾斜圖片輪播切換特效源碼。各個(gè)海報(bào)圖片可定時(shí)切換,也可點(diǎn)擊右下角的next按鈕切換顯示下一張圖片。右上角的滑塊可調(diào)整顯示圖片的傾斜角度2018-01-08- 這是一款js和CSS3的3D輪播圖特效。6種輪播圖效果,點(diǎn)擊按鈕即可實(shí)現(xiàn)3D翻轉(zhuǎn)輪播特效,歡迎下載2017-08-08
手把手教你用純css3實(shí)現(xiàn)輪播圖效果實(shí)例
本篇文章主要介紹了純css3實(shí)現(xiàn)輪播圖效果實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-04- 圖片輪播效果在各大網(wǎng)站都能用到,本文給大家分享通過純css3制作輪播效果圖的思路,感興趣的朋友一起看看吧2016-08-25
基于jquery+css3實(shí)現(xiàn)的通欄響應(yīng)式圖片輪播動(dòng)畫特效源碼
基于jquery+css3實(shí)現(xiàn)的通欄響應(yīng)式圖片輪播動(dòng)畫特效源碼是一段實(shí)用的產(chǎn)品展示代碼,運(yùn)行流暢,反應(yīng)速度靈敏,是一段非常優(yōu)秀的特效源碼,需要的朋友們可以前來下載使用2014-10-23純CSS實(shí)現(xiàn)的圖片輪播(幻燈片)效果代碼
今天給大家分享一款使用純 CSS 實(shí)現(xiàn)的圖片輪播/幻燈片,切換方式為淡入淡出效果,感興趣的朋友下載源碼吧2020-07-15