分割图片随机排序

突然来了兴致,写个切割图片随机排序。


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=0.5, maximum-scale=1.0, user-scalable=no">
	<title>Document</title>
	<script src="js/jquery-1.9.1.js"></script>
	<style type="text/css">
		body{padding:20px;}
		.picture{width:400px;height:350px;position:relative;margin:0 0 20px;}
		.picture:after{content:"";clear:both;display:block;}
		.picture img{width:100%;height:100%;position:absolute;}
		.picture_div{box-shadow:0px 0px 5px 1px yellow}
	</style>
</head>
<body>
    <div class="picture">
        <img src="images/img01.jpg">
    </div>
    <input type="button" value="重 置">
	<script>
		"use strict";
		// 声明变量 :  宽,高,下标,双坐标数组;
		var w,h,timeout,random;
		var coords=[];
		var newCoords=[];
		$(function(){
			// 重置;
			$("input").on({
				"click":()=>{
					coords=[];
					newCoords=[];
					clearTimeout(timeout);
					$("div",$(".picture")).each(function(){
						var that=$(this);
						$(this).stop(false,false).animate({
							"left":$(this).attr("data-left"),
							"top":$(this).attr("data-top")
						},1000,function(){
							$(".picture img").show();
							that.remove();
							$("input").hide();
						})
					})
				}
			});
			// 点击执行
			$("body").on({
				"click":function(){
					$("input").show();
					// 隐藏原图片
					$(".picture img").hide();
					$(".picture div").remove();
					// 获取区域大小
					w=$(this).outerWidth(false)/10;
					h=$(this).outerHeight(false)/10;
					// 循环输出100个子元素
					for(var i=0;i<100;i++){
						// 存储元素行列序号
						coords.push([i%10,Math.floor(i/10%10)]);

						// 输出子元素到容器内,并设置初始位置;
						 $(".picture").append(`<div class='picture_div' data-left='${i%10*w}px'  data-top='${Math.floor(i/10%10)*h}px' style='position:absolute;left:${i%10*w}px;top:${Math.floor(i/10%10)*h}px;width:${w}px;height:${h}px;background:url(${$(this).prop("src")}) no-repeat -${i%10*w}px ${-Math.floor(i/10%10)*h}px/${$(this).outerWidth(false)}px ${$(this).outerHeight(false)}px'></div>`);
					};
					newCoords=coords;
					random(1); //执行函数;
				}
			},".picture img");
		});

		// 核心代码
		random=(m)=>{
			// 根据参数判断并清空数组;
			m>0?newCoords=[]:coords=[];
			// 循环子元素
			$("div",$(".picture")).each(function(){
				// 第一种取值
				if(m>0){
					// 获取数组中的随机数据;
					var n=Math.floor(Math.random()*coords.length);
					$(this).stop(false,false).animate({
						// 将数据添加到子元素
						"left":coords[n][0]*w+"px",
						"top":coords[n][1]*h+"px"
					},1000)
					//存储已取的值到另一个数组;
					newCoords.push(coords[n]);
					// 删除已取的值
					coords.splice(n,1);
				//另一种情况
				}else{
					var n=Math.floor(Math.random()*newCoords.length);
					$(this).stop(false,false).animate({
						"left":newCoords[n][0]*w+"px",
						"top":newCoords[n][1]*h+"px"
					},1000)
					coords.push(newCoords[n]);
					newCoords.splice(n,1);
				}
			})
			// 再次执行并改变参数;
			clearTimeout(timeout);
			timeout =	setTimeout(function(){
				random(-m);
			},3000)
		};
	</script>
</body>
</html>




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值