forked from Crise420WMT/crise420wmt.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_picWall.html
158 lines (147 loc) · 5.43 KB
/
list_picWall.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{margin: 0;padding: 0;list-style: none;}
#box{width: 642px;margin: 0 auto;position: relative;}
#box li{width: 200px;height:200px;margin: 0 10px 10px 0;border: 2px solid #000000;float: left;}
#box li.ac{border: 2px dotted orangered;}
#box li img{width: 200px;height: 200px;}
</style>
<script>
window.onload=function(){
var oBox=document.getElementById('box');
var aLi=oBox.children;
var oBtn=document.getElementById('btn');
var arr=[];
oBtn.onclick=function(){
arr.sort(function(){
return Math.random()-0.5;
});
for(var i=0;i<aLi.length;i++){
aLi[i].style.left=arr[i].left+'px';
aLi[i].style.top=arr[i].top+'px';
}
};
for(var i=0;i<aLi.length;i++){
arr.push({left:aLi[i].offsetLeft,top:aLi[i].offsetTop});
}
for(var i=0;i<aLi.length;i++){
aLi[i].style.position='absolute';
aLi[i].style.margin=0;
aLi[i].style.left=arr[i].left+'px';
aLi[i].style.top=arr[i].top+'px';
}
function Drag(obj){
obj.onmousedown=function(ev){
var oEvent=ev||event;
var disX=oEvent.clientX-obj.offsetLeft;
var disY=oEvent.clientY-obj.offsetTop;
document.onmousemove=function(ev){
var oEvent=ev||event;
var l=oEvent.clientX-disX;
var t=oEvent.clientY-disY;
for(var i=0;i<aLi.length;i++){
aLi[i].className='';
}
if(findMin(obj)){
//console.log(1);
var oNear=findMin(obj);
oNear.className='ac';
}
obj.style.left=l+'px';
obj.style.top=t+'px';
};
document.onmouseup=function(){
document.onmousemove=null;
document.onmouseup=null;
obj.releaseCapture&&obj.releaseCapture();
if(findMin(obj)){
var oNear=findMin(obj);
obj.style.left=arr[oNear.index].left+'px';
obj.style.top=arr[oNear.index].top+'px';
oNear.style.top=arr[obj.index].top+'px';
oNear.style.left=arr[obj.index].left+'px';
oNear.className='';
var car=obj.index;
obj.index=oNear.index;
oNear.index=car;
}else{
obj.style.left=arr[obj.index].left+'px';
obj.style.top=arr[obj.index].top+'px';
}
};
obj.setCapture&&obj.setCapture();
return false;
};
}
function findMin(obj){
var iMin=999999;
var iMinIndex=-1;
for(var i=0;i<aLi.length;i++){
if(obj==aLi[i]){
continue;
}else{
obj.style.zIndex=999;
aLi[i].style.zIndex=0;
}
if(Collision(obj,aLi[i])){
//console.log(1);
var c=getDis(obj,aLi[i]);
if(iMin>c){
iMin=c;
iMinIndex=i;
}
}
}
if(iMinIndex==-1){
return null;
}else{
return aLi[iMinIndex];
}
}
function getDis(obj,obj2){
var a=obj.offsetLeft-obj2.offsetLeft;
var b=obj.offsetTop-obj2.offsetTop;
return Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
}
function Collision(obj,obj2){
var l=obj.offsetLeft;
var r=obj.offsetLeft+obj.offsetWidth;
var t=obj.offsetTop;
var b=obj.offsetTop+obj.offsetHeight;
var l2=obj2.offsetLeft;
var r2=obj2.offsetLeft+obj2.offsetWidth;
var t2=obj2.offsetTop;
var b2=obj2.offsetTop+obj2.offsetHeight;
if(r>l2&&l<r2&&b>t2&&t<b2){
//console.log(1);
return true;
}else{
return false;
}
}
for(var i=0;i<aLi.length;i++){
Drag(aLi[i]);
aLi[i].index=i;
}
};
</script>
</head>
<body>
<input id="btn" type="button" value="随机换"/>
<div id="box">
<li><img src="image/taobao_img1.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img2.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img3.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img4.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img5.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img6.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img7.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img8.jpg" width="200px" height="200px" alt=""/></li>
<li><img src="image/taobao_img9.jpg" width="200px" height="200px" alt=""/></li>
</div>
</body>
</html>