Web前端实现3D旋转魔方相册【超详细】


前言

例如:3D旋转魔方相册,表白神器!

3D旋转魔方效果图


提示:以下是本篇文章正文内容,下面案例可供参考

一、html代码

<!DOCTYPE html>
<html>
	<head lang="en">
		<meta charset="UTF-8">
		<link type="text/css" href="YoungerSister.css" rel="stylesheet ">
		<title>YoungerSister</title>
		<style>
			<pre name="code"class="css">* {
				padding: 0;
				margin: 0;
			}

			body {
				width: 100%;
				height: 100%;
				background: #333;
			}

			#react {
				width: 400px;
				height: 400px;
				margin: 400px auto;
				transform-style: preserve-3d;
				animation: rotate 20s infinite;
				animation-timing-function: linear;
			}

			#react div {
				position: absolute;
				transition: all .4s;
			}

			div .out_pic {
				width: 400px;
				height: 400px;
				opacity: 0.9;
			}

			div .in_pic {
				width: 200px;
				height: 200px;
			}

			#react span {
				display: block;
				position: absolute;
				width: 200px;
				height: 200px;
				top: 50px;
				left: 50px;
			}

			@keyframes rotate {
				from {
					transform: rotateX(0deg) rotateY(0deg);
				}

				to {
					transform: rotateX(360deg) rotateY(360deg);
				}
			}


			.out_front {
				transform: translateZ(200px);
			}

			.out_back {
				transform: translateZ(-200px);
			}

			.out_left {
				transform: rotateY(90deg) translateZ(200px);
			}

			.out_right {
				transform: rotateY(-90deg) translateZ(200px);
			}

			.out_top {
				transform: rotateX(90deg) translateZ(200px);
			}

			.out_bottom {
				transform: rotateX(-90deg) translateZ(200px);
			}

			.in_front {
				transform: translateZ(50px);
			}

			.in_back {
				transform: translateZ(-50px);
			}

			.in_left {
				transform: rotateY(90deg) translateZ(50px);
			}

			.in_right {
				transform: rotateY(-90deg) translateZ(50px);
			}

			.in_top {
				transform: rotateX(90deg) translateZ(50px);
			}

			.in_bottom {
				transform: rotateX(-90deg) translateZ(50px);
			}

			/*外面的图片散开*/
			#react:hover .out_front {
				transform: translateZ(400px);
			}

			#react:hover .out_back {
				transform: translateZ(-400px);
			}

			#react:hover .out_left {
				transform: rotateY(90deg) translateZ(400px);
			}

			#react:hover .out_right {
				transform: rotateY(-90deg) translateZ(400px);
			}

			#react:hover .out_top {
				transform: rotateX(90deg) translateZ(400px);
			}

			#react:hover .out_bottom {
				transform: rotateX(-90deg) translateZ(400px);
			}


			/*里面的图片散开*/
			#react:hover .in_front {
				transform: translateZ(200px);
			}

			#react:hover .in_back {
				transform: translateZ(-200px);
			}

			#react:hover .in_left {
				transform: rotateY(90deg) translateZ(200px);
			}

			#react:hover .in_right {
				transform: rotateY(-90deg) translateZ(200px);
			}

			#react:hover .in_top {
				transform: rotateX(90deg) translateZ(200px);
			}

			#react:hover .in_bottom {
				transform: rotateX(-90deg) translateZ(200px);
			}
		</style>
	</head>
	<body>
		<div id="react">
			<div class="out_front">
				<img src="001.jpg" class="out_pic">
			</div>
			<div class="out_back">
				<img src="002.jpg" class="out_pic">
			</div>
			<div class="out_left">
				<img src="003.jpg" class="out_pic">
			</div>
			<div class="out_right">
				<img src="004.jpg" class="out_pic">
			</div>
			<div class="out_top">
				<img src="005.jpg" class="out_pic">
			</div>
			<div class="out_bottom">
				<img src="006.jpg" class="out_pic">
			</div>
			<span class="in_front">
				<img src="007.jpg" class="in_pic">
			</span>
			<span class="in_back">
				<img src="008.jpg" class="in_pic">
			</span>
			<span class="in_left">
				<img src="009.jpg" class="in_pic">
			</span>
			<span class="in_right">
				<img src="010.jpg" class="in_pic">
			</span>
			<span class="in_top">
				<img src="011.jpg" class="in_pic">
			</span>
			<span class="in_bottom">
				<img src="012.jpg" class="in_pic">
			</span>
		</div>
	</body>
</html>

二、css代码

*{
    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;
    background-color: #57faff;
 
}
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-right: 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:rotateX(180deg) 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.8;
    left: -100px;
    top:-100px;
}
.box:hover ol li:nth-child(6){
    -webkit-transform:rotateY(90deg) translateZ(300px);
    width: 400px;
    height: 400px;
    opacity: 0.8;
    left: -100px;
    top:-100px;
}
@keyframes move{
    0%{
        -webkit-transform:rotateX(13deg) rotateY(0deg);
    }
    100%{
        -webkit-transform:rotateX(13deg) rotateY(360deg);
    }
}
 

三、放上照片

在这里插入图片描述
提示:照片和html、css文件放在一个文件夹。

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
实现旋转魔方相册,可以考虑以下步骤: 1. 创建一个容器,用于放置魔方相册。 2. 将魔方相册拆分成 6 个面。每个面都是一个独立的图片或者容器。 3. 使用 CSS3 3D 变换(transform3d)和过渡(transition)属性,将每个面沿着魔方的边缘进行旋转。 4. 使用 JavaScript 监听用户的交互事件,例如鼠标点击、拖动等,根据用户的操作改变魔方相册的状态,从而实现旋转和翻转等效果。 下面是一个简单示例代码,实现了一个基本的旋转魔方相册: HTML 代码: ```html <div class="cube"> <div class="cube__item cube__item--front"></div> <div class="cube__item cube__item--back"></div> <div class="cube__item cube__item--right"></div> <div class="cube__item cube__item--left"></div> <div class="cube__item cube__item--top"></div> <div class="cube__item cube__item--bottom"></div> </div> ``` CSS 代码: ```css .cube { position: relative; width: 200px; height: 200px; transform-style: preserve-3d; transition: transform 1s; } .cube__item { position: absolute; width: 200px; height: 200px; background-size: cover; border: 1px solid white; } .cube__item--front { transform: translateZ(100px); background-image: url(front.jpg); } .cube__item--back { transform: translateZ(-100px) rotateY(180deg); background-image: url(back.jpg); } .cube__item--right { transform: rotateY(-90deg) translateZ(100px); background-image: url(right.jpg); } .cube__item--left { transform: rotateY(90deg) translateZ(100px); background-image: url(left.jpg); } .cube__item--top { transform: rotateX(90deg) translateZ(100px); background-image: url(top.jpg); } .cube__item--bottom { transform: rotateX(-90deg) translateZ(100px); background-image: url(bottom.jpg); } .cube:hover { transform: rotateX(360deg) rotateY(360deg); } ``` JavaScript 代码: ```javascript const cube = document.querySelector('.cube'); let isDragging = false; let startX, startY, currentX, currentY; cube.addEventListener('mousedown', (e) => { isDragging = true; startX = e.pageX; startY = e.pageY; }); cube.addEventListener('mousemove', (e) => { if (!isDragging) return; currentX = e.pageX; currentY = e.pageY; const dx = currentX - startX; const dy = currentY - startY; cube.style.transform = `rotateX(${dy * -0.2}deg) rotateY(${dx * 0.2}deg)`; }); cube.addEventListener('mouseup', () => { isDragging = false; }); ``` 这段 JavaScript 代码实现了鼠标拖动旋转魔方相册的功能。当用户按下鼠标左键时,记录下当前的坐标。当用户移动鼠标时,计算出鼠标移动的距离,根据距离改变魔方相册旋转角度。当用户松开鼠标时,停止旋转

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啸啸说

你的鼓励将是我创作的最大力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值