当前位置:网站首页>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>
边栏推荐
- leetcode 300. Longest Increasing Subsequence 最长递增子序列 (中等)
- LeetCode 31 ~ 40
- NDK20b FFmpeg4.2.2 编译和集成
- Find the multiplication order of n
- SDRAM Controller - add read / write FIFO
- Theoretical speed calculation method of WiFi
- 购买了fastadmin小程序助手但是问题工单中无法发布工单
- Chrome浏览器开发者工具使用
- Connectez Le projecteur
- Jenkins localization and its invalid solution
猜你喜欢
随机推荐
vtk初始化代码学习1
Visual studio 2013 redistributable is installed, but MySQL installation fails
Dazhou suggested that we media bloggers do these four things in the early stage
连接投影仪
跨域问题的一种解决方案
Redis linked list
Redis7.0 installation steps
Three factors affecting personal growth
Raspberry pie + AWS IOT Greengrass
Command of gun make (4) rule
Connectez Le projecteur
Ardiuno智能电蚊拍
Getting to know OpenGL
【js】免费api判断节假日、工作日和周六日
记录一个诡异的图片上传问题
qtvtkvs2015测试代码
shell学习记录(三)
其他代码,,vt,,,k
图形渲染管线
socket demo01









