<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>面向对象</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
.box {
width: 1200px;
height: 500px;
border: solid 1px red;
margin: 0 auto;
position: relative;
overflow: hidden;
}
.qi {
width: 38px;
height: 60px;
border: solid 1px #000;
border-radius: 100%;
text-align: center;
line-height: 60px;
font-size: 24px;
position: absolute;
}
</style>
</head>
<body>
<div id="box" class="box">
</div>
<script type="text/javascript">
var box = document.getElementById('box');
function Ballon(className, width, height, x, y) {
this.text = Ballon.textwb[Ballon.sj(0, Ballon.textwb.length - 1)].toUpperCase()
this.className = className;
this.speed = Ballon.sj(10, 20);
this.dom = document.createElement('div');
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.render = function() {
this.dom.className = this.className;
this.dom.innerHTML = this.text;
this.dom.style.top = this.y + 'px';
}
this.move = function() {
this.x += this.speed;
this.dom.style.left = this.x + 'px'
}
this.show = function(parent) {
parent.appendChild(this.dom)
}
this.hide = function() {
this.dom.remove()
}
}
Ballon.textwb = 'abcdefghijklmnopqrstuvwxyz'
Ballon.sj = function(min, max) {
return parseInt(Math.random() * (max - min + 1)) + min;
}
var arr = []
setInterval(() => {
var sun = new Ballon('qi', 38, 60, 0, Ballon.sj(0, box.clientHeight - 62))
arr.push(sun)
sun.render()
sun.show(box)
sun.move()
}, 1000)
setInterval(() => {
arr.forEach(function(value, idx) {
value.move()
})
}, 50)
document.onkeydown = function(e) {
arr.forEach(function(value,idx) {
if(value.text == e.key.toUpperCase()){
value.hide()
arr.splice(idx,1)
}
})
}
</script>
</body>
</html>