有两组奖品,每组奖品都有对应的奖品展示,两组奖品的数据是统一的,点击两组奖品下面的按钮不管哪一个,选到了其中的一个奖品,就要把这个奖品移出去,另一组的奖品也要对应移出去,并且每次抽奖还要展示抽到的内容

本文介绍了一个使用Vue.js编写的动态抽奖应用,用户可以点击按钮从奖品列表中随机选择一个奖品,当所有奖品被选完时,会显示已无奖品。
摘要由CSDN通过智能技术生成
<template>
  <div>
    <div class="lottery-info">{{ lotteryResult }}</div>
    <div class="lottery-wrapper">
      <div class="lottery" v-bind:class="{ disabled: noRemainingPrize(0) }">
        <div class="lottery-item" v-for="(prize, index) in prizes" :key="index" v-if="!prize.selected">
          {{ prize.name }}: {{ prize.percentage }}%
        </div>
        <div class="lottery-btn" v-on:click="onLotteryClick(0)" v-bind:disabled="noRemainingPrize(0)">
          {{ noRemainingPrize(0) ? '已无奖品' : '抽奖' }}
        </div>
      </div>
      <div class="lottery" v-bind:class="{ disabled: noRemainingPrize(1) }">
        <div class="lottery-item" v-for="(prize, index) in prizes" :key="index" v-if="!prize.selected">
          {{ prize.name }}: {{ prize.percentage }}%
        </div>
        <div class="lottery-btn" v-on:click="onLotteryClick(1)" v-bind:disabled="noRemainingPrize(1)">
          {{ noRemainingPrize(1) ? '已无奖品' : '抽奖' }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      prizes: [
        { name: '奖品1', percentage: 20, selected: false },
        { name: '奖品2', percentage: 30, selected: false },
        { name: '奖品3', percentage: 25, selected: false },
        { name: '奖品4', percentage: 15, selected: false },
        { name: '奖品5', percentage: 10, selected: false }
      ],
      lotteryResult: ''
    };
  },
  methods: {
    onLotteryClick(index) {
      const prizes = this.prizes.filter(prize => !prize.selected);
      const length = prizes.length;
      if (length === 0) {
        return;
      }
      const selected = Math.floor(Math.random() * length);
      prizes[selected].selected = true;
      this.lotteryResult = `恭喜您获得了 ${prizes[selected].name}!`;
    },
    noRemainingPrize(index) {
      const remainingPrizes = this.prizes.filter(prize => !prize.selected);
      return remainingPrizes.length === 0;
    }
  }
};
</script>

<style>
.lottery-wrapper {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.lottery {
  width: 200px;
  background-color: #f5f5f5;
  padding: 10px;
}

.lottery-item {
  margin-bottom: 5px;
}

.lottery-btn {
  background-color: #2196f3;
  color: #fff;
  text-align: center;
  padding: 10px;
  cursor: pointer;
}

.lottery-info {
  margin-top: 20px;
}

.disabled {
  opacity: 0.6;
  cursor: default;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值