以下是一个简单的示例代码:
<html>
<head>
<style>
div {
position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
const box = document.querySelector("#box");
document.addEventListener("mousemove", (event) => {
box.style.top = event.clientY + "px";
box.style.left = event.clientX + "px";
});
</script>
</body>
</html>
这段代码使用 JavaScript 监听鼠标移动事件,更新 div 标签的位置,使其跟随鼠标移动。