html做一个抽奖游戏,js实现抽奖功能

大致样式如下:

d2ddf6a35a67

一、构思

奖励物品是通过接口获取的(img)

奖励结果是通过接口获取的(id)

抽奖的动画需要由慢到快再到慢

抽奖转动时间不能太短

抽奖结束需要回调

业务代码和功能代码要分离

二、先完成一个 UI

使用 flex 来布局,当 curGameIdx 等于当前奖品 index 时高亮

html:

:key="idx"

@click="beginGame">

开始游戏

class="game-item"

:class="{

active: idx === curGameIdx

}">

{{val}}

css:

.game-box {

display: flex;

flex-wrap: wrap;

text-align: center;

.game-item {

width: 1.25rem;

height: 0.3rem;

background: yellow;

border: 1px solid transparent;

transition: all 0.2s;

&.game-begin {

background: transparent;

}

&.active {

border: 1px solid black;

}

}

}

效果图:

d2ddf6a35a67

三、开始做动画效果

新建一个 Game 的 class,有有个 run 方法和 finish 方法

开始运行

动画的速度是变化的,使用 requestAnimationFrame 和 setInterval 有点不妥,所以:可以使用 setTimeout + speed 参数 来控制动画的速度。

class Game {

constructor(idx) {

this.idx = idx;

this.speed = 400;

}

addIdx(){

}

speedControl() {

}

finish() {

}

run(cb) {

this.speedControl();

setTimeout(() => {

this.addIdx();

!this.isFinish && this.run(cb);

}, this.speed);

}

}

结束运行

收到结束运行的通知时,需要先做减速动画,然后再停止在对应的 num,然后调用回调函数,所以先暂存结束回调和结束点,并将动画设置为减速。

finish(num, finishCb) {

this.oil = false;

this.endIdx = num;

this.finishCb = finishCb;

}

速度的控制

1、默认速度为加速(this.oil = true)通过是否达到预期速度来停止加速,当减速时同理。

2、为达到缓动结束效果,所以结束时间通过:到达最小速度 且 到达结束位置。

speedUp() {

this.speed -= 60;

}

speedDown() {

this.speed += 200;

}

speedControl() {

if (this.speed > this.Max_Speed) {

if (this.oil) {

this.speedUp();

}

}

if (!this.oil) {

if (this.speed < this.Min_Speed) {

this.speedDown();

} else if (this.endIdx === this.idx) {

this.isFinish = true;

typeof this.finishCb === 'function' && this.finishCb();

}

}

}

index 矫正

此时,上面 UI 是通过 v-for + flex 展示的,而动画的执行是转圈,所以需要矫正 index

更改上面 addIdx 方法,矫正 index,并将 ++index 取余

constructor(idx) {

this.idx = idx;

this.speed = 400;

this.order = null;

this.Order_List = [0,1,2,5,8,7,6,3];

this.Game_Box_Num = 8;

}

addIdx() {

this.idx = (++this.idx % this.Game_Box_Num);

this.order = this.Order_List[this.idx];

}

活动代码与业务代码互动

将需要交互的函数传递给 Game 的实例即可

// vue 代码

methods: {

updateGameIdx(order) {

this.curGameIdx = order;

},

gameFinish() {

this.playing = false;

console.log(this.curGameIdx, 'curGameIdx')

},

beginGame() {

if (this.playing) return;

this.playing = true;

this.curGameIdx = 0;

const game = new Game(this.curGameIdx);

game.run(this.updateGameIdx);

// 通过请求终止

setTimeout(() => {

game.finish(2, this.gameFinish)

}, 3000);

}

}

最后附上完整 Game 代码:

class Game {

constructor(idx) {

this.idx = idx;

this.speed = 400;

this.oil = true;

this.isFinish = false;

this.endIdx = null;

this.finishCb = function() {}

// 常量

this.Max_Speed = 100;

this.Min_Speed = 500;

this.Order_List = [0,1,2,5,8,7,6,3];

this.Game_Box_Num = 8;

}

speedUp() {

this.speed -= 60;

}

speedDown() {

this.speed += 200;

}

speedControl() {

if (this.speed > this.Max_Speed) {

if (this.oil) {

this.speedUp();

}

}

if (!this.oil) {

if (this.speed < this.Min_Speed) {

this.speedDown();

} else if (this.endIdx === this.idx) {

this.isFinish = true;

typeof this.finishCb === 'function' && this.finishCb();

}

}

}

finish(num, finishCb) {

this.oil = false;

this.endIdx = num;

this.finishCb = finishCb;

}

addIdx() {

this.idx = (++this.idx % this.Game_Box_Num);

}

run(cb) {

this.speedControl();

typeof cb === 'function' && cb(this.Order_List[this.idx]);

setTimeout(() => {

this.addIdx();

!this.isFinish && this.run(cb);

}, this.speed);

}

}

export default Game;

最后:样式比较粗略,主要还是实现功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的原神游戏抽奖活动页面的实现,包括 HTML、CSS 和 JavaScript: HTML: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>原神游戏抽奖活动</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <h1 class="title">原神游戏抽奖活动</h1> <div class="lottery-box"> <div class="lottery-wheel"></div> <div class="lottery-pointer"></div> </div> <button class="btn" id="draw-btn">点击抽奖</button> <div class="result" id="result"></div> </div> <script src="script.js"></script> </body> </html> ``` CSS: ```css *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: Arial, sans-serif; background-color: #f0f0f0; } .container { max-width: 960px; margin: 0 auto; padding: 40px; text-align: center; background-color: #fff; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .title { margin-bottom: 40px; } .lottery-box { position: relative; width: 300px; height: 300px; margin: 0 auto 40px; background-color: #ffebcd; border-radius: 50%; overflow: hidden; } .lottery-wheel { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 280px; height: 280px; background-color: #fff; border-radius: 50%; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .lottery-pointer { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) rotate(45deg); width: 0; height: 0; border-style: solid; border-width: 20px 0 20px 20px; border-color: transparent transparent transparent #f00; cursor: pointer; } .btn { display: block; margin: 0 auto 20px; padding: 10px 20px; font-size: 18px; font-weight: bold; text-transform: uppercase; color: #fff; background-color: #f00; border: none; border-radius: 5px; cursor: pointer; } .result { font-size: 24px; font-weight: bold; color: #f00; } ``` JavaScript: ```js const prizes = [ '角色 黄金周', '角色 公子鸭', '圣遗物 五连', '圣遗物 四连', '圣遗物 三连', '圣遗物 二连', '谢谢参与' ]; const btn = document.getElementById('draw-btn'); const resultEl = document.getElementById('result'); const wheel = document.querySelector('.lottery-wheel'); const pointer = document.querySelector('.lottery-pointer'); let isSpinning = false; btn.addEventListener('click', () => { if (isSpinning) return; // 防止连续点击 isSpinning = true; // 随机生成中奖结果 const rand = Math.floor(Math.random() * 100); let prizeIndex; if (rand < 5) { prizeIndex = 0; } else if (rand < 15) { prizeIndex = 1; } else if (rand < 35) { prizeIndex = 2; } else if (rand < 60) { prizeIndex = 3; } else if (rand < 85) { prizeIndex = 4; } else if (rand < 95) { prizeIndex = 5; } else { prizeIndex = 6; } // 计算旋转角度 const angle = prizeIndex * 45 + 22.5; // 开始旋转 wheel.style.transition = 'all 5s ease-out'; wheel.style.transform = `rotate(${angle}deg)`; // 指针落在中奖区域 setTimeout(() => { pointer.style.transition = 'all 0.5s ease-out'; pointer.style.transform = `translate(-50%, -50%) rotate(${angle + 45}deg)`; }, 5000); // 显示中奖结果 setTimeout(() => { resultEl.innerText = `恭喜您获得了${prizes[prizeIndex]}!`; isSpinning = false; }, 6000); }); ``` 以上代码实现一个简单的原神游戏抽奖活动页面,包括一个抽奖转盘和一个点击按钮,在点击按钮后将随机生成中奖结果并旋转转盘,最终指针落在中奖区域并显示中奖结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值