本篇文章给大家介绍js如何实现图片跟随鼠标移动,这里会列举两种实现方法,希望对需要的朋友有所帮助!
这里列举了两种实现方法:
第一种
nbsp;html> <meta><title></title><style> img{ position: fixed; left: 0px; top: 0px; } </style><img alt="js实现图片跟随鼠标移动的两种方法" ><script> var img = document.querySelector('img'); // mousemove鼠标移动事件 document.addEventListener('mousemove',function(e){ var pagex = e.pageX-20+'px'; var pagey = e.pageY-20+'px'; // console.log(pagex,pagey); img.style.left = pagex; img.style.top = pagey; }) </script>
第二种
nbsp;html> <meta><title></title><style> img{ position: absolute; width: 80px; } </style><img alt="js实现图片跟随鼠标移动的两种方法" ><script> window.onmousemove = function(e){ var x = e.pageX; var y = e.pageY; img.style.left = x+'px'; img.style.top = y+'px'; } </script>
相关推荐:【JavaScript视频教程】
以上就是js实现图片跟随鼠标移动的两种方法的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号