在css3中,可以使用transform屬性來配合rotateY()、rotateX()等3d旋轉函數來實現3d翻轉效果。 rotateX()可以使元素繞其X軸旋轉給定角度,rotateY()可以使元素繞其Y軸旋轉給定角度。
本教學操作環境:windows7系統、CSS3&&HTML5版、Dell G3電腦。
一、實作一張圖片的翻轉
1、HTML結構
<div class="stage"> <div class="flipBox"> <figure class="pic front">Front</figure> <figure class="pic back">Back</figure> </div> </div>
上述HTML的結構是:
- p.stage規定了一個3D舞台,基本上所有使用CSS3 3D變換的實作都會這麼做,規定perspective樣式從而達到透視效果 ##p.flipBox是真正實現翻面的容器,稍後將對它進行3D變換
- figure代表兩張圖片,一張是正面,一張是背面
3、CSS結構
body,figure { margin: 0; padding: 0; } .stage { width: 200px; height: 100px; margin: 40px; perspective: 1000px; } .flipBox { width: 200px; height: 100px; position: relative; transform-style: preserve-3d; transition: transform 1s; } .pic { width: 200px; height: 100px; font-size: 24px; color: #fff; line-height: 100px; text-align: center; position: absolute; top: 0; left: 0; backface-visibility: hidden; } .front { background: #f00; } .back { background: #090; transform: rotateY(180deg); }現在分析每個元素的CSS:
body,figure { margin: 0; padding: 0; }沒什麼好說的,去掉內外邊距!
.stage { width: 200px; height: 100px; margin: 40px; perspective: 1000px; }為3D舞台定義樣式。 margin是為了距離瀏覽器左邊和上邊有一些距離,讓變換顯示的更完整。 perspective規定了3D元素與攝影機(或人眼)的距離,數值越小3D元素離人眼越近,數值越大3D元素離人眼越遠。
.flipBox { width: 200px; height: 100px; position: relative; transform-style: preserve-3d; transition: transform 1s; }為翻轉盒子定義樣式。這個元素是真正進行3D變換的元素。其position屬性是為其兩個子figure元素創造定位點,以便兩個子figure元素定位到p.flipBox的左上角實現兩張圖片的對齊。 transform-style屬性是必須的,這規定了p.flipBox元素的後代元素是以哪種形式進行3D變換(preserve-3d表示後代元素任然以3d的模式進行變換;另一個值flat表示只對p .flipBox進行3D變換,後代元素則只是p.flipBox平面中的內容,不進行3D變換),這和After Effect中的偽3D十分相似。 transition規定只變換transform屬性,時間為1s.
.pic { width: 200px; height: 100px; font-size: 24px; color: #fff; line-height: 100px; text-align: center; position: absolute; top: 0; left: 0; backface-visibility: hidden; }為兩張圖片(這裡的兩個figure)規定統一的樣式。使用絕對定位,定位到p.flipBox的左上角,而兩個figure的大小又是一樣的,所以完美重疊。 backface-visibility是一個重要的屬性,它規定背對使用者的3D元素是否顯示,這裡應該規定為不顯示(hidden),否則不該顯示背面的時候背面會顯示出來。例如初始狀態,顯然不應該顯示figure.back,但又因為figure.back是後渲染的,所以會覆蓋在figure.front上,我們之前為figure.back規定了transform: rotateY(180deg),所以figure. front是背對使用者的,將不顯示。再例如翻轉過後,figure.front會擋在figure.back前面,不過此時figure.front將會背對用戶,所以被backface-visibility隱藏了,這正是我們想要的。
.front { background: #f00; }規定了圖片正面為紅色。
.back { background: #090; transform: rotateY(180deg); }規定了圖片背面為綠色,同時,transform: rotateY(180deg)規定在初始狀態,figure.back是水平翻轉180°的。
3、開始旋轉圖片
.stage:hover .flipBox { transform: rotateY(-180deg); }當滑鼠移入3D舞台時,將p.flipBox旋轉-180°,實現圖片翻轉效果。這裡讓p.flipBox旋轉 180°也是可以的,但旋轉的方向不同罷了。
二、案例
#1、圖片準備
為減少HTTP請求,這裡使用精靈圖。 圖片大小為200*200,分上下兩部分,上方為翻轉圖片的正面(黑白),下方為翻轉圖片的背面(彩色)。上方和下方的logo都經過水平居中和垂直居中,以確保翻轉前後logo位置一致。
2、程式碼實作
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>JavaScript Study</title> <style> html,body,ul,li,a,figure,h4 { padding: 0; margin: 0; } ul { list-style: none; } h4 { display: none; } .Stage { width: 604px; height: 203px; margin: 50px; border-left: 1px solid #f5f5f5; border-top: 1px solid #f5f5f5; perspective: 10000px; } .trigger { display: block; float: left; width: 200px; height:100px; border-right: 1px solid #f5f5f5; border-bottom: 1px solid #f5f5f5; position: relative; } .flipBox { display: block; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform 1.2s; transition-delay: 0.03s; } .trigger:hover .flipBox { transform: perspective(10000px) rotateY(-180deg); /*这里的perspective为每个flipBox规定单独的视点距离,解决Chrome中统一视点的问题*/ } .plane { width: 200px; height: 100px; position: absolute; top: 0; left: 0; backface-visibility: hidden; } .back { transform: rotateY(180deg); } .logo1 figure.front { background: url("pic.png") center 0 no-repeat; } .logo2 figure.front { background: url("pic_2.png") center 0 no-repeat; } .logo3 figure.front { background: url("pic_3.png") center 0 no-repeat; } .logo4 figure.front { background: url("pic_4.png") center 0 no-repeat; } .logo5 figure.front { background: url("pic_5.png") center 0 no-repeat; } .logo6 figure.front { background: url("pic_6.png") center 0 no-repeat; } .logo1 figure.back { background: url("pic.png") center -100px no-repeat; } .logo2 figure.back { background: url("pic_2.png") center -100px no-repeat; } .logo3 figure.back { background: url("pic_3.png") center -100px no-repeat; } .logo4 figure.back { background: url("pic_4.png") center -100px no-repeat; } .logo5 figure.back { background: url("pic_5.png") center -100px no-repeat; } .logo6 figure.back { background: url("pic_6.png") center -100px no-repeat; } </style> </head> <body> <div> <ul> <li> <a class="flipBox logo1" href="#"> <h4 id="Fun-nbsp-Games">Fun Games</h4> <figure class="plane front"></figure> <figure class="plane back"></figure> </a> </li> <li> <a class="flipBox logo2" href="#"> <h4 id="Man-nbsp-Style">Man Style</h4> <figure class="plane front"></figure> <figure class="plane back"></figure> </a> </li> <li> <a class="flipBox logo3" href="#"> <h4 id="Sims">Sims.</h4> <figure class="plane front"></figure> <figure class="plane back"></figure> </a> </li> <li> <a class="flipBox logo4" href="#"> <h4 id="Googla">Googla</h4> <figure class="plane front"></figure> <figure class="plane back"></figure> </a> </li> <li> <a class="flipBox logo5" href="#"> <h4 id="JavaScript">JavaScript</h4> <figure class="plane front"></figure> <figure class="plane back"></figure> </a> </li> <li> <a class="flipBox logo6" href="#"> <h4 id="Felix">Felix</h4> <figure class="plane front"></figure> <figure class="plane back"></figure> </a> </li> </ul> </div> </body> </html>
css影片教學)
以上是css3怎麼達到3d翻轉效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

Wufoo一直在集成方面非常出色。他們與特定應用程序(例如廣告系列顯示器,MailChimp和Typekit)進行集成,但他們也


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能