1.0.2 • Published 1 month ago

@tanzhenxing/zx-turntable v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

zx-turntable 转盘抽奖

一个可配置的转盘抽奖组件,支持 H5、小程序和 App。

使用方法

<template>
  <view>
    <zx-turntable
      ref="turntable"
      :rewardNames="rewards"
      :rewardBGColors="bgColors"
      :rewardColors="textColors"
      :turns="5"
      :duration="6000"
      @end="onLotteryEnd"
    />
    <button @click="startLottery">开始抽奖</button>
  </view>
</template>

<script setup>
import { ref } from 'vue';

const turntable = ref(null);
const rewards = ref(['奖品1', '奖品2', '奖品3', '奖品4', '奖品5', '奖品6']);
const bgColors = ref(['#FFDF88', '#FFF1C4', '#FFDF88', '#FFF1C4', '#FFDF88', '#FFF1C4']);
const textColors = ref(['#C70000', '#8C0000', '#C70000', '#8C0000', '#C70000', '#8C0000']);

const startLottery = () => {
  if (turntable.value && !turntable.value.isSpinning) {
    const randomIndex = Math.floor(Math.random() * rewards.value.length);
    turntable.value.spinTo(randomIndex);
  }
};

const onLotteryEnd = (index) => {
  console.log('抽奖结束,中奖索引:', index, '奖品为:', rewards.value[index]);
  uni.showToast({
    title: `恭喜你抽中:${rewards.value[index]}`,
    icon: 'none'
  });
};
</script>

API

Props

属性名类型默认值说明
rewardNamesArray['', '', '', '', '', '']奖品名称数组
rewardBGColorsArray[] (内部有默认颜色)奖品扇区背景颜色数组,数量不足时循环使用默认颜色
rewardColorsArray[] (内部有默认颜色)奖品文字颜色数组,数量不足时循环使用默认颜色
fontSizeString'32rpx'奖品文字大小
turnsNumber6转盘转动圈数
durationNumber5000转盘转动时间 (毫秒)

Events

事件名回调参数说明
endindex抽奖结束时触发,返回中奖奖品的索引

Methods

通过 ref 获取组件实例后,可以调用以下方法:

方法名参数说明
init-初始化转盘(通常在 onMounted 后自动调用)
spinTotargetIndex转动到指定的奖品索引
isSpinning- (getter)返回一个布尔值,表示转盘当前是否正在转动

注意事项

  • 奖品数量至少为2个,建议为偶数个,以获得更好的视觉效果。
  • rewardBGColorsrewardColors 数组的长度如果小于 rewardNames 的长度,组件会使用内置的默认颜色进行补充。
  • 确保在调用 spinTo 方法前,转盘已经初始化完成(通常是自动的)。
  • spinTo 方法的 targetIndex 参数必须是 rewardNames 数组中的有效索引。
1.0.2

1 month ago

1.0.1

2 months ago

1.0.0

11 months ago