SS3實現球體旋轉
本教程簡述如何用CSS3實現旋轉的球體
效果如下圖所示,球體沿著中間的軸旋轉:
要理解的知識點
1 三維空間的透視屬性
css屬性:perspective
perspective 屬性有兩個值,none 和 自己定義的具體像素,例如1000px。
注意:當為元素定義 perspective 屬性時,其子元素會獲得透視效果,而不是元素本身,所以我們可以把這個屬性理解為照相機和被拍攝物體的距離,CSS3 3D 變換中透視的透視點是在瀏覽器的前方,當我們設置它的值為具體數值的時候,其子元素就會具有近大遠小,近實遠虛的效果。
目前這個屬性對瀏覽器支持還不算太好,只有Chrome 和 Safari 支持替代的 -webkit-perspective 屬性。
2 css屬性transform-style
這個屬性瀏覽器支持情況良好,除了ie瀏覽器,其他瀏覽器都支持。Firefox 支持 transform-style 屬性,而Chrome、Safari 和 Opera 支持替代的 -webkit-transform-style 屬性。
transform-style屬性是設置其子元素是否保留3d位置的。
它也是有兩個值:
1 默認值是flat,表示其子元素不保留3d位置。
2另一個值是preserve-3d,當元素設置了這個值的時候,其子元素就相當于進入了三維空間。
如圖所示:
x軸表示橫向的軸
y軸表示縱向的軸
z軸其實就是我們的視線,是與我們直視的水平面垂直的
如果一個元素沿著x軸旋轉,可以想象成奧運單桿運動員那樣運動~
如果一個元素沿著y軸旋轉,可以想象成公園旋轉木馬沿著中間的柱子繞圈圈~
而元素沿著z軸旋轉的話更好理解了,可以想象成視線正前方,有一個風扇,風扇的扇葉就是沿著z軸旋轉的~
關于3d的一些相關屬性,介紹完畢,那么開始案例制作
結構和樣式
(代碼如下,具體屬性都添加了對應注釋)
效果如圖:
此時我們給球體整體則.ball_in元素沿著x軸向前傾斜和沿著z軸向左傾斜一定角度,則立體感更強。
代碼:
效果如圖:
利用偽元素給球體添加中心軸,代碼如下:
效果如圖:
最后步驟,給每個li添加顏色和整個球體添加旋轉的動畫:
整個球體添加旋轉的動畫代碼如下:
最終效果如圖:
(繞著y軸轉圈圈)
是否覺得五顏六色的球體沒意思?
其實還可以改裝一下,搞點小意思,例如球體不需要那么多個面,然后把剩余的面換成好看的圖片,改裝后效果如圖:
有沒有一種cd在三維空間旋轉的感覺?
又或者我們再改裝一下,效果如下圖:
好了,最后奉上案例源碼,有興趣的朋友可以自行下載。http://bbs.520it.com/ueditor/php/upload/file/20161109/1478670247341112.zip
想要了解小碼哥網頁ui學院更多課程,可以到此鏈接:http://www.520it.com/special/ui
還有,小碼哥論壇bbs.520it.com歡迎您隨時來逛逛
切圖網客戶項目中碰到一個問題,有一個數字需要傾斜,這個便用到了css3的 rotate的旋轉的屬性,沒有用 skew傾斜,因為傾斜以后的文字是被拉伸變形了,但我并不像這樣,下面是css3詳細,另外要說明的是,這個數字做了 滾動到當前屏幕的時候,數字增長。
用到了jquery插件
<script type='text/javascript' src='js/jquery-waypoints-2.0.3.js'></script> <!--滾動視差-->
<script type="text/javascript" src="js/jquery.countTo.js"></script> <!--數字增長-->
共一個參數“角度”,單位deg為度的意思,正數為順時針旋轉,負數為逆時針旋轉,上述代碼作用是順時針旋轉45度。
二.縮放 scale
用法:transform: scale(0.5) 或者 transform: scale(0.5, 2);
參數表示縮放倍數;
一個參數時:表示水平和垂直同時縮放該倍率
兩個參數時:第一個參數指定水平方向的縮放倍率,第二個參數指定垂直方向的縮放倍率。
三.傾斜 skew
用法:transform: skew(30deg) 或者 transform: skew(30deg, 30deg);
參數表示傾斜角度,單位deg
一個參數時:表示水平方向的傾斜角度;
兩個參數時:第一個參數表示水平方向的傾斜角度,第二個參數表示垂直方向的傾斜角度。
四.移動 translate
用法:transform: translate(45px) 或者 transform: translate(45px, 150px);
參數表示移動距離,單位px,
一個參數時:表示水平方向的移動距離;
兩個參數時:第一個參數表示水平方向的移動距離,第二個參數表示垂直方向的移動距離。
五.基準點 transform-origin
在使用transform方法進行文字或圖像的變形時,是以元素的中心點為基準點進行的。使用transform-origin屬性,可以改變變形的基準點。
用法:transform-origin: 10px 10px;
共兩個參數,表示相對左上角原點的距離,單位px,第一個參數表示相對左上角原點水平方向的距離,第二個參數表示相對左上角原點垂直方向的距離;
兩個參數除了可以設置為具體的像素值,其中第一個參數可以指定為left、center、right,第二個參數可以指定為top、center、bottom。
六.多方法組合變形
上面我們介紹了使用transform對元素進行旋轉、縮放、傾斜、移動的方法,這里講介紹綜合使用這幾個方法來對一個元素進行多重變形。
用法:transform: rotate(45deg) scale(0.5) skew(30deg, 30deg) translate(100px, 100px);
這四種變形方法順序可以隨意,但不同的順序導致變形結果不同,原因是變形的順序是從左到右依次進行,這個用法中的執行順序為1.rotate 2.scalse 3.skew 4.translate
原文地址:http://www.qietu.cn/thread-15197-1-1.html (切圖社區)
加微信公眾號:qietuwang (限做前端的人)
現效果視頻:
https://m.toutiaoimg.com/i7012628289806139918/?gd_ext_json=%7B%22enter_from%22%3A%22click_creation_center%22%2C%22category_name%22%3A%22creation_center%22%7D&enter_from=click_creation_center&category_name=creation_center&share_token=c3b59c5c-c95d-43a9-a842-4cd30e321a34&tt_from=copy_link&utm_source=copy_link&utm_medium=toutiao_android&utm_campaign=client_share
實現代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>旋轉、縮放</title>
<style type="text/css">
.box {
width: 750px;
height: 520px;
margin: 50px auto;
background-image: url(狼王-靈夢狼王.jpg);
position: relative;
/* 溢出隱藏 */
overflow: hidden;
}
.box img {
/* 設置圖片位置 */
position: absolute;
top: 0;
left: 0;
}
.img1 {
z-index: 100;
/* 動畫 */
animation: image1 2s linear 1s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image1 {
0% {
transform: scale(1);
}
50% {
/* 縮放,縮小 */
transform: scale(0.5);
}
100% {
transform: scale(0.0001);
}
}
.img2 {
z-index: 98;
/* 動畫 */
animation: image2 2s linear 3s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image2 {
0% {
transform: scale(1);
}
50% {
/* 縮放,縮小 */
transform: scale(1.5);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: scale(5);
opacity: 0;
}
}
.img3 {
z-index: 97;
/* 動畫 */
animation: image3 2s linear 5s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image3 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotate(360deg);
opacity: 0;
}
}
.img4 {
z-index: 96;
/* 動畫 */
animation: image4 2s linear 7s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image4 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotate(-360deg);
opacity: 0;
}
}
.img5 {
z-index: 95;
/* 動畫 */
animation: image5 2s linear 9s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image5 {
0% {
/* 繞y軸旋轉 */
transform: rotateY(0deg);
}
50% {
transform: rotateY(-90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateY(-180deg);
opacity: 0;
}
}
.img6 {
z-index: 94;
/* 動畫 */
animation: image6 2s linear 11s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image6 {
0% {
transform: rotateY(0deg);
}
50% {
transform: rotateY(90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateY(180deg);
opacity: 0;
}
}
.img7 {
z-index: 93;
/* 動畫 */
animation: image7 2s linear 13s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image7 {
0% {
transform: rotateZ(0deg);
}
50% {
transform: rotateZ(180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateZ(360deg);
opacity: 0;
}
}
.img8 {
z-index: 92;
/* 動畫 */
animation: image8 2s linear 15s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image8 {
0% {
transform: rotateZ(0deg);
}
50% {
transform: rotateZ(-180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateZ(-360deg);
opacity: 0;
}
}
.img9 {
z-index: 91;
/* 動畫 */
animation: image9 2s linear 17s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image9 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(-90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateX(-180deg);
opacity: 0;
}
}
.img10 {
z-index: 90;
/* 動畫 */
animation: image10 2s linear 19s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image10 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateX(180deg);
opacity: 0;
}
}
.img11 {
z-index: 89;
/* 動畫 */
animation: image11 2s linear 21s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image11 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img12 {
z-index: 88;
/* 動畫 */
animation: image12 2s linear 23s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image12 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img13 {
z-index: 87;
/* 動畫 */
animation: image13 2s linear 25s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image13 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(-45deg);
}
100% {
transform: rotateX(-90deg);
}
}
.img14 {
z-index: 86;
/* 動畫 */
animation: image14 2s linear 27s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image14 {
0% {
transform: rotateX(0deg);
/* 設置模糊度 */
filter: blur(0px);
}
50% {
transform: rotateX(45deg);
}
100% {
transform: rotateX(90deg);
filter: blur(1);
}
}
.img15 {
z-index: 85;
/* 動畫 */
animation: image15 2s linear 29s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image15 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img16 {
z-index: 84;
/* 動畫 */
animation: image16 2s linear 31s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image16 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img17 {
z-index: 83;
/* 動畫 */
animation: image17 2s linear 33s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image17 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(45deg);
}
100% {
transform: rotateX(90deg);
}
}
.img18 {
z-index: 82;
/* 動畫 */
animation: image18 2s linear 35s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image18 {
0% {
transform: rotateX(0deg);
filter: blur(0px);
}
50% {
transform: rotateX(-45deg);
}
100% {
transform: rotateX(-90deg);
filter: blur(1px);
}
}
.img19 {
z-index: 81;
/* 動畫 */
animation: image19 2s linear 37s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image19 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img20 {
z-index: 80;
/* 動畫 */
animation: image20 2s linear 39s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image20 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img21 {
z-index: 79;
/* 動畫 */
animation: image21 2s linear 41s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image21 {
0% {
transform: rotateY(0deg);
}
50% {
transform: rotateY(45deg);
}
100% {
transform: rotateY(90deg);
}
}
.img22 {
z-index: 78;
/* 動畫 */
animation: image22 2s linear 43s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image22 {
0% {
transform: rotateY(0deg);
filter: blur(0px);
}
50% {
transform: rotateY(-45deg);
}
100% {
transform: rotateY(-90deg);
filter: blur(1px);
}
}
.img23 {
z-index: 77;
/* 動畫 */
animation: image23 2s linear 45s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image23 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img24 {
z-index: 76;
/* 動畫 */
animation: image24 2s linear 47s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image24 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img25 {
z-index: 75;
/* 動畫 */
animation: image25 2s linear 49s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image25 {
0% {
transform: rotateY(0deg);
filter: blur(0px);
}
50% {
transform: rotateY(45deg);
}
100% {
transform: rotateY(90deg);
filter: blur(1px);
}
}
.img26 {
z-index: 74;
/* 動畫 */
animation: image26 2s linear 51s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image26 {
0% {
transform: rotateY(0deg);
}
50% {
transform: rotateY(-45deg);
}
100% {
transform: rotateY(-90deg);
}
}
.img27 {
z-index: 73;
/* 動畫 */
animation: image27 2s linear 53s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left top;
}
@keyframes image27 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img28 {
z-index: 72;
/* 動畫 */
animation: image28 2s linear 55s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right top;
}
@keyframes image28 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img29 {
z-index: 71;
/* 動畫 */
animation: image29 2s linear 57s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image29 {
0% {
transform: rotateZ(0deg) scale(1);
}
50% {
transform: rotateZ(180deg) scale(0.5);
}
100% {
transform: rotateZ(360deg) scale(0.0001);
}
}
.img30 {
z-index: 70;
/* 動畫 */
animation: image30 2s linear 59s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image30 {
0% {
transform: rotateX(0deg) scale(1);
}
50% {
transform: rotateX(180deg) scale(0.5);
}
100% {
transform: rotateX(360deg) scale(0.0001);
}
}
.img31 {
z-index: 69;
/* 動畫 */
animation: image31 2s linear 61s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image31 {
0% {
transform: rotateY(0deg) scale(1);
}
50% {
transform: rotateY(180deg) scale(0.5);
}
100% {
transform: rotateY(360deg) scale(0.0001);
}
}
.img32 {
z-index: 68;
/* 動畫 */
animation: image32 2s linear 63s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image32 {
0% {
transform: scaleX(1);
}
50% {
transform: scaleX(0.5);
}
100% {
transform: scaleX(0.0001);
}
}
.img33 {
z-index: 67;
/* 動畫 */
animation: image33 2s linear 65s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image33 {
0% {
transform: rotateY(0deg) scaleX(1);
}
50% {
transform: rotateY(180deg) scaleX(0.5);
}
100% {
transform: rotateY(360deg) scaleX(0.0001);
}
}
.img34 {
z-index: 66;
/* 動畫 */
animation: image34 2s linear 67s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image34 {
0% {
transform: scaleY(1);
}
50% {
transform: scaleY(0.5);
}
100% {
transform: scaleY(0);
}
}
.img35 {
z-index: 65;
/* 動畫 */
animation: image35 2s linear 69s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image35 {
0% {
transform: rotateY(0deg) scaleY(1);
}
50% {
transform: rotateY(180deg) scaleY(0.5);
}
100% {
transform: rotateY(360deg) scaleY(0);
}
}
.img36 {
z-index: 64;
/* 動畫 */
animation: image36 2s linear 71s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image36 {
0% {
transform: rotate(0deg) scaleY(1);
}
50% {
transform: rotate(180deg) scaleY(1.5);
opacity: 1;
}
100% {
transform: rotate(360deg) scaleY(5);
opacity: 0;
}
}
</style>
</head>
<body>
<div class="box">
<img class="img1" src="狼蛛.jpg" alt="" />
<img class="img2" src="狼蛛-紫魅毒姬.jpg" alt="" />
<img class="img3" src="洛神.jpg" alt="" />
<img class="img4" src="獵魔人.jpg" alt="" />
<img class="img5" src="獵魔人-蜂針魔女.jpg" alt="" />
<img class="img6" src="獵魔人-原力神槍.jpg" alt="" />
<img class="img7" src="獵魔人-摯愛甜心.jpg" alt="" />
<img class="img8" src="羅剎郡主.jpg" alt="" />
<img class="img9" src="羅剎郡主-玫紅冰晶.jpg" alt="" />
<img class="img10" src="羅剎郡主-耀世神皇.jpg" alt="" />
<img class="img11" src="羅剎郡主-櫻落飛翎.jpg" alt="" />
<img class="img12" src="洛神-北境仙姬.jpg" alt="" />
<img class="img13" src="洛神-飛羽女王.jpg" alt="" />
<img class="img14" src="洛神-驚鴻仙子.jpg" alt="" />
<img class="img15" src="綠野花仙.jpg" alt="" />
<img class="img16" src="綠野花仙-精靈公主.jpg" alt="" />
<img class="img17" src="綠野花仙-矩陣天翼.jpg" alt="" />
<img class="img18" src="綠野花仙-綠夢天仙.jpg" alt="" />
<img class="img19" src="魅魔公主.jpg" alt="" />
<img class="img20" src="魅魔公主-煉金魔女.jpg" alt="" />
<img class="img21" src="魅魔公主-夢有靈犀.jpg" alt="" />
<img class="img22" src="魅魔公主-星幻少女.jpg" alt="" />
<img class="img23" src="哪吒.jpg" alt="" />
<img class="img24" src="哪吒-飛輪公主.jpg" alt="" />
<img class="img25" src="哪吒-黃金威靈.jpg" alt="" />
<img class="img26" src="哪吒-輪刃審判.jpg" alt="" />
<img class="img27" src="哪吒-異界仙將.jpg" alt="" />
<img class="img28" src="聶小倩.jpg" alt="" />
<img class="img29" src="聶小倩-黛染幽情.jpg" alt="" />
<img class="img30" src="聶小倩-勾魂燈使.jpg" alt="" />
<img class="img31" src="聶小倩-海洋之心.jpg" alt="" />
<img class="img32" src="聶小倩-綠影魔仙.jpg" alt="" />
<img class="img33" src="聶小倩-仲夏清和.jpg" alt="" />
<img class="img34" src="女武神.jpg" alt="" />
<img class="img35" src="女武神-火羽流炎.jpg" alt="" />
<img class="img36" src="女武神-金槍戰神.jpg" alt="" />
</div>
</body>
</html>
實現效果視頻:
https://www.ixigua.com/i7012628289806139918/
*請認真填寫需求信息,我們會在24小時內與您取得聯系。