css樣式:
*{margin: 0; padding: 0}
#box{
width:700px;
height:400px;
background:#ccc;
position:relative;
margin:100px auto;
}
#box span{
width:100px;
height:100px;
position:absolute;
transition: 1s all ease;
transform-style: preserve-3d;
transform: perspective(800px) rotate(0deg);
}
#box span .front{
width:100%;
height:100%;
position:absolute;
top:0;
left:0;
transform: translateZ(1px);
background:url("img/0.jpg") no-repeat;
}
#box span .back{
width:100%;
height:100%;
position:absolute;
top:0;
left:0;
transform:scale(-1,1) translateZ(-1px);
background:url("img/1.jpg") no-repeat;
}
布局給個(gè)div就行:
<div id="box">
<!--<span>
<em class="front"></em>
<em class="back"></em>
</span>
<span></span>-->
</div>
js代碼
document.addEventListener('DOMContentLoaded',function(){
var oBox=document.getElementById('box');
var R=4;
var C=7;
for(var r=0; r<R; r++){
for(var c=0; c<C; c++){
var oSpan=document.createElement('span');
var w=oBox.offsetWidth/C;
var h=oBox.offsetHeight/R;
oSpan.style.width=w+'px';
oSpan.style.height=h+'px';
oSpan.style.left=w*c+'px';
oSpan.style.top=h*r+'px';
oSpan.innerHTML='<em class="front"></em><em class="back"></em>';
oBox.appendChild(oSpan);
//添加兩個(gè)自定義屬性奋早,用于添加延遲效果
oSpan.c=c;
oSpan.r=r;
oSpan.children[0].style.backgroundPosition=(-oSpan.offsetLeft)+'px '+(-oSpan.offsetTop)+'px';
oSpan.children[1].style.backgroundPosition=(-oSpan.offsetLeft)+'px '+(-oSpan.offsetTop)+'px';
}
}
var iNow=0;
var aSpan=oBox.children;
var bReady=true;
//點(diǎn)擊事件
document.addEventListener('click',function(){
//加個(gè)開(kāi)關(guān),以防點(diǎn)太快跳圖
if(bReady==false){
return;
}
bReady=false;
iNow++;
for(var i=0;i<aSpan.length;i++){
//需要一個(gè)延遲才能有從左上到右下的翻頁(yè)效果
aSpan[i].style.transition='1s all ease '+200*(aSpan[i].r+aSpan[i].c)+'ms';
aSpan[i].style.transform='perspective(800px) rotateY(180deg)'; }
},false);
//運(yùn)動(dòng)結(jié)束觸發(fā)事件
aSpan[aSpan.length-1].addEventListener('transitionend',function(){
//瞬間拉回 換圖
for(var i=0;i<aSpan.length;i++){
aSpan[i].style.transition='none';
aSpan[i].style.transform='perspective(800px) rotateY(0deg)'; aSpan[i].children[0].style.backgroundImage='url("img/'+iNow%3+'.jpg")';
aSpan[i].children[1].style.backgroundImage='url("img/'+((iNow+1)%3)+'.jpg")';
}
bReady=true;
},false);
},false);
效果圖:
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者