用JS怎样实现鼠标移动格子随机变色的效果?
发布时间:2022-01-12 01:49:36 所属栏目:语言 来源:互联网
导读:这篇文章给大家分享的是用JS实现鼠标移动格子随机变色的内容,也就是鼠标移入可以随机变换颜色的效果,对大家学习鼠标移动事件有一定的帮助,感兴趣的朋友可以参考,接下来一起跟随小编看看吧。 今天分享一个在 JavaScript中,实现一个鼠标移入可以随机变换
这篇文章给大家分享的是用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> (编辑:好传媒网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |