原生js实现3D图片小案例

<!DOCTYPE html>
  <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"  content="width=device-width,initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
body{
background: pink;
}
.wrapper .content{
width: 500px;
height: 250px;
background: lightgray;
margin: 70px auto;
}
.wrapper .btn{
margin-left: 610px;
margin-top: 10px;
width: 100px;
height: 50px;
font-size: 40px;
border-radius: 2px;
background: lightblue;
color: #000;
cursor: pointer;
border: none;
box-shadow: 2px 2px 2px 2px gray;
}
img{
width: 50px;
height: 50px;
float: left;
}
  </style> 
<title>3D实现图片</title>
</head>
<body>
<div class="wrapper">
	<div class="content">
	</div>
	<button class="btn">click</button>
</div>
<script > 
function init(){
addImg();
}
init();
var flag = true;
 var len,img;
function addImg(){
for (var i = 0; i < 50; i++) {
	var imgBox = document.getElementsByClassName('content')[0];
	var num = Math.floor(Math.random()*10);
	var src = "./lijian/" + num + ".jpg";
	var dom = document.createElement('img');
	dom.setAttribute('src',src);
	imgBox.appendChild(dom);
}
bindEvent();
}
function bindEvent(){
img = document.getElementsByTagName('img');
 len = img.length;
 var btn = document.getElementsByClassName('btn')[0];
btn.addEventListener('click',function(){
	if (!flag) {
		return;
	}
	var endNum = 0;
	flag = false;
	for (var i = 0; i < len; i++) {
		(function(i){
			setTimeout(function(){
				monition(img[i],'1s',function(){
					this.style.transform = 'scale(0)';
				},function(){
					monition(this,'1s',function(){
						this.style.transform = 'scale(1)';
						this.style.opacity = 0;
					},function(){
						endNum ++;
						if (endNum == len) {
							show();
						}
					})
				})
			},Math.random()*1000);
		})(i);
	}
})
}
function show(){
var allEnd = 0;
for (var i = 0; i < len; i++) {
	img[i].style.transition = '';
	//先将图片向后移动一段距离作为旋转半径
	img[i].style.transform = 'rotateY(0deg) translateZ(-' + Math.random()*500 +'px)';
	(function(i){
		setTimeout(function(){
			monition(img[i],'2s',function(){
				this.style.opacity = 1;
				this.style.transform = 'rotateY(-360deg) translateZ(0)';
			},function(){
				allEnd ++;
				if (allEnd == len) {
					flag = true;
				}
			})
		},Math.random()*1000);
	})(i)
}
}
function monition(dom,time,doFun,cb){
dom.style.transition = time;
doFun.call(dom);
var called = true; 
dom.addEventListener('transitionend',function(){
	if (called) {
		cb&&cb.call(dom);
		called = false;
 	}
});
}
   </script>
 </body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值