原生JS实现10*10小方块随机变色,鼠标悬浮显示rgb值,鼠标点击后消失,3s后出现

代码如下:

<!DOCTYPE html>
<html lang="en">
	<head>
	    <meta charset="UTF-8">
	    <meta http-equiv="X-UA-Compatible" content="IE=edge">
	    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	    <title>Document</title>
	</head>

	<body style="width:550px ;height:550px;margin-left: 400px;">
	    <script>
	        function createBox() {
	            console.log("createBox");
	            //每个盒子宽高都设置为50
	            let nDivWidth = 50;
	            let nDivHeight = 50;
	
	            //创建容器
	            //每个页面,最大的容器是body对象,所有dom对象创建后默认都会放到body
	            let container = document.body;
	            //也可以自行创建一个div对象,div对象创建如果不添加任何属性,相当于一个空的容器,它与body自大的区别是,它属于body的一部分,并且body有默认长宽尺寸(浏览器当前长宽), div默认没有长宽
	            //let container = document.createElement('div');
	
	            for (let row = 0; row < 10; row++) { 
	                for (let col = 0; col < 10; col++) {
	                    let oDiv = document.createElement("div"); //创建div
	                    oDiv.style.float = "left";
	                    oDiv.style.marginLeft = "5px"; //设置每个盒子的左间距为5px
	                    oDiv.style.marginTop = "5px"; //设置每个盒子的上间距为5px
	                    oDiv.style.height = nDivHeight + "px";
	                    oDiv.style.width = nDivWidth + "px";
	
	                    //下面随机生RGB颜色值, 随机 数介于 0-256之间
	                    oDiv.style.background = "RGB(" + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + ")";
	
	                    //注册鼠标悬浮事件 title
	                    oDiv.onmouseover = ()=>{oDiv.title = "我的RGB值:"+oDiv.style.background}
	
	                    //注册鼠标点击事件
	                    oDiv.onclick = function () {
	                        //点击后隐藏
	                        oDiv.style.visibility = "hidden";
	                        //2秒显示, 2000表示 两千毫秒
	                        setTimeout(()=>{oDiv.style.visibility = 'visible'},2000)
	                        
	                    }
						
						// 让这些盒子的背景色随机发生变化的方法
				        function changeColor(){
				        	document.getElementsByTagName('div').item(Math.random()*100).style.background = "RGB("+ Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")";
	    				}
				        window.setInterval(changeColor,1000);//每一秒随机变化一次颜色,setInterval定时调用			        
						
	                    //浏览器控制台可以看到当前div的排位
	                    console.log(row,col);
	                    //创建后把div放到容器内
	                    container.appendChild(oDiv);
	                }
	            }
	        }

        //无法使用window.onload方法,因为当前脚步挂载在页面上,相当于已经加载,方法创建后在脚本结尾直接调用整个程序即可
        	createBox();
        
    
    	</script>
	</body>
</html>

看看效果叭:

方块变色

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值