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>

三、实验效果

相关推荐
追光少年33221 小时前
Learning Vue 读书笔记 Chapter 4
前端·javascript·vue.js
软件2051 小时前
【Vite + Vue + Ts 项目三个 tsconfig 文件】
前端·javascript·vue.js
老大白菜1 小时前
在 Ubuntu 中使用 FastAPI 创建一个简单的 Web 应用程序
前端·ubuntu·fastapi
渔阳节度使1 小时前
React
前端·react.js·前端框架
LCG元3 小时前
Vue.js组件开发-如何实现异步组件
前端·javascript·vue.js
Lorcian3 小时前
web前端12--表单和表格
前端·css·笔记·html5·visual studio code
问道飞鱼3 小时前
【前端知识】常用CSS样式举例
前端·css
wl85113 小时前
vue入门到实战 三
前端·javascript·vue.js
ljz20164 小时前
本地搭建deepseek-r1
前端·javascript·vue.js
爱是小小的癌4 小时前
Java-数据结构-优先级队列(堆)
java·前端·数据结构