<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#pic {
position: fixed;
}
</style>
</head>
<body>
<img src="images/tianshi.gif" alt="" id="pic">
<script>
// 通过 鼠标移动事件给 图片添加 left 和 top 的值
// 获取元素
var pic = document.getElementById("pic");
// 给整个文档添加鼠标移动事件
document.onmousemove = function (e) {
e = e || window.event;
// 给元素的css 属性赋值
pic.style.left = e.clientX + "px";
pic.style.top = e.clientY + "px";
};
</script>
</body>
</html>
图片跟随鼠标移动
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:设置元素的大小和位置
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
JQuery鼠标跟随效果
#float { position: absolute; width: 100px; height: 50px; border: solid 1px; } 鼠标跟随效果
jquery function xhtml server border -
javascript 鼠标跟随特效代码及理解
javascript 鼠标跟随特效代码及理解
java script