<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Javascript Test</title>
<style>
.container {
position : relative;
margin : 50px auto;
width : 600px;
height : 90px;
overflow : hidden;
background-color : #ccc;
}
.runner {
position : absolute;
top : 5px;
left : 0;
width : 80px;
height : 80px;
background-color : #00fdff;
}
</style>
</head>
<body>
<div class="container">
<div class="runner" id="runner"></div>
</div>
<script>
window.onload = function () {
let runner = document.getElementById('runner');
let speed = 2;
let timer = null;
timer = setInterval(function () {
let left = runner.offsetLeft;
if (left === 520) {
runner.style.left = '0px';
left = 0;
}
runner.style.left = left + speed + 'px';
}, 30);
};
</script>
</body>
</html>
|