JavaScript模仿弹窗BUG(~emo:有点好看哦)

先看图:

大致思路:

根据当前浏览器大小,用setInterval()函数每隔一定时间创建一个随机的div,并设置div的样式。

具体步骤:

1、将随机生成的div定位。

            * {
				margin: 0;
				padding: 0;
			}
			div {
				position: absolute;
			}

2、用setInterval()函数每隔一定时间创建一个div和删除按钮。

            start = setInterval(function() {
				let getDiv = document.createElement("div");
				let del = document.createElement("span");
			}, 100)

3、设置div和按钮的样式。

                //del删除按钮样式                
                del.innerText = "×";
				del.style.position = "absolute";
				del.style.right = "0";
				del.style.cursor = "pointer";
                //div样式
                let wid = getRandom(100, 150);//调用随机生成数函数getRandom()设置宽高
				let hig = getRandom(100, 150);//调用随机生成数函数getRandom()设置宽高
				getDiv.style.width = wid + "px";
				getDiv.style.height = hig + "px";
				getDiv.style.left = getRandom(0, window.innerWidth - wid) + "px";//调用随机生成数函数getRandom()设置位置
				getDiv.style.top = getRandom(0, window.innerHeight - hig) + "px";//调用随机生成数函数getRandom()设置位置
				let color = "#" + getRandom(0, 16777215).toString(16);//将随机生成的数转成16进制
				getDiv.style.backgroundColor = color;
				getDiv.style.opacity = Math.random();//透明度
                //生成随机数函数
                function getRandom(min, max) {
					let res = Math.abs(max - min);
					return Math.floor(Math.random() * (res + 1)) + min;
				}

4、将生成的div加入到body中。

                getDiv.appendChild(del);//将删除按钮加入到div中
                document.body.appendChild(getDiv);//将div加入到body中

5、实现删除按钮功能。

                //给按钮添加单击事件,单击删除其父节点
				del.onclick = function() {
					document.body.removeChild(this.parentNode);
				}

6、为了友好显示,设置一个计数器,并到一定数量时清除间隔期。

                if (count == 50) {
					clearInterval(start);
				}

所有代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			div {
				position: absolute;
			}
		</style>
	</head>
	<body>
		<script>
			let start;
			let count = 0;
			let randomX, randomY;
			//console.log(window.innerHeight + " : " + window.innerWidth);
			start = setInterval(function() {
				let randomX, randomY;
				let getDiv = document.createElement("div");
				let del = document.createElement("span");
				del.innerText = "×";
				del.style.position = "absolute";
				del.style.right = "0";
				del.style.cursor = "pointer";
				//给按钮添加单击事件,单击删除其父节点
				del.onclick = function() {
					document.body.removeChild(del.parentNode);
				}
				let wid = getRandom(100, 150);
				let hig = getRandom(100, 150);
				getDiv.style.width = wid + "px";
				getDiv.style.height = hig + "px";
				getDiv.style.left = getRandom(0, window.innerWidth - wid) + "px";
				getDiv.style.top = getRandom(0, window.innerHeight - hig) + "px";
				let color = "#" + getRandom(0, 16777215).toString(16);
				getDiv.style.backgroundColor = color;
				getDiv.style.opacity = Math.random();
                //添加按钮到div中
				getDiv.appendChild(del);
                //添加div到body中
				document.body.appendChild(getDiv);
				count++;
				if (count == 500) {
					clearInterval(start);
				}
				function getRandom(min, max) {
					let res = Math.abs(max - min);
					return Math.floor(Math.random() * (res + 1)) + min;
				}
			}, 100)
		</script>
	</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值