css怎么實(shí)現(xiàn)無限輪播圖動(dòng)畫

這篇文章主要介紹css怎么實(shí)現(xiàn)無限輪播圖動(dòng)畫,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為樂東黎族企業(yè)提供專業(yè)的網(wǎng)站制作、成都網(wǎng)站建設(shè),樂東黎族網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

1、設(shè)置動(dòng)畫的舞臺(tái)

HTML與之前文章里的示例非常相似。我們要有一個(gè)動(dòng)畫發(fā)生的舞臺(tái)(#stage),一個(gè)將會(huì)旋轉(zhuǎn)的div容器和一系列圖像:

<divid="stage">

<divid="rotator"style="-webkit-animation-name:rotator;">

<ahref="1.jpg"><imgsrc="1.jpg"width="140"></a>

<ahref="2.jpg"><imgsrc="2.jpg"width="140"></a>

<ahref="3.jpg"><imgsrc="3.jpg"width="140"></a>

<ahref="4.jpg"><imgsrc="4.jpg"width="140"></a>

<ahref="5.jpg"><imgsrc="5.jpg"width="140"></a>

<ahref="6.jpg"><imgsrc="6.jpg"width="140"></a>

<ahref="7.jpg"><imgsrc="7.jpg"width="140"></a>

<ahref="8.jpg"><imgsrc="8.jpg"width="140"></a>

</div>

</div>

內(nèi)聯(lián)樣式屬性引用下面的動(dòng)畫@keyframes。它需要內(nèi)聯(lián)而不是CSS,以便我們能夠使用JavaScript停止和重新啟動(dòng)動(dòng)畫。

2、在3D空間中布置照片

CSS樣式用于定位多張照片,首先圍繞y軸旋轉(zhuǎn)它們(垂直向上翻頁),然后徑向向外平移:

#stage{

margin:2emauto1em50%;

height:140px;

-webkit-perspective:1200px;

-webkit-perspective-origin:050%;

}

#rotatora{

position:absolute;

left:-81px;

}

#rotatoraimg{

padding:10px;

border:1pxsolid#ccc;

background:#fff;

-webkit-backface-visibility:hidden;

}

#rotatora:nth-of-type(1)img{

-webkit-transform:rotateY(-90deg)translateZ(300px);

}

#rotatora:nth-of-type(2)img{

-webkit-transform:rotateY(-60deg)translateZ(300px);

}

#rotatora:nth-of-type(3)img{

-webkit-transform:rotateY(-30deg)translateZ(300px);

}

#rotatora:nth-of-type(4)img{

-webkit-transform:translateZ(300px);

background:#000;

}

#rotatora:nth-of-type(5)img{

-webkit-transform:rotateY(30deg)translateZ(300px);

}

#rotatora:nth-of-type(6)img{

-webkit-transform:rotateY(60deg)translateZ(300px);

}

#rotatora:nth-of-type(n+7){display:none;}

照片設(shè)置-81px的值代表向左移動(dòng),使前向照片在旋轉(zhuǎn)軸上居中。距離是圖像寬度的一半(140px/2)加上LHS圖像填充(10px)和邊框(1px)。

該階段需要設(shè)置立體的動(dòng)畫,舞臺(tái)從頁面的中心開始,因此旋轉(zhuǎn)元素下的錨元素需要向后移動(dòng)以使動(dòng)畫居中。

我們只開始準(zhǔn)備六張照片,左邊三張,中間一張,右邊兩張。最左側(cè)的照片(位置1)從左側(cè)開始,因此僅在第一次旋轉(zhuǎn)后才可見。

當(dāng)照片旋轉(zhuǎn)時(shí),它會(huì)消失(顯示:無),并且新照片會(huì)附加到左側(cè),準(zhǔn)備從位置1旋轉(zhuǎn)。在7和更高的位置可以有任意數(shù)量的照片。只有當(dāng)它們移動(dòng)到可見位置時(shí),它們才會(huì)出現(xiàn)。

甚至可以在動(dòng)畫進(jìn)行時(shí)使用Ajax加載新照片。

3、添加動(dòng)畫效果

正如我們之前嘗試的那樣,不是將照片輪旋轉(zhuǎn)整整360度,而是我們實(shí)際做的只是旋轉(zhuǎn)30度(足以從一張照片到下一張照片):

@-webkit-keyframesrotator{

from{-webkit-transform:rotateY(0deg);}

to{-webkit-transform:rotateY(30deg);}

}

#rotator{

-webkit-transform-origin:00;

-webkit-transform-style:preserve-3d;

-webkit-animation-timing-function:cubic-bezier(1,0.2,0.2,1);

-webkit-animation-duration:1s;

-webkit-animation-delay:1s;

}

#rotator:hover{

-webkit-animation-play-state:paused;

}

要使關(guān)鍵幀在其他瀏覽器中工作,請(qǐng)復(fù)制所有樣式,替換-webkit-with-moz-和-ms-,如下面的示例代碼塊所示。

動(dòng)畫完成后,它會(huì)觸發(fā)一個(gè)JavaScript事件,您可以在下一節(jié)中閱讀該事件。事件處理程序沿著照片移動(dòng),以便在動(dòng)畫重置時(shí),而不是回到初始狀態(tài),照片都圍繞圓圈移動(dòng)了一步。

為了更清楚地了解正在發(fā)生的事情,中心照片已在下面的演示中突出顯示。在動(dòng)畫發(fā)生時(shí),您將看到突出顯示的節(jié)點(diǎn)旋轉(zhuǎn),然后重置回起始位置,但包含不同的照片。

4、JavaScript添加動(dòng)畫控制器

我們?cè)诖耸纠行枰狫avaScript來檢測動(dòng)畫何時(shí)結(jié)束,以便協(xié)調(diào)通過車輪重置移動(dòng)的照片。沒有這個(gè),輪子只會(huì)在前兩張照片之間交替出現(xiàn)。

document.addEventListener("DOMContentLoaded",function(){

varrotateComplete=function(){

target.style.webkitAnimationName="";

target.insertBefore(arr[arr.length-1],arr[0]);

setTimeout(function(el){

el.style.webkitAnimationName="rotator";

},0,target);

};

vartarget=document.getElementById("rotator");

vararr=target.getElementsByTagName("a");

target.addEventListener("webkitAnimationEnd",rotateComplete,false);

},false);

對(duì)于每個(gè)WebKit樣式和其他引用,存在Firefox(-moz-或Moz),Opera(-o-或O),甚至InternetExplorer(-ms-或ms)的替代品-我們必須忍受的混亂直到標(biāo)準(zhǔn)最終確定。

要在Safari,Chrome,F(xiàn)irefox,Opera和InternetExplorer10中使用此功能,我們需要包含以下額外的設(shè)置:

varrotateComplete=function(){

with(target.style){

webkitAnimationName=MozAnimationName=msAnimationName="";

}

target.insertBefore(arr[arr.length-1],arr[0]);

setTimeout(function(el){

with(el.style){

webkitAnimationName=MozAnimationName=msAnimationName="rotator";

}

},0,target);

};

vartarget=document.getElementById("rotator");

vararr=target.getElementsByTagName("a");

target.addEventListener("webkitAnimationEnd",rotateComplete,false);

target.addEventListener("animationend",rotateComplete,false);

target.addEventListener("MSAnimationEnd",rotateComplete,false);

目前尚不清楚為何需要setTimeout。我們不需要它來設(shè)置延遲,因?yàn)槭褂肅SS完成,但沒有setTimeout(甚至0ms)動(dòng)畫無法重新觸發(fā)。

以上是“css怎么實(shí)現(xiàn)無限輪播圖動(dòng)畫”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站名稱:css怎么實(shí)現(xiàn)無限輪播圖動(dòng)畫
文章來源:http://muchs.cn/article34/ippcpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司建站公司、手機(jī)網(wǎng)站建設(shè)搜索引擎優(yōu)化、ChatGPT

廣告

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

成都網(wǎng)頁設(shè)計(jì)公司