今天,我給大家分享一個3D相冊的代碼
廢話不說先上效果圖:
先新建兩個文件夾,一個叫css,另一個叫img,如下圖:
先新建一個文本文檔,輸入下面的代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>3D立體動態(tài)相冊</title>
<link rel="stylesheet" type="text/css" href="css/css.css"/>
</head>
<body>
<div id="cube">
<!--外層盒子-->
<ul>
<li>
<img src="img/1.jpg" >
</li>
<li>
<img src="img/2.jpg" >
</li>
<li>
<img src="img/3.jpg" >
</li>
<li>
<img src="img/4.jpg" >
</li>
<li>
<img src="img/5.jpg" >
</li>
<li>
<img src="img/6.jpg" >
</li>
</ul>
<!--內(nèi)層盒子-->
<ol>
<li>
<img src="img/7.jpg" >
</li>
<li>
<img src="img/8.jpg" >
</li>
<li>
<img src="img/9.jpg" >
</li>
<li>
<img src="img/10.jpg" >
</li>
<li>
<img src="img/11.jpg" >
</li>
<li>
<img src="img/12.jpg" >
</li>
</ol>
</div>
</body>
</html>
然后保存關(guān)閉,將后綴名更改為HTML
再打開css文件夾,新建一個文本文檔,輸入以下代碼:
/*默認(rèn)樣式*/
*{
margin: 0;
padding: 0;
}
ol,ul{list-style: none;}
/*外層盒子*/
#cube{
height: 200px;
width: 200px;
/* 將整個盒子至于中間 */
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
/* 這是旋轉(zhuǎn)基點 */
transform-origin: -550px 550px 100;
/* 設(shè)置景深 */
perspective:800px;
}
#cube ul{
width: 200px;
height: 200px;
transform-style: preserve-3d;
animation: rotate1 5s linear infinite;
}
#cube ul img{
width: 200px;
height: 200px;
}
#cube ul li{
width: 200px;
height: 200px;
position: absolute;
/* 設(shè)置過渡時間 */
transition: 2s;
}
#cube ul li:nth-child(1){left: 0;top: 0;opacity: .4;transform: translateZ(100px);}
#cube ul li:nth-child(2){left: 100px;top: 0;transform: rotateY(90deg);opacity: .4;}
#cube ul li:nth-child(3){left: -100px;top: 0;transform: rotateY(-90deg);opacity: .4;}
#cube ul li:nth-child(4){left: 0;top: 100px;transform: rotateX(-90deg);opacity:.4;}
#cube ul li:nth-child(5){left: 0;top: -100px;transform: rotateX(90deg);opacity: .4;}
#cube ul li:nth-child(6){left: 0;top: 0;transform: translateZ(-100px) rotateY(180deg);opacity: .4;}
/* 內(nèi)層盒子*/
#cube ol{
width: 100px;
height: 100px;
position: absolute;
left: 50px;
top: 50px;
/* 給內(nèi)層盒子設(shè)置3d空間 */
transform-style: preserve-3d;
/* 設(shè)置動畫 */
animation: rotate1 5s linear infinite;
}
#cube ol img{
width: 100px;
height: 100px;
}
#cube ol li{
width: 100px;
height: 100px;
position: absolute;
}
#cube ol li:nth-child(1){left: 0;top: 0;opacity: .7;transform: translateZ(50px);}
#cube ol li:nth-child(2){left: 100px;top: 0px;transform-origin: left;transform:translateZ(50px) rotateY(90deg);opacity: .7;}
#cube ol li:nth-child(3){left: -100px;top: 0px;transform-origin: right;transform:translateZ(50px) rotateY(-90deg);opacity: .7;}
#cube ol li:nth-child(4){left:0px;top: 100px;transform-origin: top;transform:translateZ(50px) rotateX(-90deg);opacity: .7;}
#cube ol li:nth-child(5){left:0px;top: -100px;transform-origin: bottom;transform:translateZ(50px) rotateX(90deg);opacity: .7;}
#cube ol li:nth-child(6){left:0px;top: 0px;transform: translateZ(-50px) rotateY(180deg);opacity: .7;}
/*外層盒子鼠標(biāo)移入效果*/
#cube:hover ul li:nth-child(1){transform: translateZ(200px) scale(1.5);opacity: .6;}
#cube:hover ul li:nth-child(2){transform:rotateY(90deg) translateZ(100px) scale(1.5);opacity: .6;}
#cube:hover ul li:nth-child(3){transform:rotateY(-90deg) translateZ(100px) scale(1.5);opacity: .6;}
#cube:hover ul li:nth-child(4){transform:rotateX(-90deg) translateZ(100px) scale(1.5);opacity: .6;}
#cube:hover ul li:nth-child(5){transform:rotateX(90deg) translateZ(100px) scale(1.5);opacity: .6;}
#cube:hover ul li:nth-child(6){transform:rotateY(180deg) translateZ(200px) scale(1.5);opacity: .6;}
/*旋轉(zhuǎn)動畫效果*/
@keyframes rotate1{
0%{transform: rotateY(0);}
100%{transform: rotateY(360deg);}
}
然后保存關(guān)閉,將后綴名更改為css
再打開img文件夾,將圖片發(fā)在里面,重命名(名字必須與下圖一樣,否則無法顯示圖片),如下圖:
最后運行那個后綴名為HTML的文件就可以了文章來源:http://www.zghlxwxcb.cn/news/detail-507282.html
我會不定期更新,喜歡的話就關(guān)注文章來源地址http://www.zghlxwxcb.cn/news/detail-507282.html
到了這里,關(guān)于HTML實現(xiàn)3D相冊的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!