这篇文章给大家分享的是用JS实现鼠标移动格子随机变色的内容,也就是鼠标移入可以随机变换颜色的效果,对大家学习鼠标移动事件有一定的帮助,感兴趣的朋友可以参考,接下来一起跟随小编看看吧。
今天分享一个在 JavaScript中,实现一个鼠标移入可以随机变换颜色。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> /* 这里定义一下div(块元素)已下span 标签的宽.高.边框线以及边框线的颜色*/ span{ display: block; width: 80px; height: 80px; border: 1px solid #000000; float: left; } </style> </head> <body> <div> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> <span></span><span></span><span></span><span></span><span></span><span></span> </div> <script type="text/javascript"> var a=document.getElementsByTagName("span"); /* 获取一下span标签 */ for(var i=0;i<=a.length;i++){ a[i].onmouseover=function(){ /* 循环出每一个方块,加入鼠标移入 */ this.style.backgroundColor="#"+Math.floor(Math.random()*16777215).toString(16); /* 颜色随机颜色 */ } } </script> </body> </html>
如下是代码运行后在这里插入图片描述
总结
以上就是JS实现鼠标移动格子随机变色的代码,上述示例具有一定的借鉴价值,有需要的朋友可以参考学习,希望对大家学习鼠标移动事件有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理