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>

三、实验效果

相关推荐
东东5161 小时前
基于ssm的网上房屋中介管理系统vue
前端·javascript·vue.js
harrain2 小时前
什么!vue3.4开始,v-model不能用在prop上
前端·javascript·vue.js
阿蒙Amon7 小时前
TypeScript学习-第7章:泛型(Generic)
javascript·学习·typescript
睡美人的小仙女1277 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
fanruitian8 小时前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo8 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk8 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
摘星编程8 小时前
React Native + OpenHarmony:Timeline垂直时间轴
javascript·react native·react.js
2501_944525549 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
jin12332210 小时前
React Native鸿蒙跨平台完成剧本杀组队详情页面,可以复用桌游、团建、赛事等各类组队详情页开发
javascript·react native·react.js·ecmascript·harmonyos