打开网页后,页面上会出现一个60px * 60px的黄色小方块,如果用鼠标点击小方块,这个小方块便会“瞬间移动”到页面上一个随机的位置,点击小方块之外的区域则无反应
你可以看看下面的代码实现,望采纳
<html>
<head>
<style>
#square {
width: 60px;
height: 60px;
background-color: yellow;
position: absolute;
}
</style>
<script>
function moveSquare() {
var square = document.getElementById("square");
var maxX = window.innerWidth - square.offsetWidth;
var maxY = window.innerHeight - square.offsetHeight;
square.style.left = Math.floor(Math.random() * maxX) + "px";
square.style.top = Math.floor(Math.random() * maxY) + "px";
}
</script>
</head>
<body>
<div id="square" onclick="moveSquare()"></div>
</body>
</html>