九宫格抽奖带跑马灯代码实现

时间: 2023-08-25 09:05:51 浏览: 42
以下是一种可能的九宫格抽奖带跑马灯的代码实现: HTML代码: ``` <div class="lottery-container"> <div class="lottery-grid"> <div class="lottery-cell" data-index="0">1</div> <div class="lottery-cell" data-index="1">2</div> <div class="lottery-cell" data-index="2">3</div> <div class="lottery-cell" data-index="3">4</div> <div class="lottery-cell" data-index="4">5</div> <div class="lottery-cell" data-index="5">6</div> <div class="lottery-cell" data-index="6">7</div> <div class="lottery-cell" data-index="7">8</div> <div class="lottery-cell" data-index="8">9</div> </div> <div class="lottery-result">恭喜您获得<span class="result-text"></span></div> <div class="lottery-marquee"> <div class="marquee-list"> <div class="marquee-item">恭喜用户A获得了奖品1!</div> <div class="marquee-item">恭喜用户B获得了奖品2!</div> <div class="marquee-item">恭喜用户C获得了奖品3!</div> <div class="marquee-item">恭喜用户D获得了奖品4!</div> <div class="marquee-item">恭喜用户E获得了奖品5!</div> <div class="marquee-item">恭喜用户F获得了奖品6!</div> <div class="marquee-item">恭喜用户G获得了奖品7!</div> <div class="marquee-item">恭喜用户H获得了奖品8!</div> <div class="marquee-item">恭喜用户I获得了奖品9!</div> </div> </div> <div class="lottery-button">开始抽奖</div> </div> ``` CSS代码: ``` .lottery-container { position: relative; width: 300px; height: 300px; margin: 0 auto; font-size: 24px; text-align: center; } .lottery-grid { display: flex; flex-wrap: wrap; justify-content: space-between; width: 100%; height: 100%; background-color: #f2f2f2; } .lottery-cell { display: flex; justify-content: center; align-items: center; width: 32%; height: 32%; border: 1px solid #ccc; } .lottery-result { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); width: 80%; height: 40px; line-height: 40px; background-color: #fff; border: 1px solid #ccc; } .result-text { font-weight: bold; color: red; } .lottery-marquee { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); width: 80%; height: 40px; line-height: 40px; overflow: hidden; } .marquee-list { display: flex; width: 200%; animation: marquee 10s linear infinite; } .marquee-item { width: 50%; } .lottery-button { position: absolute; bottom: 50%; left: 50%; transform: translate(-50%, 50%); width: 120px; height: 50px; line-height: 50px; background-color: #ff5722; color: #fff; border-radius: 25px; cursor: pointer; } .lottery-button:hover { background-color: #ff7043; } @keyframes marquee { 0% { transform: translateX(0%); } 100% { transform: translateX(-50%); } } ``` JavaScript代码: ``` const grid = document.querySelector('.lottery-grid'); const cells = document.querySelectorAll('.lottery-cell'); const result = document.querySelector('.result-text'); const marquee = document.querySelector('.marquee-list'); const button = document.querySelector('.lottery-button'); let index = -1; let timer = null; function startLottery() { clearInterval(timer); index = -1; timer = setInterval(() => { // 取消上一个选中的格子的样式 if (index >= 0) { cells[index].classList.remove('selected'); } // 随机选中一个格子 index = Math.floor(Math.random() * cells.length); cells[index].classList.add('selected'); }, 100); } function stopLottery() { clearInterval(timer); result.textContent = cells[index].textContent; cells[index].classList.remove('selected'); marquee.insertBefore(document.createElement('div'), marquee.firstChild).textContent = `恭喜用户${Math.random().toString(36).substring(2)}获得了${result.textContent}!`; } button.addEventListener('click', () => { if (timer) { stopLottery(); } else { startLottery(); } }); ``` 说明: - HTML中的九宫格使用flex布局实现,每个格子使用data-index属性标记其在数组中的位置。 - CSS中的跑马灯使用CSS动画实现,滚动的速度和时间通过animation属性控制。 - JavaScript中的抽奖过程使用setInterval实现,每隔一定时间随机选中一个格子,并添加选中样式。停止抽奖时,清除定时器,显示中奖结果,取消选中样式,并在跑马灯中添加一条中奖信息,其中用户名随机生成。

相关推荐

最新推荐

recommend-type

Unity实现跑马灯抽奖效果

主要为大家详细介绍了Unity实现跑马灯抽奖效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

微信小程序 摇一摇抽奖简单实例实现代码

主要介绍了微信小程序 摇一摇抽奖简单实例实现代码的相关资料,这里实现摇一摇抽奖的功能,需要的朋友可以参考下
recommend-type

Python_ChatGLM3 series Open Bilingual Chat LLMs 开源双语对话语言模型.zip

Python_ChatGLM3 series Open Bilingual Chat LLMs 开源双语对话语言模型
recommend-type

附件1:1名男大学生的一日食谱.xlsx

附件1:1名男大学生的一日食谱.xlsx
recommend-type

用“假定成功”思路做爆品.pdf

用“假定成功”思路做爆品
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。