发布时间:2025-12-09 11:57:00 浏览次数:3
offsetLeft -返回当前元素的相对水平偏移位置的偏移容器
offsetTop -返回当前元素的相对垂直偏移位置的偏移容器
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8"> <style type="text/css"> #inner{ color: #fff; width: 100px; height: 100px; background-color: orange; position: absolute; left:0; top:100px; } </style></head><body> <p >行走的DIV</p> <script type="text/javascript"> var inner = document.getElementById('inner'); var timer = setInterval(function(){ //拿到当前位置 offsetLeft,+1px //再把最新位置重写赋值给inner inner.style.left = inner.offsetLeft + 1 + 'px'; //大于1000停止(记住读取位置就用 offsetLeft) if (inner.offsetLeft > 1000) { clearInterval(timer); } },10); </script></body></html>