当前位置:网站首页>Small ball bouncing against the wall
Small ball bouncing against the wall
2022-06-26 02:10:00 【Asmruan】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
header {
width: 600px;
height: 500px;
border: 10px dashed red;
position: relative;
}
div {
width: 100px;
height: 100px;
background-color: blue;
border-radius: 50%;
position: absolute;
}
button {
font-size: 30px;
padding: 6px 10px;
}
</style>
</head>
<body>
<header>
<div></div>
</header>
<button> Start </button>
<button disabled> Pause </button>
</body>
</html>
<script>
var header = document.querySelector("header");
var div = document.querySelector("div");
var timer = null;
// Initial value
var x = 0;
var y = 0;
// Direction
var addX = true;
var addY = true;
// critical value The width of the box - The diameter of the ball
var xMax = header.scrollWidth - div.offsetWidth;
var yMax = header.scrollHeight - div.offsetHeight;
document.querySelector("button").onclick = function () {
// Pause release
document.querySelectorAll("button")[1].disabled = false;
this.disabled = true;
timer = setInterval(function () {
// div.style.left = num + "px"
// div.style.top = num + "px"
// Specify an initial speed and direction
if (addX) {
x++;
// Judge the warning value
if (x >= xMax) {
// At this time, the horizontal direction reaches the maximum value , Change direction
addX = false;
} else {
// Horizontal assignment
div.style.left = x + "px";
}
} else {
// redirect
x--;
// Judge the warning value
if (x <= 0) {
// At this time, the horizontal direction reaches the maximum value , Change direction
addX = true;
} else {
// Horizontal assignment
div.style.left = x + "px";
}
}
// vertical direction
if (addY) {
y+=10;
if (y >= yMax) {
addY = false;
} else {
div.style.top = y + "px";
}
} else {
y--;
if (y <= 0) {
addY = true;
} else {
div.style.top = y + "px";
}
}
}, 1);
};
document.querySelectorAll("button")[1].onclick = function () {
document.querySelector("button").disabled = false;
this.disabled = true;
clearInterval(timer);
};
</script>
边栏推荐
- Multi type study of Worthington collagen protease
- Analytic hierarchy process
- 静态库动态库的使用
- Meaning of each state in TCP network communication
- Input 3 integers and output them from large to small
- shell学习记录(一)
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
- Qtvtkvs2015 test code
- 启牛推荐的证券账户安全吗?
- Ndk20b ffmpeg4.2.2 compilation and integration
猜你喜欢

Interface test case design

SDRAM controller -- implementation of arbitration module

win32

shell学习记录(二)

shell学习记录(一)

Abnova CMV CISH probe solution

论文阅读 Exploring Temporal Information for Dynamic Network Embedding

Raspberry pie + AWS IOT introductory experiment

One minute to understand the difference between synchronous, asynchronous, blocking and non blocking

SQL column value to row value (unpivot)
随机推荐
Other codes,, VT,,, K
将lua print输出到cocos2d控制台输出窗口中
Connectez Le projecteur
Meaning of each state in TCP network communication
Command of gun make (4) rule
Qtvtkvs2015 test code
Redis-链表
shell学习记录(一)
SQL column value to row value (unpivot)
Steps of program compilation precompile compilation assembly connection
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
Chrome browser developer tool usage
Redis7.0 installation steps
Application and chemical properties of elastase
Pointnet/Pointnet++学习
树莓派 + AWS IoT Greengrass
Prometeus 2.33.0 新特性
Shell learning record (III)
The first intimate contact of caching technology
连接投影仪