弹块(广告悬浮窗)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			* {
				margin: 0;
				padding: 0;
				box-sizing: border-box;
			}

			div {
				position: relative;
				width: 100px;
				height: 100px;
				/* background-image: url("./imges/88.jpeg"); */
				background-color: red;
				border: 2px solid black;

			}

			#but {
				position: absolute;
				background-color: aqua;
				right: 0;
				top: 0;
				/* 指针/手型图标 */
				cursor: pointer;
			}
		</style>
	</head>
	<body>
		<div id="myDiv">
			<button id="but" onclick="myClose()">关闭</button>
		</div>
		<script>
			var div = document.getElementById("myDiv");
			var but = document.getElementById("but");
			var timer = setInterval(move, 100);
			var x = -10;
			var y = -10;

			function move() {

				var wt = document.documentElement.clientWidth;
				var ht = document.documentElement.clientHeight;
				// console.log("wt=", wt);
				// console.log("ht=", ht);
				var lf = parseInt(getComputedStyle(div, null)["left"]);
				var tp = parseInt(getComputedStyle(div, null)["top"]);
				//判断上边是否相交,
				//判断下边是否相交
				if (div.getBoundingClientRect().top <= 0 || div.getBoundingClientRect().bottom >= ht) {
					y = -y;
				}
				//判断左边是否相交
				//判断右边是否相交
				if (div.getBoundingClientRect().left <= 0 || div.getBoundingClientRect().right >= wt) {
					x = -x;
				}
				div.style.left = lf + x + "px";
				div.style.top = tp + y + "px";
			}

			function myClose() {
				div.remove();
			}
			div.onmousemove = function() {
				clearInterval(timer);
			}
			div.onmouseout = function() {
				timer = setInterval(move, 100);
			}
		</script>
	</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值