HTML5 第三天
一、 認識 3D 轉(zhuǎn)換
3D 的特點 三維坐標系 -
x 軸:水平向右 – 注意:x 軸右邊是正值,左邊是負值
-
y 軸:垂直向下 – 注意:y 軸下面是正值,上面是負值
-
z 軸:垂直屏幕 – 注意:往外邊的是正值,往里面的是負值
?
二、3D 轉(zhuǎn)換
3D 轉(zhuǎn)換知識要點
- 3D 位移:translate3d(x, y, z)
- 3D 旋轉(zhuǎn):rotate3d(x, y, z)
- 透視:perspctive
- 3D呈現(xiàn) transfrom-style
3D 移動 translate3d
- 3D 移動就是在 2D 移動的基礎(chǔ)上多加了一個可以移動的方向,就是 z 軸方向
- transform: translateX(100px):僅僅是在 x 軸上移動
- transform: translateY(100px):僅僅是在 y 軸上移動
- transform: translateZ(100px):僅僅是在 z 軸上移動
- transform: translate3d(x, y, z):其中x、y、z 分別指要移動的軸的方向的距離
- 注意:x, y, z 對應(yīng)的值不能省略,不需要填寫用 0 進行填充
語法
transform: translate3d(x, y, z
)
代碼演示
transform: translate3d(100px, 100px, 100px
)
transform: translate3d(100px, 100px, 0
)
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document
</title><style>body {perspective: 200px;}div {width: 200px;height: 200px;background-color: pink;transform: translate3d(400px, 100px, 100px);}</style>
</head><body><div></div>
</body></html>
三、透視 perspective
知識點講解
- 如果想要網(wǎng)頁產(chǎn)生 3D 效果需要透視(理解成 3D 物體投影的 2D 平面上)
- 實際上模仿人類的視覺位置,可視為安排一直眼睛去看
- 透視也稱為視距,所謂的視距就是人的眼睛到屏幕的距離
- 距離視覺點越近的在電腦平面成像越大,越遠成像越小
- 透視的單位是像素
知識要點
?
代碼演示
body {perspective: 1000px;
}
四、 translateZ
translateZ 與 perspecitve 的區(qū)別
- perspecitve 給父級進行設(shè)置,translateZ 給 子元素進行設(shè)置不同的大小
五、3D 旋轉(zhuǎn)rotateX
3D 旋轉(zhuǎn)指可以讓元素在三維平面內(nèi)沿著 x 軸、y 軸、z 軸 或者自定義軸進行旋轉(zhuǎn)
語法
- transform: rotateX(45deg) – 沿著 x 軸正方向旋轉(zhuǎn) 45 度
- transform: rotateY(45deg) – 沿著 y 軸正方向旋轉(zhuǎn) 45 度
- transform: rotateZ(45deg) – 沿著 z 軸正方向旋轉(zhuǎn) 45 度
- transform: rotate3d(x, y, z, 45deg) – 沿著自定義軸旋轉(zhuǎn) 45 deg 為角度
代碼案例
div {perspective: 300px
;
}img {display: block
;margin: 100px auto
;transition: all 1s
;
}img:hover {transform: rotateX(-45deg
)
}
左手準則
?
六、3D 旋轉(zhuǎn) rotateY
代碼演示
div {perspective: 500px
;
}img {display: block
;margin: 100px auto
;transition: all 1s
;
}img:hover {transform: rotateY(180deg
)
}
左手準則
七、 3D 旋轉(zhuǎn) rotateZ
代碼演示
div {perspective: 500px
;
}img {display: block
;margin: 100px auto
;transition: all 1s
;
}img:hover {transform: rotateZ(180deg
)
}
rotate3d
- transform: rotate3d(x, y, z, deg) – 沿著自定義軸旋轉(zhuǎn) deg 為角度
- x, y, z 表示旋轉(zhuǎn)軸的矢量,是標識你是否希望沿著該軸進行旋轉(zhuǎn),最后一個標識旋轉(zhuǎn)的角度
- transform: rotate3d(1, 1, 0, 180deg) – 沿著對角線旋轉(zhuǎn) 45deg
- transform: rotate3d(1, 0, 0, 180deg) – 沿著 x 軸旋轉(zhuǎn) 45deg
代碼演示
div {perspective: 500px;
}img {display: block;margin: 100px auto;transition: all 1s;
}img:hover {transform: rotate3d(1, 1, 0, 180deg)
}
八、3D 呈現(xiàn) transform-style
transform-style 代碼演示
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document
</title><style>body {perspective: 500px;}.box {position: relative;width: 200px;height: 200px;margin: 100px auto;transition: all 2s;transform-style: preserve-3d;}.box:hover {transform: rotateY(60deg);}.box div {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-color: pink;}.box div:last-child {background-color: purple;transform: rotateX(60deg);}</style>
</head><body><div class="box"><div></div><div></div></div>
</body></html>
案例一:兩面翻轉(zhuǎn)的盒子
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>2面翻轉(zhuǎn)的盒子
</title><style type="text/css">body {perspective: 450px;}.box {position: relative;width: 200px;height: 200px; border: 1px solid #ccc;margin: 200px auto;transition: all 2s;transform-style: preserve-3d;}.box:hover {transform: rotateY(180deg);}.front,.back {position: absolute;width: 100%;height: 100%;border-radius: 50%;top: 0;left: 0; color: #fff;font-size: 30px;text-align: center;line-height: 200px;}.front {background-color: pink;z-index: 1;}.back {background-color: mediumpurple;transform: rotateY(180deg);}</style></head><body><div class="box"><div class="front">前端
</div><div class="back">后端
</div></div></body>
</html>
案例二:3D導(dǎo)航欄
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>3D導(dǎo)航欄
</title><style type="text/css">ul {height: 50px;}ul li {float: left;list-style: none;margin: 0 10px;perspective: 400px;}.box {position: relative;width: 200px;height: 50px;margin: 200px auto;transform-style: preserve-3d;transition: all 0.6s;}.box:hover {transform: rotateX(90deg);}.box .front,.box .bottom {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-color: pink;text-align: center;line-height: 50px;}.box .front {z-index: 1;transform: translateZ(25px);}.box .bottom {background-color: paleturquoise;transform: translateY(50%) rotateX(-90deg);}</style></head><body><ul><li><div class="box"><div class="front">前端
</div><div class="bottom">后端
</div></div></li><li><div class="box"><div class="front">前端
</div><div class="bottom">后端
</div></div></li><li><div class="box"><div class="front">前端
</div><div class="bottom">后端
</div></div></li><li><div class="box"><div class="front">前端
</div><div class="bottom">后端
</div></div></li><li><div class="box"><div class="front">前端
</div><div class="bottom">后端
</div></div></li></ul></body>
</html>
案例三:旋轉(zhuǎn)木馬效果
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>旋轉(zhuǎn)木馬
</title><style type="text/css">body {perspective: 1000px;}@keyframes rotates{0% {transform: rotateY(0);}100% {transform: rotateY(360deg);}}section {position: relative;width: 300px;height: 200px;margin: 150px auto; transform-style: preserve-3d;animation: rotates 10s linear infinite; background: url(img/pig.jpg) no-repeat;}section:hover {animation-play-state: paused;}section div {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background: url(./img/dog.jpg) no-repeat; }div:first-child {transform: rotateY(0deg) translateZ(300px);}div:nth-child(2) {transform: rotateY(60deg) translateZ(300px);}div:nth-child(3) {transform: rotateY(120deg) translateZ(300px);}div:nth-child(4) {transform: rotateY(180deg) translateZ(300px);}div:nth-child(5) {transform: rotateY(240deg) translateZ(300px);}div:nth-child(6) {transform: rotateY(300deg) translateZ(300px);}</style></head><body><section><div></div><div></div><div></div><div></div><div></div><div></div></section></body>
</html>
瀏覽器的私有前綴:
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的十五、CSS 3新特性详解(三)——3D转换(位移、旋转、呈现)、透视perspective、旋转rotateX、Y、Z、呈现transform-style的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。