引入一张图片 设置大小样式
<!DOCTYPE html> <html lang="en"> <head> <style> img { width: 200px; height: 200px; /* 圆角 */ border-radius: 50%; /* 子绝 父相(此时父元素是body)*/ position: absolute; } </style> </head> <body> <img src="../img/10.jpg" alt=""> </body> </html>
效果
js 代码
代码1、2相同
<script> var img = document.querySelector('img') // 绑定的是页面文档(鼠标在页面上移动而移动) //代码1 document.onmousemove=function(e){ // 给予可视区域的坐标 img.style.left = e.clientX + 'px' img.style.top = e.clientY + 'px' console.log('横坐标' + e.clientX + ',纵坐标' + e.clientY); } </script>
<script> var img = document.querySelector('img') // 绑定的是页面文档(鼠标在页面上移动而移动) // 代码2 document.addEventListener('mousemove', function(e) { // 给予可视区域的坐标 img.style.left = e.clientX + 'px' img.style.top = e.clientY + 'px' console.log('横坐标' + e.clientX + ',纵坐标' + e.clientY); }) </script>
效果