純CSS3實現(xiàn)3D旋轉(zhuǎn)書本效果
發(fā)布時間:2016-03-21 15:04:15 作者:陳小峰
我要評論

有一些前沿的電商網(wǎng)站已經(jīng)開始使用3D模型來展示商品并支持在線定制,而其中圖書的展示是最為簡單的一種,無需復(fù)雜的建模過程,使用圖片和CSS3的一些變換即可實現(xiàn)更好的展示效果,簡潔而實用,接下來通過本文給大家介紹CSS3實現(xiàn)3D旋轉(zhuǎn)書本效果,需要的朋友一起學(xué)習(xí)
有一些前沿的電商網(wǎng)站已經(jīng)開始使用3D模型來展示商品并支持在線定制,而其中圖書的展示是最為簡單的一種,無需復(fù)雜的建模過程,使用圖片和CSS3的一些變換即可實現(xiàn)更好的展示效果,簡潔而實用。
書本的3D模型是所有商品中最為簡單的,因為其本質(zhì)上就是一個立方體(cube),只是帶有封面/封底和左側(cè)封條。 所以要構(gòu)造一個3D書本展示,問題就被分解為構(gòu)造一個立方體+旋轉(zhuǎn)+圖片背景。
1. 構(gòu)造一個立方體
要創(chuàng)建一個立方體,首先我們需要創(chuàng)建一個虛擬的三維視覺空間,這可以通過設(shè)置包容器元素的perspective屬性獲得。
CSS Code復(fù)制內(nèi)容到剪貼板
- .stage {
- width: 200px;
- height: 260px;
- perspective: 1000px;
- perspective-origin: center center;// 缺省值,可忽略
- }
CSS Code復(fù)制內(nèi)容到剪貼板
- <div class="stage">
- <div class="cube">
- <figure class="back"></figure>
- <figure class="top"></figure>
- <figure class="bottom"></figure>
- <figure class="left"></figure>
- <figure class="right"></figure>
- <figure class="front"></figure>
- </div>
- </div>
我們需要根據(jù)書本的厚度和長寬來確定立方體各個面的坐標(biāo)位置,在本例中所用書本模型(一本MySQL書)的絕對厚度為18.2px,高度260px,寬度197.6px。
那么根據(jù)簡單的幾何知識,前后面距離立方體中心的距離為18.2/2=9.1px,其中“后”元素需要再翻轉(zhuǎn)一下(即“背”過去)。
CSS Code復(fù)制內(nèi)容到剪貼板
- .front {
- transform: translateZ(9.1px);
- }
- .back {
- transform: rotateY(180deg) translateZ(9.1px);
- }
CSS Code復(fù)制內(nèi)容到剪貼板
- .front {
- transform: translateZ(9.1px);
- }
- .back {
- transform: rotateY(180deg) translateZ(9.1px);
- }
- .top {
- transform: rotateX(90deg) rotateZ(90deg) translateZ(98.8px) translateY(-89.7px);
- width: 18.2px;
- height: 197.6px;
- }
- .bottombottom {
- transform: rotateX(-90deg) rotateZ(90deg) translateZ(161.2px) translateY(-89.7px);
- }
- .left {
- transform: rotateY(-90deg) translateZ(9.1px);
- width: 18.2px;
- }
- .rightright {
- transform: rotateY(90deg) translateZ(188.5px);
- width: 18.2px;
- }
接著我們給前后以及左側(cè)面元素添加背景圖(可以使用一張圖,然后從不同的位置截?。o其他3個面添加背景顏色,并給“底”面添加陰影效果:
CSS Code復(fù)制內(nèi)容到剪貼板
- .front {
- transform: translateZ(9.1px);
- background: url("//wow.techbrood.com/uploads/160301/mysql.png") top rightright;
- background-size: auto 100%;
- }
- .back {
- transform: rotateY(180deg) translateZ(9.1px);
- background: url("//wow.techbrood.com/uploads/160301/mysql.png") top left;
- background-size: auto 100%;
- }
- .top {
- transform: rotateX(90deg) rotateZ(90deg) translateZ(98.8px) translateY(-89.7px);
- background: #fafafa;
- width: 18.2px;
- height: 197.6px;
- }
- .bottombottom {
- transform: rotateX(-90deg) rotateZ(90deg) translateZ(161.2px) translateY(-89.7px);
- background: #ccc;
- width: 18.2px;
- height: 197.6px;
- -webkit-filter: drop-shadow(0 0 26px rgba(0, 0, 0, 0.75));
- }
- .left {
- transform: rotateY(-90deg) translateZ(9.1px);
- background: url("//wow.techbrood.com/uploads/160301/mysql.png") top center;
- background-size: auto 100%;
- width: 18.2px;
- }
- .rightright {
- transform: rotateY(90deg) translateZ(188.5px);
- background: #ddd;
- background-size: auto 100%;
- width: 18.2px;
- }
3. 添加旋轉(zhuǎn)動畫
這個比較簡單,使用rotateY方法就可以。
CSS Code復(fù)制內(nèi)容到剪貼板
- @-webkit-keyframes rotate {
- 0% {
- transform: rotateY(0) translateX(-18.2px);
- }
- 100% {
- transform: rotateY(360deg) translateX(-18.2px);
- }
- }
最終的效果圖如下:
關(guān)于CSS3實現(xiàn)3D旋轉(zhuǎn)書本 的全部內(nèi)容小編就給大家介紹這么多,希望對大家有所幫助!
原文:http://blog.csdn.net/iefreer/article/details/50931478
相關(guān)文章
基于HTML5/CSS3實現(xiàn)的書本翻頁3D動畫特效源碼 鼠標(biāo)滑過書本自動翻頁
今天我們要分享一款基于HTML5和CSS3的書本翻頁3D動畫,當(dāng)我們將鼠標(biāo)滑過書本時,書本就會自動一頁頁翻過去,書本的3D效果非常不錯2014-11-06- 這篇文章主要介紹了CSS實現(xiàn)3D書本效果的實例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-06-11