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>

三、实验效果

相关推荐
小李小李不讲道理14 分钟前
「Ant Design 组件库探索」四:Input组件
前端·javascript·react.js
连合机器人34 分钟前
晨曦中的守望者:当科技为景区赋予温度
java·前端·科技
郑板桥301 小时前
tua-body-scroll-lock踩坑记录
前端·javascript
小*-^-*九1 小时前
php 使用html 生成pdf word wkhtmltopdf 系列2
pdf·html·php
解道Jdon1 小时前
SpringBoot4与Spring7发布:云原生深度进化
javascript·reactjs
慢半拍iii2 小时前
JAVA Web —— A / 网页开发基础
前端
gnip2 小时前
pnpm 的 monorepo架构多包管理
前端·javascript
zolty2 小时前
基于hiprint的票据定位打印系统开发实践
javascript
新手村领路人4 小时前
Firefox自定义备忘
前端·firefox
乖女子@@@4 小时前
css3新增-网格Grid布局
前端·css·css3