web前端案例之抽奖

使用HTML+Javascript完成抽奖案例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			*{
				margin: 0;
				padding: 0;
			}
			
		</style>
	</head>
	<body>
		<div id="container" onclick="div_click(this)"></div>
		<script>
			function div_click(event){
				console.log(event)
			}
			let div = document.querySelector("#container")
			div.style.width = 400 + "px"
			div.style.height = 400 + "px"
			div.style.border = "1px solid red"
			div.style.margin = "auto"
			div.style.textAlign = "center"
			
			let circle = document.createElement("div")
			circle.style.width = 280 + "px"
			circle.style.height = 280 + "px"
			circle.style.borderRadius = '50%'
			circle.style.backgroundColor = "red"
			circle.style.margin = "auto"
			circle.textContent = "一等奖"
			circle.style.fontSize = '30px'
			circle.style.color = "white"
			circle.style.lineHeight = 280 + "px"
			circle.style.textAlign = "center"
			div.appendChild(circle)
			
			let button = document.createElement("button")
			button.style.width = '280px'
			button.style.height = '40px'
			button.style.textAlign = "center"
			button.style.margin = "auto"
			button.style.marginTop = "30px"
			button.innerText = "开始抽奖"
			
			let btn_status = "开始抽奖"			
			button.onclick = function(){
				if (btn_status == "开始抽奖"){	
					btn_status = "停止"
					id = setInterval(random_check, 100)
				}else{
					btn_status = "开始抽奖"
					button.innerText = btn_status
					clearInterval(id)
				}
			}
			div.appendChild(button)
			
			let arr = ['一等奖', '二等奖', '三等奖', '安慰奖']			
			
			function random_check(){
				let index = Math.round(Math.random()*3)
				circle.innerHTML = arr[index]
				button.innerText = btn_status
			}
		</script>
	</body>
</html>

实现图片:

开始时:

抽奖时: 

 

相关推荐

  1. web前端错误案例:深度剖析与防范

    2024-01-11 01:12:02       37 阅读
  2. Web前端写随机抽奖:技术与创意的碰撞

    2024-01-11 01:12:02       29 阅读
  3. web前端css、style

    2024-01-11 01:12:02       65 阅读
  4. 前端设计模式】抽象工厂模式

    2024-01-11 01:12:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 01:12:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 01:12:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 01:12:02       87 阅读
  4. Python语言-面向对象

    2024-01-11 01:12:02       96 阅读

热门阅读

  1. Linux防火墙相关命令

    2024-01-11 01:12:02       59 阅读
  2. Kubernetes 调度器及其优化

    2024-01-11 01:12:02       46 阅读
  3. 3个Linux文件权限命令

    2024-01-11 01:12:02       55 阅读
  4. LeetCode 0383. 赎金信:计数

    2024-01-11 01:12:02       62 阅读
  5. sap报表导入查询数据检查

    2024-01-11 01:12:02       43 阅读
  6. 【笔记】Helm-2 如何使用-2 同步你的仓库

    2024-01-11 01:12:02       62 阅读
  7. linux网络

    2024-01-11 01:12:02       48 阅读