用JS控制图片随鼠标移动

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
       img
       {
           position:fixed;
       }
    </style>
    <script type="text/javascript">
        var picture;
        window.onload=function()
        {
            picture = document.getElementById("picture1");
            window.onmousemove = onMove;
        }


        function onMove(event)
        {
            var x = event.clientX;
            var y = event.clientY;
            picture.style.top = y + "px";
            picture.style.left = x + "px";
        }
    </script>
</head>
<body>
        <img src="Img/1330721580922.gif" id="picture1" />
</body>
</html>

里面主要用到的样式是position:fixed;和事件参数对象

浏览器在调用事件方法时,会创建一个事件参数对象,并将其传入到事件方法中

例如:

原文地址:https://www.cnblogs.com/miaoying/p/5280793.html