3D旋转相册代码及详细使用教程

相信很多小伙伴看着短视频里的3D旋转相册很想要吧,下面就让我们来看看它的代码及使用方法!

  • 效果展示
    在这里插入图片描述

  • 代码展示
    1.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>3D立体动态相册</title>
		<link rel="stylesheet" type="text/css" href="css/style.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>
			<!--内层盒子-->
			<ol>
				<li>
					<img src="img/01.jpg" >
				</li>
				<li>
					<img src="img/02.jpg" >
				</li>
				<li>
					<img src="img/03.jpg" >
				</li>
				<li>
					<img src="img/04.jpg" >
				</li>
				<li>
					<img src="img/05.jpg" >
				</li>
				<li>
					<img src="img/06.jpg" >
				</li>
			</ol>
		</div>
	</body>
</html>

2.css

/*默认样式*/
*{
	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;
	/* 这是旋转基点 */
	transform-origin: -550px 550px 100;
	/* 设置景深 */
	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;
	/* 设置过渡时间 */
	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;} 

/* 内层盒子*/
#cube ol{
	width: 100px;
	height: 100px;
	position: absolute;
	left: 50px;
	top: 50px;
	/* 给内层盒子设置3d空间 */
	transform-style: preserve-3d;
	/* 设置动画 */
	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;}

/*外层盒子鼠标移入效果*/
#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;}
/*旋转动画效果*/
@keyframes rotate1{
	0%{transform: rotateY(0);}
	100%{transform: rotateY(360deg);}
}

  • 使用方法
    1.在桌面(或磁盘内)新建一个文件夹。在这里插入图片描述
    2.在3D相册文件夹内再新建两个文件夹和一个文本文档,分别取名为css,img,index。
    在这里插入图片描述
    3.将html下的代码复制粘贴到index中,再将index后缀名改为html。
    在这里插入图片描述
    4.在css文件夹内新建一个文本文档,取名为style,将css下的代码复制粘贴到style中,再将style后缀名改为css。


    5.将12张jpg类型的照片放入img文件夹中,分别命名为01-06,1-6。(大小:只要照片宽等于高即可,推荐:01-06:100×100,1-6:200×200,序号为6的照片上下颠倒一下效果更好哦
    6.最后双击index即可。

注:IE浏览器无法呈现效果,推荐使用其他浏览器。3、4两步改的是后缀名,请检查新建文本文档是否以.txt结尾,如果不是则后缀名隐藏了。改为显示后按上述步骤即可。

因为有许多博友按步骤操作后仍有错误,所以我把资源压缩上传到百度网盘了,需要的请点击这里链接: 浪漫相册
提取码:gclr
各位老铁,点个赞再走呗,万分感谢。

  • 158
    点赞
  • 414
    收藏
    觉得还不错? 一键收藏
  • 131
    评论
CSS实现鼠标经过3D立体动态展示图片特效代码 @charset "utf-8"; *{ margin:0; padding:0; } body{ max-width: 100%; min-width: 100%; height: 100%; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-size:100% 100%; position: absolute; margin-left: auto; margin-right: auto; } li{ list-style: none; } .box{ width:200px; height:200px; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-size:100% 100%; position: absolute; margin-left: 42%; margin-top: 22%; -webkit-transform-style:preserve-3d; -webkit-transform:rotateX(13deg); -webkit-animation:move 5s linear infinite; } .minbox{ width:100px; height:100px; position: absolute; left:50px; top:30px; -webkit-transform-style:preserve-3d; } .minbox li{ width:100px; height:100px; position: absolute; left:0; top:0; } .minbox li:nth-child(1){ background: url(img/01.png) no-repeat 0 0; -webkit-transform:translateZ(50px); } .minbox li:nth-child(2){ background: url(img/02.png) no-repeat 0 0; -webkit-transform:rotateX(180deg) translateZ(50px); } .minbox li:nth-child(3){ background: url(img/03.png) no-repeat 0 0; -webkit-transform:rotateX(-90deg) translateZ(50px); } .minbox li:nth-child(4){ background: url(img/04.png) no-repeat 0 0; -webkit-transform:rotateX(90deg) translateZ(50px); } .minbox li:nth-child(5){ background: url(img/05.png) no-repeat 0 0; -webkit-transform:rotateY(-90deg) translateZ(50px); } .minbox li:nth-child(6){ background: url(img/06.png) no-repeat 0 0; -webkit-transform:rotateY(90deg) translateZ(50px); } .maxbox li:nth-child(1){ background: url(img/1.png) no-repeat 0 0; -webkit-transform:translateZ(50px); } .maxbox li:nth-child(2){ background: url(img/2.png) no-repeat 0 0; -webkit-transform:translateZ(50px); } .maxbox li:nth-child(3){ background: url(img/3.png) no-repeat 0 0; -webkit-transform:rotateX(-90deg) translateZ(50px); } .maxbox li:nth-child(4){ background: url(img/4.png) no-repeat 0 0; -webkit-transform:rotateX(90deg) translateZ(50px); } .maxbox li:nth-child(5){ background: url(img/5.png) no-repeat 0 0; -webkit-transform:rotateY(-90deg) translateZ(50px); } .maxbox li:nth-child(6){ background: url(img/6.png) no-repeat 0 0; -webkit-transform:rotateY(90deg) translateZ(50px); } .maxbox{ width: 800px; height: 400px; position: absolute; left: 0; top: -20px; -webkit-transform-style: preserve-3d; } .maxbox li{ width: 200px; height: 200px; background: #fff; border:1px solid #ccc; position: absolute; left: 0; top: 0; opacity: 0.2; -webkit-transition:all 1s ease; } .maxbox li:nth-child(1){ -webkit-transform:translateZ(100px); } .maxbox li:nth-child(2){ -webkit-transform:rotateX(180deg) translateZ(100px); } .maxbox li:nth-child(3){ -webkit-transform:rotateX(-90deg) translateZ(100px); } .maxbox li:nth-child(4){ -webkit-transform:rotateX(90deg) translateZ(100px); } .maxbox li:nth-child(5){ -webkit-transform:rotateY(-90deg) translateZ(100px); } .maxbox li:nth-child(6){ -webkit-transform:rotateY(90deg) translateZ(100px); } .box:hover ol li:nth-child(1){ -webkit-transform:translateZ(300px); width: 400px; height: 400px; opacity: 0.8; left: -100px; top: -100px; } .box:hover ol li:nth-child(2){ -webkit-transform:rotateX(180deg) translateZ(300px); width: 400px; height: 400px; opacity: 0.8; left: -100px; top: -100px; } .box:hover ol li:nth-child(3){ -webkit-transform:rotateX(-90deg) translateZ(300px); width: 400px; height: 400px; opacity: 0.8; left: -100px; top: -100px; } .box:hover ol li:nth-child(4){ -webkit-transform:rotateX(90deg) translateZ(300px); width: 400px; height: 400px; opacity: 0.8; left: -100px; top: -100px; } .box:hover ol li:nth-child(5){ -webkit-transform:rotateY(-90deg) translateZ(300px); width: 400px; height: 400px; opacity: 0
你可以在以下代码中找到一个基本的3D旋转相册HTML实现: ```html <!DOCTYPE html> <html> <head> <style> .container { perspective: 1000px; width: 300px; height: 300px; } .carousel { transform-style: preserve-3d; animation: rotation 10s infinite linear; width: 100%; height: 100%; } .carousel:hover { animation-play-state: paused; } .item { position: absolute; width: 200px; height: 200px; background-color: #ddd; line-height: 200px; font-size: 48px; text-align: center; } .item:nth-child(1) { transform: rotateY(0deg) translateZ(300px); } .item:nth-child(2) { transform: rotateY(60deg) translateZ(300px); } .item:nth-child(3) { transform: rotateY(120deg) translateZ(300px); } .item:nth-child(4) { transform: rotateY(180deg) translateZ(300px); } .item:nth-child(5) { transform: rotateY(240deg) translateZ(300px); } .item:nth-child(6) { transform: rotateY(300deg) translateZ(300px); } @keyframes rotation { from { transform: rotateY(0deg); } to { transform: rotateY(360deg); } } </style> </head> <body> <div class="container"> <div class="carousel"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> </div> </div> </body> </html> ``` 这段代码会创建一个带有6个项目的旋转相册,每个项目以一个方块表示,通过CSS的`transform`属性来实现旋转效果。你可以根据需要修改CSS样式和相册中的项目内容来适应你的需求。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 131
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值