美文网首页前端开发那些事儿
js制作图片跟随鼠标移动

js制作图片跟随鼠标移动

作者: 兰德索尔 | 来源:发表于2020-11-13 11:39 被阅读0次

<!DOCTYPE html>

<html>


<head>

<meta charset="utf-8">

<title></title>

<style type="text/css">

img{position: abso

lute;}

</style>

</head>

<body>

<img src="滑稽.webp" >

<script type="text/javascript">

let img = document.querySelector('img')         //获取img标签

// console.log(img)

document.addEventListener('mousemove',function(e){ //悬浮时

let x = e.clientX //获取x轴

let y = e.clientY //获取y轴

img.style.top = y + 'px' //添加样式

img.style.left = x + 'px' //添加样式

})

</script>

</body>

</html>

相关文章

网友评论

    本文标题:js制作图片跟随鼠标移动

    本文链接:https://www.haomeiwen.com/subject/weevbktx.html