JavaScript—冒泡排序动画
HTML文件
<!DOCTYPE html>
<html>
<head>
<title>排序</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles/index.css">
</head>
<body id="body">
<div id="stage"></div>
<script src="scripts/index.js"></script>
</body>
</html>
CSS文件
html{
height: 100%;
-o-user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
-khtml-user-select :none;
user-select: none;
}
#body{
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
#stage{
position: absolute;
width: 600px;
height: 400px;
background-color: #eeeeee;
border:10px solid #999999;
border-radius: 5px;
}
.element{
position: absolute;
background-color: #1cb69e;
width: 30px;
bottom: 30px;
border-radius: 10px 10px 2px 2px;
}
.number{
position: absolute;
color: #555555;
font-size: 18px;
height: 30px;
font-weight: bold;
}
.position{
position: absolute;
width: 0px;
height: 0px;
border:5px solid #eeeeee;
border-bottom-color: #888888;
display: none;
}
JS文件
function ranColor() {
var color = '#';
for (var i = 0; i < 6; i++) {
color += '0123456789abcdef'[Math.floor(Math.random() * 16)]
}
return color;
}
var arr=[0,0,0,0,0,0,0,0,0,0];
for(var i=0;i<arr.length;i++)
{
arr[i]=Math.floor(Math.random()*250+50);
var e = document.createElement('div');
e.classList.add("element");
e.style.left=i*50+60+'px';
e.style.height=arr[i]+'px';
e.style.backgroundColor=ranColor();
document.getElementById("stage").appendChild(e);
var f = document.createElement('div');
f.classList.add("number");
f.style.top='-35px';
f.innerHTML=arr[i];
var p =document.createElement('div');
p.classList.add("position");
p.style.bottom='-10px';
e.appendChild(f);
e.appendChild(p);
f.style.left=(30-f.offsetWidth)/2+'px';
p.style.left=(30-p.offsetWidth)/2-5+'px';
}
document.body.onresize = function(){
document.getElementById("stage").style.left = document.getElementById("body").offsetWidth/2-document.getElementById("stage").offsetWidth/2+'px';
document.getElementById("stage").style.top = document.getElementById("body").offsetHeight/2-document.getElementById("stage").offsetHeight/2+'px';
}
document.body.onpageshow = function(){
document.getElementById("stage").style.left = document.getElementById("body").offsetWidth/2-document.getElementById("stage").offsetWidth/2+'px';
document.getElementById("stage").style.top = document.getElementById("body").offsetHeight/2-document.getElementById("stage").offsetHeight/2+'px';
}
var i=0,j=arr.length-1;
document.getElementById("stage").onclick = function(){
var r=document.getElementsByClassName("element");
if(i<arr.length-1)
{
if(j>i)
{
if(i-1>=0)
r[i-1].getElementsByClassName("position")[0].style.display="none";
r[i].getElementsByClassName("position")[0].style.display="none";
if(j+1<arr.length)
r[j+1].getElementsByClassName("position")[0].style.display="none";
r[j].getElementsByClassName("position")[0].style.display="none";
if(r[j].offsetHeight>r[j-1].offsetHeight)
{
var p=r[j].offsetHeight;
r[j].style.height=r[j-1].style.height;
r[j-1].style.height=p+'px';
var t=r[j].getElementsByClassName("number")[0].innerHTML;
r[j].getElementsByClassName("number")[0].innerHTML=r[j-1].getElementsByClassName("number")[0].innerHTML;
r[j-1].getElementsByClassName("number")[0].innerHTML=t;
r[j-1].getElementsByClassName("number")[0].style.left=(30-r[j-1].getElementsByClassName("number")[0].offsetWidth)/2+'px';
r[j].getElementsByClassName("number")[0].style.left=(30-r[j].getElementsByClassName("number")[0].offsetWidth)/2+'px';
var c=r[j].style.backgroundColor;
r[j].style.backgroundColor=r[j-1].style.backgroundColor;
r[j-1].style.backgroundColor=c;
}
r[j].getElementsByClassName("position")[0].style.display="block";
r[j-1].getElementsByClassName("position")[0].style.display="block";
j--;
}
else
{
j=arr.length-1;
i++;
}
}
}
setInterval(function(){
var b = document.getElementById('stage');
b.click();
},500);
效果图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200519115121391.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU4OTM3NA==,size_16,color_FFFFFF,t_70)