国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > CSS > 正文

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

2024-07-11 09:00:00
字體:
供稿:網(wǎng)友

今天我們學(xué)習如何使用CSS創(chuàng)建一個炫酷的圖片輪播組件。它的原理簡單的說就是通過單擊標簽元素(label)來切換背景圖像和動畫效果。核心是使用與標簽關(guān)聯(lián)的單選按鈕和使用通用兄弟選擇器來定位每張圖片。

我們要實現(xiàn)的最終效果是這樣的:

輪播的構(gòu)成

HTML由三個主要部分組成:單選按鈕和標簽、包含四個面板的容器及其每張圖片的“切片”以及標題。設(shè)置為class="cr-bgimg"的容器將為每個圖片劃分四個面板,其中,在每個面板里通過background-position屬性將背景圖片定位在合適的位置上。所以,第一個面板將有四個切片,每個切片都有一個圖片作為背景,并且位于容器最左邊的位置。第二個面板也有四個切片,它位于第一個面板的右側(cè),用于顯示圖片的第二個切片部分:

 

<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

為了代碼的簡潔,文章的CSS中都省略了瀏覽器前綴,但是你獲取到的代碼是包含完整的。

我們的第一個目標是實現(xiàn)點擊label元素的時候觸發(fā)對應(yīng)的單選按鈕,方法很簡單,就是將label元素的for屬性值與單選按鈕的ID值對應(yīng)起來就可以了。

第二步,我們要將所有的背景圖片定位在正確的位置上。第三步,在單擊label標簽時,顯示對應(yīng)的圖片切片和標題。

我們一步一步看,首先設(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)的圖片切片和標題,于是將所有的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標簽的樣式就完成了,我們可以將所有的單選按鈕隱藏掉。

.cr-container input{    display: none;}

當我們點擊一個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è)置為當前選定的圖片。我們還需要一張默認可見的圖片,因此,再添加一些背景屬性:

.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;}

 

當我們單擊一個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標簽,它對應(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標簽中主副標題的樣式,當我們點擊了某個label標簽后,它對應(yīng)的標題的透明度會從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;}

 

就是這樣!切片式的輪播效果就這樣實現(xiàn)了。當然通過這個效果我們還可以延伸出更多的切片效果,例如:

效果二


 

效果三

效果四

總結(jié)

以上所述是小編給大家介紹的CSS3實現(xiàn)炫酷的切片式圖片輪播效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊通| 玉龙| 磴口县| 高邑县| 浦江县| 长兴县| 广汉市| 利津县| 宁海县| 乌鲁木齐县| 临清市| 无极县| 泗阳县| 垫江县| 象山县| 平陆县| 卢湾区| 赞皇县| 都匀市| 石棉县| 木兰县| 盘山县| 拜城县| 元谋县| 武定县| 莆田市| 吴川市| 建阳市| 广元市| 长武县| 辽阳县| 咸丰县| 伊吾县| 库车县| 梓潼县| 监利县| 公主岭市| 澄江县| 喀喇沁旗| 滦南县| 鹤岗市|