feat: 九宫格抽奖

This commit is contained in:
Burt 2024-01-10 16:57:02 +08:00
parent 7ffed1a18c
commit e63aa25902
4 changed files with 139 additions and 1 deletions

View File

@ -47,7 +47,17 @@
},
{
"path": "pages/demo/clock",
"type": "page"
"type": "page",
"style": {
"navigationBarTitleText": "动态时钟"
}
},
{
"path": "pages/demo/lottery",
"type": "page",
"style": {
"navigationBarTitleText": "九宫格抽奖"
}
},
{
"path": "pages/login/login",

View File

@ -1,3 +1,9 @@
<route lang="json5">
{
style: { navigationBarTitleText: '动态时钟' },
}
</route>
<template>
<view class="mt-4 h-10 text-center">动态时钟</view>
<view class="clock-box">

121
src/pages/demo/lottery.vue Normal file
View File

@ -0,0 +1,121 @@
<route lang="json5">
{
style: { navigationBarTitleText: '九宫格抽奖' },
}
</route>
<template>
<view class="mt-4 h-10 text-center">九宫格抽奖</view>
<view class="lottery-box">
<view class="lottery-list">
<view
class="lottery-item"
:class="{
active: n === activeIndex,
btn: n === btnIndex, //
}"
v-for="n in numList"
:key="n"
@click="handleClick(n)"
>
<view v-if="n === btnIndex">点击抽奖</view>
<view v-else> {{ n }}</view>
</view>
</view>
</view>
</template>
<script lang="ts" setup>
import { reactive, computed } from 'vue'
const numList = [1, 2, 3, 8, -1, 4, 7, 6, 5]
const btnIndex = numList[4] //
// const activeIndex = ref(0)
const state = reactive({
lottery: 0, //
step: -1, //
stopStep: 32, // 32
speed: 2, //
timer: null, // ID
loading: false,
})
// 8
const activeIndex = computed(() => {
return (state.step % 8) + 1
})
function runFn() {
//
if (state.step >= state.stopStep) {
//
clearTimeout(state.timer)
//
state.step = state.lottery
state.speed = 2
state.loading = false
console.log(`恭喜获得${state.lottery}号奖品`)
return
}
// speed
if (state.step > 24 + state.lottery) {
state.speed++
}
//
state.step++
//
state.timer = setTimeout(runFn, state.speed * 30)
}
//
function handleClick(n) {
if (n !== btnIndex) {
return
}
if (state.loading) return
state.loading = true
// 使
state.lottery = Math.floor(Math.random() * 9)
console.log(state.lottery)
// 4
state.stopStep = state.lottery + 32
//
runFn()
}
</script>
<style lang="css">
.lottery-box {
display: flex;
align-items: center;
justify-content: center;
}
.lottery-list {
--size: 100px;
display: flex;
flex-wrap: wrap;
width: calc(3 * var(--size) + 3px);
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
.lottery-item {
width: var(--size);
height: var(--size);
line-height: var(--size);
text-align: center;
border-top: 1px solid #ccc;
border-left: 1px solid #ccc;
}
.lottery-item.active {
color: #fff;
background-color: red;
}
.lottery-item.btn {
cursor: pointer;
}
</style>

1
uni-pages.d.ts vendored
View File

@ -6,6 +6,7 @@
interface NavigateToOptions {
url: "pages/index/index" |
"pages/demo/clock" |
"pages/demo/lottery" |
"pages/login/login" |
"pages/my/index" |
"pages/throughout/index" |