- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <svg id="mySvg" style="width: 600px; height: 300px; border: 1px solid blue;">
- <circle id="myCircle" cx="100" cy="100" r="30">
- </circle>
- </svg>
- <script>
- var svg = document. getElementById("mySvg");
- var circle = document.getElementById("myCircle");
- svg.onmousemove =function (event) {
- var x = event.offsetX;
- var y= event .offsetY;
- circle.setAttribute("cx",x);
- circle.setAttribute("cy",y);
- //svg 不能直接赋值
- }
- </script>
- </body>
- </html>html 代码
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <svg id="mySvg" style="width: 600px; height: 300px; border: 1px solid blue;">
- <circle id="myCircle" cx="100" cy="100" r="30">
- </circle>
- </svg>
- <script>
- var svg = document. getElementById("mySvg");
- var circle = document.getElementById("myCircle");
- svg.onmousemove =function (event) {
- var x = event.offsetX;
- var y= event .offsetY;
- circle.setAttribute("cx",x);
- circle.setAttribute("cy",y);
- //svg 不能直接赋值
- }
- </script>
- </body>
- </html>
来源: http://www.qdfuns.com/article/39482/e436725f460f26a9de087e903cd512b7.html