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>

三、实验效果

相关推荐

  1. 51单片机

    2024-01-11 09:22:04       29 阅读
  2. STM32——

    2024-01-11 09:22:04       32 阅读
  3. ARM:汇编

    2024-01-11 09:22:04       19 阅读
  4. 单片机-亮LED

    2024-01-11 09:22:04       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-11 09:22:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-11 09:22:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-11 09:22:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-11 09:22:04       20 阅读

热门阅读

  1. Hive分组取满足某字段的记录

    2024-01-11 09:22:04       33 阅读
  2. Https

    2024-01-11 09:22:04       29 阅读
  3. Go语言开发IDE介绍

    2024-01-11 09:22:04       37 阅读
  4. Docker的基本管理

    2024-01-11 09:22:04       29 阅读
  5. centos7自动清理nohup.out文件、*.log文件

    2024-01-11 09:22:04       41 阅读
  6. JPA的复杂查询包括一对多多对一和多对多的查询

    2024-01-11 09:22:04       35 阅读