vue3实现双色球需求

该代码示例展示了使用Vue.js创建的一个应用,其中包括一个开始/暂停按钮,用于生成随机的红蓝球组合。红球和蓝球的数量是固定的,蓝球通过while循环确保不重复。应用还提供了添加当前组合到备选号列表的功能。
摘要由CSDN通过智能技术生成
<template>
  <div class="app">
    <button @click="start" v-show="!flag">开始</button>
    <button @click="end" v-show="flag">暂停</button>
    <ul>
      <li class="blueBall" v-for="(item, index) in allBall" :key="index">{{ item }}</li>
    </ul>
    <button @click="add">添加选号</button>
    <p v-for="item in list" :key="item"> {{ item }}</p>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";

// 开关
const flag = ref(false);

// 定时器
const timer = ref(null);

// 蓝球
const blueBall = ref([]);

// 红球
const redBall = ref([]);

// 总球
const allBall = ref([]);

// 备选号
const list = ref([]);

// 初始渲染
onMounted(() => {
  randomAllBall()
});

// 随机红蓝组合 
const randomAllBall = () => {
  redBall.value = []
  blueBall.value = []
  allBall.value = []
  // 随机红球
  redBall.value[0] = Math.floor(Math.random() * 16 + 1)

  // 随机蓝球
  let i = 0;
  while (i < 6) {
    let num = Math.floor(Math.random() * 33 + 1);
    if(blueBall.value.indexOf(num) == -1){
        blueBall.value.push(num);
        i++;
    }
  }

  blueBall.value.sort((a, b) => a - b);

  allBall.value = blueBall.value.concat(redBall.value)
} 

// 开始
const start = () => {
  flag.value = true;
  timer.value = setInterval(() => {
    randomAllBall()
  }); 
};

// 结束
const end = () => {
  flag.value = false;
  clearInterval(timer.value);
};

// 添加备选号
const add = () => {
  list.value.push(String(allBall.value))
  console.log(list.value);
}




</script>

<style>
.app {
  width: 600px;
  height: 400px;
  margin: 100px auto;
}
ul {
  list-style: none;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.blueBall {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: rgb(0, 89, 255);
  font-size: 17px;
  color: white;
  text-align: center;
  line-height: 50px;
}
li:last-child {
  background-color: rgb(225, 2, 28);
}
</style>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值