html星星点灯

一、实验题目

用html实现星星点灯效果

二、实验代码

复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			* {
				margin: 0%;
				padding: 0%;
			}
		</style>
	</head>
	<body>
		<script>
			document.body.style.backgroundColor = "#000"

			document.onclick = function(event) {
				let img = document.createElement("img")
				img.src = "../pic/0.gif"
				img.style.position = "absolute"
				document.body.appendChild(img)
				img.onload = function() {
					const img_w = parseInt(getComputedStyle(img).width)
					const img_h = parseInt(getComputedStyle(img).height)
					w = getRandom(50, 200)
					h = img_h / img_w * w
					img.style.width = w + "px"
					img.style.height = h + "px"
					img.style.left = (event.pageX - w / 2) + "px"
					img.style.top = (event.pageY - h / 2) + "px"
				}
			}

			function getRandom(min, max) {
				return min + Math.ceil((max - min) * Math.random())
			}
		</script>
	</body>
</html>

三、实验效果

相关推荐
陈随易3 小时前
有生之年系列,Nodejs进程管理pm2 v7.0发布
前端·后端·程序员
冰暮流星3 小时前
javascript之事件代理/事件委托
前端
陈随易5 小时前
AI时代,你还在坚持手搓文章吗
前端·后端·程序员
里欧跑得慢7 小时前
17. Flutter Hero动画实现:让界面过渡更加优雅
前端·css·flutter·web
IT_陈寒7 小时前
Vue的这个响应式陷阱,我debug了一整天才爬出来
前端·人工智能·后端
cn_mengbei7 小时前
用React Native开发OpenHarmony应用:Reanimated共享元素过渡
javascript·react native·react.js
kyriewen7 小时前
前端测试:别为了100%覆盖率而写测试,那是自欺欺人
前端·javascript·单元测试
去伪存真8 小时前
我自己写的第一个skills--project-core-standards
前端·agent
Data_Journal8 小时前
如何使用cURL更改User Agent
大数据·服务器·前端·javascript·数据库
掌心向暖RPA自动化8 小时前
如何获取网页某个元素在屏幕可见部分的中心坐标影刀RPA懒加载坐标定位技巧
java·javascript·自动化·rpa·影刀rpa