3D旋轉(zhuǎn)特效(上)
3D旋轉(zhuǎn)特效的實(shí)現(xiàn)也是很簡單的,開發(fā)者可以有許多種方式來實(shí)現(xiàn)它,高級(jí)瀏覽器支持實(shí)現(xiàn)3D空間的能力,所以這里直接采用了css3來實(shí)現(xiàn)。3D旋轉(zhuǎn)特效的所有代碼全部用JS封裝好了,參考右邊代碼區(qū)域。
實(shí)現(xiàn)原理:
先看結(jié)構(gòu),因?yàn)槭?D視角所以在結(jié)構(gòu)上需有一個(gè)透視參考點(diǎn),也就是舞臺(tái)節(jié)點(diǎn)上設(shè)置perspective,然后在容器父節(jié)點(diǎn)上設(shè)置preserve-3d屬性,下面就是最基本的結(jié)構(gòu)
舞臺(tái)
容器
圖片
圖片
圖片
節(jié)點(diǎn)#carousel中有3個(gè)面,其中有2個(gè)面是有側(cè)旋轉(zhuǎn)的立體效果,這里可以通過rotateY的設(shè)置達(dá)到這個(gè)效果,旋轉(zhuǎn)的面是平均分布的,那么每個(gè)面需要在上一個(gè)面的基礎(chǔ)上多旋轉(zhuǎn)120度(360 / 3)
transform:rotateY(0deg)
transform:rotateY(120deg)
transform:rotateY(240deg)
給3個(gè)面設(shè)置不同rotateY值的實(shí)際效果,通過增加與刪除preserve-3d設(shè)置后的效果對(duì)比,比較直觀
去掉preserve-3d后,如下圖
增加preserve-3d后,如下圖

其實(shí)上圖的基本布局效果已經(jīng)出來了,但是因?yàn)樵囟夹D(zhuǎn)是以自身的中心點(diǎn)為參考點(diǎn),默認(rèn)下中心點(diǎn)都是在正中間,所以感覺都是在一個(gè)范圍區(qū)域變化
下一節(jié)將要學(xué)習(xí)如何拉開3d距離
任務(wù)
請(qǐng)?jiān)趇ndex.html文件代碼16行出填出任務(wù)代碼
設(shè)置舞臺(tái)元素的透視點(diǎn)為500
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>圣誕主題</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div style=" position: absolute; left: 6.8rem; top: 3.5rem;">
<h6>去掉preserve-3d后,如下圖</h6>
<figure style="width: 4rem; transition: 1s;">
<figure style="width:100%;transform:rotateY(0deg) ;position:absolute;"><img src="http://img1.sycdn.imooc.com//5662e29a0001905a14410901.png" style="width:100%;height:100%;"></figure>
<figure style="width:100%;transform:rotateY(120deg) ;position:absolute;"><img src="http://img1.sycdn.imooc.com//5662e2960001f16314410901.png" style="width:100%;height:100%;"></figure>
<figure style="width:100%;transform:rotateY(240deg) ;position:absolute;"><img src="http://img1.sycdn.imooc.com//5662e26f00010dea14410901.png" style="width:100%;height:100%;"></figure>
</figure>
</div>
<div style=" position: absolute; left: 6.8rem; top: 15.5rem;">
<h6>增加preserve-3d后,如下圖</h6>
<figure style="width: 4rem; transform-style: preserve-3d; transition: 1s;">
<figure style="width:100%;transform:rotateY(0deg) ;position:absolute;"><img src="http://img1.sycdn.imooc.com//5662e29a0001905a14410901.png" style="width:100%;height:100%;"></figure>
<figure style="width:100%;transform:rotateY(120deg) ;position:absolute;"><img src="http://img1.sycdn.imooc.com//5662e2960001f16314410901.png" style="width:100%;height:100%;"></figure>
<figure style="width:100%;transform:rotateY(240deg) ;position:absolute;"><img src="http://img1.sycdn.imooc.com//5662e26f00010dea14410901.png" style="width:100%;height:100%;"></figure>
</figure>
</div>
<script type="text/javascript">
//rem設(shè)置
(function(doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function() {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
//寬與高度
document.body.style.height = clientWidth * (900 / 1440) + "px"
};
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
</script>
</body>
</html>
請(qǐng)驗(yàn)證,完成請(qǐng)求
由于請(qǐng)求次數(shù)過多,請(qǐng)先驗(yàn)證,完成再次請(qǐng)求