使用CSS3怎么實現(xiàn)一個切片式圖片輪播效果?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

css是一種用來表現(xiàn)HTML或XML等文件樣式的計算機語言,主要是用來設(shè)計網(wǎng)頁的樣式,使網(wǎng)頁更加美化。它也是一種定義樣式結(jié)構(gòu)如字體、顏色、位置等的語言,并且css樣式可以直接存儲于HTML網(wǎng)頁或者單獨的樣式單文件中,而樣式規(guī)則的優(yōu)先級由css根據(jù)這個層次結(jié)構(gòu)決定,從而實現(xiàn)級聯(lián)效果,發(fā)展至今,css不僅能裝飾網(wǎng)頁,也可以配合各種腳本對于網(wǎng)頁進(jìn)行格式化。
輪播的構(gòu)成
HTML由三個主要部分組成:單選按鈕和標(biāo)簽、包含四個面板的容器及其每張圖片的“切片”以及標(biāo)題。設(shè)置為class="cr-bgimg"的容器將為每個圖片劃分四個面板,其中,在每個面板里通過background-position屬性將背景圖片定位在合適的位置上。所以,第一個面板將有四個切片,每個切片都有一個圖片作為背景,并且位于容器最左邊的位置。第二個面板也有四個切片,它位于第一個面板的右側(cè),用于顯示圖片的第二個切片部分:
<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"> <h4> <span>Serendipity</span> <span>What you've been dreaming of</span> </h4> <h4> <span>Adventure</span> <span>Where the fun begins</span> </h4> <h4> <span>Nature</span> <span>Unforgettable eperiences</span> </h4> <h4> <span>Serenity</span> <span>When silence touches nature</span> </h4> </div> </section>
h4元素中包含兩個span元素,一個是主標(biāo)題,一個是子標(biāo)題。
CSS
為了代碼的簡潔,文章的CSS中都省略了瀏覽器前綴,但是你獲取到的代碼是包含完整的。
我們的第一個目標(biāo)是實現(xiàn)點擊label元素的時候觸發(fā)對應(yīng)的單選按鈕,方法很簡單,就是將label元素的for屬性值與單選按鈕的ID值對應(yīng)起來就可以了。
第二步,我們要將所有的背景圖片定位在正確的位置上。第三步,在單擊label標(biāo)簽時,顯示對應(yīng)的圖片切片和標(biāo)題。
我們一步一步看,首先設(shè)置最外層section元素的樣式,給它設(shè)置一個淡淡的陰影效果。
.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ī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;
}接著,添加一個小圓圈來美化label元素,我們創(chuàng)建一個偽元素,并將其放在文字的中心位置。
.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)建一個分隔線,我們使用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;
}最后一個面板的右側(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)我們點擊一個label元素時,它對應(yīng)的單選按鈕就是被選中。反過來,就可以使用通用兄弟選擇器來選擇選中按鈕對應(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;
}我們還可以改變小圓圈的顏色并添加一個陰影效果。
.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ù)所有的寬度,并且絕對定位。稍后使用這個容器將背景圖片設(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;
}因為我們有四個面板,每個面板的寬度為150像素(600除以4)。面板設(shè)置為左浮動后會并排顯示,同時設(shè)置它們溢出隱藏,因為我們不希望在滑動時看到切片出來:
.cr-bgimg div{
width: 150px;
height: 100%;
position: relative;
float: left;
overflow: hidden;
background-repeat: no-repeat;
}每個切片也被設(shè)置為絕對定位,并且通過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);
}我們還需要根據(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)我們單擊一個label標(biāo)簽時,我們將所有的切片滑到右邊:
.cr-container input:checked ~ .cr-bgimg div span{
animation: slideOut 0.6s ease-in-out;
}
@keyframes slideOut{
0%{
left: 0px;
}
100%{
left: 150px;
}
}但是除了我們選中的這個label標(biāo)簽,它對應(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è)置h4標(biāo)簽中主副標(biāo)題的樣式,當(dāng)我們點擊了某個label標(biāo)簽后,它對應(yīng)的標(biāo)題的透明度會從0過渡到1:
.cr-titles h4{
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 h4 span:nth-child(1){
font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
font-size: 70px;
display: block;
letter-spacing: 7px;
}
.cr-titles h4 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 h4:nth-child(1),
.cr-container input.cr-selector-img-2:checked ~ .cr-titles h4:nth-child(2),
.cr-container input.cr-selector-img-3:checked ~ .cr-titles h4:nth-child(3),
.cr-container input.cr-selector-img-4:checked ~ .cr-titles h4:nth-child(4){
opacity: 1;
}看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
當(dāng)前文章:使用CSS3怎么實現(xiàn)一個切片式圖片輪播效果-創(chuàng)新互聯(lián)
本文地址:http://chinadenli.net/article6/ccghog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站營銷、ChatGPT、面包屑導(dǎo)航、商城網(wǎng)站、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容