js:九宫格抽奖

项目需要实现一个九宫格抽奖的模板,当然是微信小程序版本的,但是现在有点时间,就整理了个js版本的(兼容性,使用jq的dom操作)

当然,不喜欢用jq的同学可以直接使用document来操作,如果不喜欢dom操作的,就移步下一篇博客的微信小程序版本的


代码如下:

<!DOCTYPE html>
<html>
	<style>
		li {
			width: 200px;
			height: 200px;
		}
		
		.ul {
			width: 606px;
			height: 606px;
		}
		
		.ul li {
			float: left;
			border: 1px solid #000000;
			list-style: none;
			line-height: 200px;
			text-align: center;
			font-size: 50px;
		}
	</style>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<div class="ul">
			<li class="li1">1</li>
			<li class="li2">2</li>
			<li class="li3">3</li>
			<li class="li8">8</li>
			<li class="listart">开始</li>
			<li class="li4">4</li>
			<li class="li7">7</li>
			<li class="li6">6</li>
			<li class="li5">5</li>
		</div>
		<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
		<script>
			var last_index = 0, //上一回滚动的位置
				amplification_index = 0, //轮盘的当前滚动位置,0表示为第一次
				roll_flag = true, //是否允许滚动
				max_number = 8, //轮盘的全部数量
				speed = 300, //速度,速度值越大,则越慢 初始化为300
				finalindex = 3, //最终的奖励
				myInterval = "", //定时器
				max_speed = 40, //滚盘的最大速度
				minturns = 8, //最小的圈数为2
				runs_now = 0; //当前已跑步数
			$(".listart").bind("click", function() {
				//初始化步数
				runs_now = 0;
				//当前可以点击的状态下
				if(roll_flag) {
					roll_flag = false;
					//启动滚盘,注,若是最终后台无返回就不好意思里
					rolling();
				}
			});

			//滚动轮盘的动画效果
			function rolling() {
				myInterval = setTimeout(function() {
					rolling();
				}, speed);
				runs_now++; //已经跑步数加一
				amplification_index++; //当前的加一
				//获取总步数,接口延迟问题,所以最后还是设置成1s以上
				var count_num = minturns * max_number + finalindex - last_index;
				console.log(count_num);
				//上升期间
				if(runs_now <= (count_num / 3) * 2) {
					speed -= 30; //加速
					if(speed <= max_speed) {
						speed = max_speed; //最高速度为40;
					}
				}
				//抽奖结束
				else if(runs_now >= count_num) {
					clearInterval(myInterval);
					last_index = amplification_index;
					roll_flag = true;

				}
				//下降期间
				else if(count_num - runs_now <= 10) {
					speed += 20;
				}
				//缓冲区间
				else {
					speed += 10;
					if(speed >= 100) {
						speed = 100; //最低速度为100;
					}
				}
				if(amplification_index > max_number) { //判定!是否大于最大数
					amplification_index = 1;
				}

				//刷新页面
				var strli = ".li";
				strli += amplification_index;
				//全部清除
				$("li").each(function() {
					$(this).css("background", "#ffffff");
				})
				//画颜色
				$(strli).css("background", "red");
			}
		</script>
	</body>

</html>
没有多大的麻烦。而且有备注,就不多解释了

效果图如下:




  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
以下是初学者JS实现九宫格抽奖的代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>九宫格抽奖</title> <style> #box { width: 300px; height: 300px; margin: 0 auto; position: relative; } .cell { width: 100px; height: 100px; position: absolute; border: 1px solid #000; box-sizing: border-box; } .cell span { display: block; width: 100%; height: 100%; line-height: 100px; text-align: center; font-size: 30px; } #start { width: 100px; height: 50px; margin: 20px auto; display: block; font-size: 20px; } </style> </head> <body> <div id="box"> <div class="cell" style="left: 0; top: 0;"><span>1</span></div> <div class="cell" style="left: 100px; top: 0;"><span>2</span></div> <div class="cell" style="left: 200px; top: 0;"><span>3</span></div> <div class="cell" style="left: 200px; top: 100px;"><span>4</span></div> <div class="cell" style="left: 200px; top: 200px;"><span>5</span></div> <div class="cell" style="left: 100px; top: 200px;"><span>6</span></div> <div class="cell" style="left: 0; top: 200px;"><span>7</span></div> <div class="cell" style="left: 0; top: 100px;"><span>8</span></div> <div class="cell" style="left: 100px; top: 100px;"><span>9</span></div> </div> <button id="start">开始</button> <script> var box = document.getElementById('box'); var cells = box.getElementsByClassName('cell'); var startBtn = document.getElementById('start'); var timer = null; var index = 0; var targetIndex = 0; var isMoving = false; // 点击开始按钮 startBtn.onclick = function() { if (isMoving) { return; } isMoving = true; targetIndex = Math.floor(Math.random() * 9); move(); } // 移动单元格 function move() { cells[index].style.borderColor = '#000'; index++; if (index > 8) { index = 0; } cells[index].style.borderColor = 'red'; if (index == targetIndex) { clearInterval(timer); isMoving = false; } } // 定时器 timer = setInterval(move, 100); </script> </body> </html> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值