<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
#span {
position: absolute;
border: 1px solid red;
background-color: blue;
color:white;
width:100px;
}
</style>
<script>
onload = function () {
var left = 0;
var direction = 1;
setInterval(function () {
left += 10 * direction;
//移动到最右端
if (left + 100 > window.innerWidth) {
direction = -1;
}
//移动到最左端
if (left <= 0) {
direction = 1;
}
var span = document.getElementById('span');
span.style.left = left + 'px';
}, 100);
}
</script>
</head>
<body>
<span id="span">嘿嘿</span>
</body>
</html>