【发布时间】:2021-04-23 07:30:03
【问题描述】:
我希望我的图像跟随光标,但我使用的代码不起作用。我无法解决这里的问题。
代码预览:
function followIt(e){
var img=document.getElementById("img");
img.style.left = e.clientX;
img.style.top = e.clientY;
}
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<style>
#img{
box-shadow: 0px 0px 1px 1px #000000;
width: 100px;
}
</style>
<body id="demo_body" onmousemove="followIt(event)">
<img src="https://i.postimg.cc/g02SqdrF/followmouse.png" id="img">
</body>
<script src="script.js" text="text/javascript"></script>
</html>
【问题讨论】:
-
提示:不要使用内联 JS
on*属性 - 就像你希望不要使用内联style属性一样。 Script 和 Style 应该只在一个地方,那就是它们各自的标签或文件。
标签: javascript html css dom-events