今天翻看移动硬盘内容发现之前写的鼠标点击效果。老古董了,有需要的参考下吧。
效果预览图:
网页源码:
- <!doctype html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>网页</title>
- <style>
- svg{
- background:#eee;
- }
- </style>
- </head>
- <body>
- <svg id="svg" width="500" height="400"></svg>
- <script>
- document.querySelector("svg").onclick=function(){
- var x=event.offsetX;
- var y=event.offsetY;
- console.log(x);
- console.log(y);
- var c=document.createElementNS("http://www.w3.org/2000/svg","circle");
- c.setAttribute("cx",x);
- c.setAttribute("cy",y);
- c.setAttribute("r",rn(5,20));
- c.setAttribute("fill","rgb("+rn(0,255)+","+rn(0,255)+","+rn(0,255)+")");
- c.setAttribute("opacity",1);
- svg.appendChild(c);
- var t=setInterval(function(){
- var r=parseFloat(c.getAttribute("r"));
- r+=5;
- c.setAttribute("r",r);
- var o=parseFloat(c.getAttribute("opacity"));
- o-=0.05;
- c.setAttribute("opacity",o);
- if(o<=0){
- clearInterval(t);
- c.parentNode.removeChild(c);
- }
- },100);
- }
- function rn(min,max){
- return parseInt(Math.random()*(max+1-min)+min);
- }
- </script>
- </body>
- </html>
复制代码,保存为html文件,鼠标可以在灰色区域点击查看效果。