如何使用純CSS實現(xiàn)一個旋轉(zhuǎn)魔方輪播效果

這篇文章給大家分享的是有關如何使用純CSS實現(xiàn)一個旋轉(zhuǎn)魔方輪播效果的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司成立十多年來,這條路我們正越走越好,積累了技術與客戶資源,形成了良好的口碑。為客戶提供網(wǎng)站設計制作、網(wǎng)站設計、網(wǎng)站策劃、網(wǎng)頁設計、域名注冊、網(wǎng)絡營銷、VI設計、網(wǎng)站改版、漏洞修補等服務。網(wǎng)站是否美觀、功能強大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設都非常重要,成都創(chuàng)新互聯(lián)公司通過對建站技術性的掌握、對創(chuàng)意設計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。

總的來說我們需要實現(xiàn)以下兩個主要功能:

  • 構建一個能夠旋轉(zhuǎn)的立方體
  • 讓立方體擁有基本輪播所具有的特性

但在完成以上兩點之前我們需要再次了解或熟悉一下實現(xiàn)其功能的CSS3基礎知識點:

  1. transition
  2. transform
  3. perspective
  4. preserve-3d
  5. animation
transition屬性 --- 過渡效果
transition: property duration timing-fucntion delay;

這個屬性應該都很熟悉, 也不過多講, 只是列出其所具有的所有子屬性。

過渡屬性 --- 過渡持續(xù)時間 --- 過渡函數(shù)(曲線) --- 過渡延遲

transition-timing-function: linear|ease|ease-in|ease-out|ease-in-out; 原生具有的基本過渡函數(shù)
transform屬性 --- 對元素進行2D或3D轉(zhuǎn)換

它有幾個常用的變換方法:scale scale3d translate translate3d rotate rotate3d 等。

transform-origin: x-axis y-axis z-axis;  設置旋轉(zhuǎn)元素的基點位置
transform-style: preserve-3d; 讓轉(zhuǎn)換的子元素保留3D轉(zhuǎn)換(與perspective搭配使用)
perspective屬性 --- 讓元素實現(xiàn)3D透=視效果
perspective: 1000px;
    它有兩種寫法    
transform: perspective(1000px);

這個屬性讓物體具有立體的3D透=視效果, 值越大物體離此時我們的眼睛看到屏幕里物體的距離就越遠, 相反若它的值越小, 離我們的視角就越近, 即在屏幕中顯示的大小就越大。它和preserve-3d共同使用在需要實現(xiàn)3D效果的父元素上搭建舞臺視角, 讓其子元素能夠?qū)崿F(xiàn)真正的3D轉(zhuǎn)換。

一個基本的立方體就需要結(jié)合以上三點屬性來實現(xiàn)。

Cube

<div class="cube-wrap">
    <div class="cube">
        <div class="cube-face front"><img src="1.jpg"></div>
        <div class="cube-face back"><img src="2.jpg"></div>
        <div class="cube-face left"><img src="3.jpg"></div>
        <div class="cube-face right"><img src="4.jpg"></div>
        <div class="cube-face top"><img src="5.jpg"></div>
        <div class="cube-face bottom"><img src="6.jpg"></div>
    </div>
</div>
重要的CSS樣式
.cube-wrap{
    width: 300px;
    height: 300px;
    perspective: 1000px;
    position: relative;
}
.cube-wrap .cube{
    width: 100%;
    height: 100%;
    position: absolute;
    transform-style: preserve-3d;
    transition: all .5s ease;
}
.cube-wrap .cube .cube-face{
    width: 100%;
    height: 100%;
    position: absolute;
    overflow: hidden;
    opacity: 0.9;
    border: 1px solid #ccc;
}
.cube-wrap .cube .cube-face img{
    width: 100%;
    height: 100%;
}
.cube-face.front{
    transform: translateZ(150px);
}
.cube-face.back{
    transform: rotateX(180deg) translateZ(150px);
}
.cube-face.left{
    transform: rotateY(-90deg) translateZ(150px);
}
.cube-face.right{
    transform: rotateY(90deg) translateZ(150px);
}
.cube-face.top{
    transform: rotateX(90deg) translateZ(150px);
}
.cube-face.bottom{
    transform: rotateX(-90deg) translateZ(150px);
}

在這里我們給父元素使用了perspective和preserve-3d, 接下來子元素的3D變換效果才會生效。

那么重點來了, 上面的代碼是如何拼接為一個完整的立方體的呢?在瀏覽器開發(fā)者工具里面仔細觀察一下不難發(fā)現(xiàn), 類名為cube元素所在位置是在立方體正中間的那一面。因此我們在如何利用代碼構造立方體的每一面時就有了思路。

首先要清楚, transform相關變換時建立的空間直角坐標系x, y , z軸的方向。

即以電腦屏幕為平面, 水平方向為x軸, 豎直方向為y軸, 垂直于屏幕方向的為z軸。

所以如何構建立方體的六個面就變得很簡單了, cube 面的初始位置在正中間, 整個立方體的長度為 300px, 因此 translateZ(150px) 即為正面。要想構造背面, 則先需要逆時針反轉(zhuǎn)初始面 180deg , 這時候的正面指向背面, 所以只需再 translateZ(150px) 即可。要構造左面則需繞y軸旋轉(zhuǎn)rotateY(-90deg) , 相應的右側(cè)則為rotateY(90deg) ,然后再進行translateZ(150px) 的平移,剩下的兩個面同理按照相應的邏輯進行即可。 需要注意的是當一個面繞軸轉(zhuǎn)動時, 逆時針轉(zhuǎn)動為正值, 順時針為負值。

animation屬性

這個屬性在CSS3動畫中肯定是最重要的了, 它的每一個子屬性都值得我們?nèi)プ屑氀芯俊?/p>

animation: name duration timing-function delay iteration-count direction fill-mode play-state;
animation-delay: 1s;  設置為負值時讓動畫馬上開始, 并且跳過1秒前的動畫
animation-direction: normal|reverse|alternate|alternate-reverse; 定義是否循環(huán)交替反向播放動畫
alternate 動畫在奇數(shù)次(1、3、5...)正向播放, 在偶數(shù)次(2、4、6...)反向播放
alternate-reverse 動畫在奇數(shù)次(1、3、5...)反向播放, 在偶數(shù)次(2、4、6...)正向播放
animation-fill-mode: none|forwards|backwards|both; 規(guī)定當動畫不播放時, 要應用到元素的樣式
forwards 動畫結(jié)束后停留在最后一幀
backwards 在animation-delay期間啟動動畫的第一幀屬性
both 同時實現(xiàn)forwards與backwards的效果
animation-play-state: paused|running; 控制動畫暫停或運行。
@keyframes 設置動畫關鍵幀, 在這里我們用from...to或者百分比來實現(xiàn)自定義的動畫
animation詳解

下面我們給已經(jīng)構建好的立方體添加上animation動畫:

.cube-wrap .cube{
    ......
    animation: spin 10s linear infinite;
}
@keyframes spin {
   from {
       transform: rotateX(45deg) rotateY(45deg);
   }
   to {
       transform: rotateX(405deg) rotateY(765deg);
   }
}

Carousel

現(xiàn)在我們已經(jīng)實現(xiàn)了能夠自由旋轉(zhuǎn)的立方體效果了, 接下來就需要完成輪播所具有的基本功能。

  1. 左右按鈕切換
  2. 底部按鈕切換

在實現(xiàn)這兩個功能之前我們需要了解一下兩個強大的HTML標簽, 它們的配合使用實現(xiàn)了輪播圖中點擊切換的效果。它們就是label和input標簽, 先來看看它們的基本用法。

<input type="radio" id="1">
<label for="1" ></label> 點擊label標簽, id為1的input標簽被選中

這里label標簽中的for與input標簽中的id相關聯(lián), 而input標簽中type為radio時是選擇框的效果, 它具有一個checked的屬性 (若要實現(xiàn)單選框的效果, 則需要設置name="xxx" ,此時的名稱要一致, 下文就用到了這個效果)

現(xiàn)在就來開始實現(xiàn)具體的效果吧。

<div class="container">
    <div class="cube-wrap">
        <input type="radio" name="cuber" class="controller" id="1" checked="true">
        <input type="radio" name="cuber" class="controller" id="2">
        <input type="radio" name="cuber" class="controller" id="3">
        <input type="radio" name="cuber" class="controller" id="4">
        <input type="radio" name="cuber" class="controller" id="5">
        <input type="radio" name="cuber" class="controller" id="6">
        <div class="cube">
          ......
        </div>
        <div class="cube_left">
            <label for="6" class="cube_action"></label>
            <label for="1" class="cube_action"></label>
            <label for="2" class="cube_action"></label>
            <label for="3" class="cube_action"></label>
            <label for="4" class="cube_action"></label>
            <label for="5" class="cube_action"></label>
        </div>
        <div class="cube_right">
            <label for="2" class="cube_action"></label>
            <label for="3" class="cube_action"></label>
            <label for="4" class="cube_action"></label>
            <label for="5" class="cube_action"></label>
            <label for="6" class="cube_action"></label>
            <label for="1" class="cube_action"></label>
        </div>
        <div class="indicators">
            <label for="1" class="indicator"></label>
            <label for="2" class="indicator"></label>
            <label for="3" class="indicator"></label>
            <label for="4" class="indicator"></label>
            <label for="5" class="indicator"></label>
            <label for="6" class="indicator"></label>
        </div>
    </div>
</div>
先實現(xiàn)左右和底部的CSS樣式
.cube_left .cube_action{
    left: -75px;
    top: 50%;
    transform: translateY(-50%);
}
.cube_right .cube_action{
    right: -75px;
    top: 50%;
    transform: translateY(-50%);
}
.cube_action{
    background-color: #fafafa;
    border-radius: 50%;
    cursor: pointer;
    display: none;
    width: 40px;
    height: 40px;
    opacity: 0.15;
    position: absolute;
    transition: opacity 0.5s ease;
    z-index: 5;
}
.cube_action:hover{
    opacity: 1;
}
.cube_action::before{
    border-bottom: 4px solid #111;
    border-right: 4px solid #111;
    content: '';
    display: block;
    height: 25%;
    left: 50%;
    position: absolute;
    top: 50%;
    width: 25%;
    transform: translate(-70%, -50%) rotate(-45deg);
}
.cube_left .cube_action::before{
    transform: translate(-40%, -50%) rotate(135deg);
}
.indicators{
    position: absolute;
    left: 0;
    right: 0;
    bottom: -80px;
    padding: 20px;
    text-align: center;
    opacity:0;
    transition: opacity .3s;
}
.container:hover .indicators{
    opacity: 1;
}
.indicators .indicator{
    background-color: #fafafa;
    border-radius: 50%;
    cursor: pointer;
    display: inline-block;
    width: 14px;
    height: 14px;
    margin: 6px;
    opacity: .15;
}
.controller{
    display: none;
}

寫完上面的代碼后并不能看到我們想要的結(jié)果, 因為它們都需要hover事件來觸發(fā)。

現(xiàn)在我們來設置最外層 container 的樣式以及定義一個入場動畫。

.container{
    width: 600px;
    height: 600px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -300px;
    margin-left: -300px;
    transition: all .5s ease;
    transform: scale(0.25);
}
.container:hover {
    transform: scale(1);
}
.container:hover .cube-wrap .cube{
    animation: entrance .5s ease ;
}
@keyframes entrance {
    from {
        transform: rotateX(-225deg) rotateY(-225deg);
    }
}

當鼠標移入立方體時, 動畫由spin被替換為entrance 。

那么重點再次出現(xiàn)了, 到底CSS是如何實現(xiàn)點擊切換輪播圖片的呢?

原理很簡單, 其實就是搭配前面提到的label標簽和input標簽從而實現(xiàn)了驚人的效果。

.controller:nth-of-type(1):checked ~ .cube{
    transform: translateZ(-150px);
}
.controller:nth-of-type(2):checked ~ .cube{
    transform: translateZ(-150px) rotateX(-180deg) ;
}
.controller:nth-of-type(3):checked ~ .cube{
    transform: translateZ(-150px) rotateY(90deg) ;
}
.controller:nth-of-type(4):checked ~ .cube{
    transform: translateZ(-150px) rotateY(-90deg) ;
}
.controller:nth-of-type(5):checked ~ .cube{
    transform: translateZ(-150px) rotateX(-90deg) ;
}
.controller:nth-of-type(6):checked ~ .cube{
    transform: translateZ(-150px) rotateX(90deg) ;
}

無論是點擊左右的按鈕, 還是點擊底部的按鈕, 我們都觸發(fā)了label標簽的for屬性從而聯(lián)動了對應的input標簽中的checked屬性。

至于該如何將對應的那一面反轉(zhuǎn)到正對屏幕的這一面, 只需要在構造立方體每一面的轉(zhuǎn)換中將符號反向即可。

值得注意的是這里我們運用的CSS選擇器也算是一個技巧, :nth-of-type(n) 選擇的是相同類型標簽的第n個標簽, ~符號選擇的是同級中的標簽。

現(xiàn)在我們回過頭來再仔細看下開頭的HTML結(jié)構, indicators 里面的label標簽中的for好像能夠明白其邏輯, 即點擊哪一個標簽就觸發(fā)哪一個input標簽的checked屬性從而進行相應的3D轉(zhuǎn)換。 可是左右按鈕中的label標簽里的for數(shù)字順序咋看起來不對勁呢?

在這里我自己也琢磨了很久, 費了很大的功夫才想明白原來.cube_left或者.cube_right 中相應的6個label標簽是重合在一起的, 而且都為display:none , 這就很有意思了, 來看看接下來的代碼。

.container:hover .controller:nth-of-type(1):checked ~ .cube_left .cube_action:nth-of-type(1), 
.container:hover .controller:nth-of-type(1):checked ~ .cube_right .cube_action:nth-of-type(1){
    display: block;
}
.container:hover .controller:nth-of-type(2):checked ~ .cube_left .cube_action:nth-of-type(2),
.container:hover .controller:nth-of-type(2):checked ~ .cube_right .cube_action:nth-of-type(2){
    display: block;
}
......
......
......
.container:hover .controller:nth-of-type(6):checked ~ .cube_left .cube_action:nth-of-type(6),
.container:hover .controller:nth-of-type(6):checked ~ .cube_right .cube_action:nth-of-type(6){
    display: block;
}

現(xiàn)在我們默認的是 controller 中的第一個元素被選中, 即它的checked屬性為true。因此左右按鈕里label標簽中的第一個顯示為display:block , 若現(xiàn)在點擊左邊的按鈕, 我們希望立方體的底部呈現(xiàn)在屏幕的正面, 所以for應該設置為6。若點擊右邊按鈕其第一個label標簽的for應該設置為2。按照這個邏輯, 我們也就明白了為什么.cube_left 或.cube_right中的for屬性是亂序的原因了。

感謝各位的閱讀!關于“如何使用純CSS實現(xiàn)一個旋轉(zhuǎn)魔方輪播效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

標題名稱:如何使用純CSS實現(xiàn)一個旋轉(zhuǎn)魔方輪播效果
URL分享:http://muchs.cn/article6/ipjoog.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、動態(tài)網(wǎng)站、網(wǎng)站改版網(wǎng)站排名、軟件開發(fā)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設