JS九宫格抽奖制作

1.先写好html 页面要呈现的内容。

HTML内容如下:

<div class="Sudoku">
			<div class="one">口红</div>
			<div class="two">耳机</div>
			<div class="three">衣服</div>
			<div class="four">华为p60</div>
			<div class="five">键盘</div>
			<div class="six">美甲</div>
			<div class="seven">苹果15</div>
			<div class="eight">蛋糕</div>
			<button class="click" id="clicks">点击抽奖</button>
		</div>

2.然后使用CSS样式给内容添加颜色 大小 做好定位。

CSS样式如下:

<style>
			.Sudoku {
				width: 380px;
				height: 380px;
				position: relative;
			}

			.click {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				margin: 20px;
				background-color: #aaff7f;
				position: absolute;
				margin-top: 140px;
				margin-left: 140px;
			}

			.one {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin: 20px;
			}

			.two {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin-left: 140px;
				margin-top: 20px;
			}

			.three {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin-left: 260px;
				margin-top: 20px;
			}

			.four {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin-left: 260px;
				margin-top: 140px;
			}

			.five {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin-left: 260px;
				margin-top: 260px;
			}

			.six {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin-left: 140px;
				margin-top: 260px;
			}

			.seven {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin-left: 20px;
				margin-top: 260px;
			}

			.eight {
				width: 100px;
				height: 100px;
				text-align: center;
				line-height: 100px;
				background-color: mistyrose;
				position: absolute;
				margin-left: 20px;
				margin-top: 140px;
			}
		</style>

JS代码如下:

<script>
			
			let clicks = document.getElementById('clicks');
			
			let Sudoku = document.getElementsByClassName('Sudoku')[0];
			let prizes = Sudoku.getElementsByTagName('div');
			console.log(prizes);
			
			let i = 0;
			
			let speed = 0;
			
			let time = 500;
			let inter;
			let num;
			
			clicks.onclick = asbo;
			function asbo() {
				prizes[i].style.backgroundColor = '#d6c5ff';
				inter = setInterval(rotates, time);
				num = Math.floor(Math.random() * prizes.length);
				console.log(num);
				clicks.onclick = null;
			}

			function rotates() {
				if (i < prizes.length - 1) {
					i++;
					prizes[i].style.backgroundColor = '#d6c5ff';
					prizes[i - 1].style.backgroundColor = '#FFE4E1';
					console.log(time);
				} else {
					i = 0;
					prizes[i].style.backgroundColor = '#d6c5ff';
					prizes[prizes.length - 1].style.backgroundColor = '#FFE4E1';
					speed++;
					console.log(speed);
				}
				if (speed < 2) {
					time -= 100;
					if (time < 100) {
						time = 100
					}
				} else if (speed > 4) {
					time += 70;
					if (time > 1000) {
						time = 1000;
					}

				}
				if (i == num && speed > 5) {
					clearInterval(inter);
					setTimeout(function() {
						alert("恭喜获得" + prizes[i].innerHTML);
						speed = 0;
						clicks.onclick = asbo;
					}, 300);
				} else {
					clearInterval(inter);
					inter = setInterval(rotates, time);
				}
			}
		</script>

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的九宫抽奖的 JavaScript 代码示例: HTML 结构: ```html <div class="lottery"> <div class="grid"> <div class="cell">1</div> <div class="cell">2</div> <div class="cell">3</div> <div class="cell">4</div> <div class="cell">5</div> <div class="cell">6</div> <div class="cell">7</div> <div class="cell">8</div> <div class="cell">9</div> </div> <button class="btn" onclick="startLottery()">开始抽奖</button> </div> ``` CSS 样式: ```css .lottery { display: flex; flex-direction: column; align-items: center; } .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 300px; height: 300px; padding: 10px; background-color: #eee; } .cell { display: flex; justify-content: center; align-items: center; font-size: 48px; color: #333; background-color: #fff; border-radius: 10px; cursor: pointer; transition: all 0.3s ease-in-out; } .cell:hover { box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); } .btn { padding: 10px 20px; font-size: 20px; color: #fff; background-color: #f00; border: none; border-radius: 5px; cursor: pointer; transition: all 0.3s ease-in-out; } .btn:hover { background-color: #c00; } ``` JavaScript 代码: ```js const cells = document.querySelectorAll('.cell'); const btn = document.querySelector('.btn'); let timer; function startLottery() { btn.disabled = true; let count = 0; timer = setInterval(() => { cells.forEach(cell => { cell.classList.remove('active'); }); let index = Math.floor(Math.random() * cells.length); cells[index].classList.add('active'); count++; if (count === 20) { clearInterval(timer); btn.disabled = false; alert(`恭喜您抽中了 ${cells[index].textContent}!`); } }, 100); } cells.forEach(cell => { cell.addEventListener('click', () => { alert(`您选择了 ${cell.textContent}!`); }); }); ``` 这个代码通过将九个子的 HTML 元素保存到一个数组中,然后在点击“开始抽奖”按钮后启动一个定时器,每隔一段时间就随机选择一个子并给其添加 active 类名,同时移除其他所有子的 active 类名,以模拟子的闪烁效果。当定时器运行了一定次数后,就停止定时器并弹出一个提示框,告诉用户他/她抽中了哪个子。 同时,这个代码还为每个子添加了点击事件监听器,当用户点击子时,会弹出一个提示框,告诉用户他/她选择了哪个子。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值