-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path冒泡排序.html
78 lines (78 loc) · 2.16 KB
/
冒泡排序.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
*{
margin:0;
padding:0;
}
#warp{
position: relative;
width: 800px;
margin:300px auto;
}
#warp div{
position: absolute;
width: 60px;
background: red;
}
</style>
<body>
<div id="warp"></div>
<script>
var oBox = document.getElementById("warp");
var arr = [3, 1, 5, 7, 2, 4, 9, 6, 10, 8];
var dom = [];
arr.sort(function(){return Math.random() - .5});
function run(el, target, attr, fn) {
clearTimeout(el.timer)
var iStop = false;
(function recurs() {
el.timer = setTimeout(function() {
var cur = parseInt(el.style[attr])
,iSpeed = (target - cur) / 8
,iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed)
,iStop = cur === target
el.style[attr] = cur + iSpeed + 'px'
if(iStop) {
clearTimeout(el.timer)
fn && fn()
}else{
recurs()
}
}, 30);
})()
};
void function() {
for(var i=0; i<arr.length; i++) {
var divs = document.createElement('div')
divs.style.cssText = `height:${arr[i]*10}px;bottom:0;left:${i*70}px`
dom.push(divs)
oBox.appendChild(divs)
}
}();
(function recurs(m, n) {
if(arr[m] > arr[m+1]) {
var temp = arr[m]
arr[m] = arr[m+1]
arr[m+1] = temp
run(dom[m], 100, 'bottom', function() {
run(dom[m], (m+1)*70, 'left', function() {
run(dom[m], 0, 'bottom', function() {
dom.splice(m,1,...dom.splice(m+1,1,dom[m]))
recurs(m===arr.length-1?0:m+1, m===arr.length-1?n+1:n)
})
run(dom[m+1], m*70, 'left')
})
})
}else {
if(arr.join('') === '12345678910') return
recurs(m===arr.length-1?0:m+1, m===arr.length-1?n+1:n)
}
})(0, 0)
</script>
</body>
</html>