<!DOCTYPE HTML>
<html lang = "en">
<head>
<meta charset = "UTF-8"/>
<style>
</style>
</head>
<body>
<script>
//直接在script中创建一个div,但是此时不在页面里面
var div = document.createElement('div');
//用appendChild插入到body里面去
document.body.appendChild(div);
div.style.width = '200px';
div.style.height = '200px';
div.style.backgroundColor = 'red';
div.style.position = 'absolute';
/*
div.onclick = function (){
div.style.left = (Math.random() * 200 - 50) + 'px';
div.style.top = (Math.random() * 200 - 50) + 'px';
div.style.right = (Math.random() * 200 - 50) + 'px';
div.style.bottom = (Math.random() * 200 - 50) + 'px';
};*/
//setInterval(操作,)计时函数
setInterval(function (){
div.style.left = (Math.random() * 200 - 50) + 'px';
div.style.top = (Math.random() * 200 - 50) + 'px';
div.style.right = (Math.random() * 200 - 50) + 'px';
div.style.bottom = (Math.random() * 200 - 50) + 'px';
}, 100);
</script>
</body>
</html>
dom操作实现div运动
最新推荐文章于 2022-04-05 12:41:53 发布