vue年会大转盘抽奖活动代码

2024年06月15日 建站教程

Vue移动端抽奖组件,用于大转盘滚动抽奖场景,可配置奖品、图片、转盘样式等。下面web建站小编给大家简单介绍一下具体实现代码!

vue组件安装

import { createApp } from "vue";
// vue
import { Turntable } from "@nutui/nutui-bingo";
// taro
import { Turntable } from "@nutui/nutui-bingo-taro";

const app = createApp();
app.use(Turntable);

vue基础用法

<template>
  <nutbig-turntable
    class="turntable"
    ref="turntable"
    :width="luckWidth"
    :height="luckheight"
    :prize-list="prizeList"
    :turns-number="turnsNumber"
    :turns-time="turnsTime"
    :prize-index="prizeIndex"
    :style-opt="styleOpt"
    :pointer-
    @start-turns="startTurns"
    @end-turns="endTurns"
  >
  </nutbig-turntable>
</template>
<script>
  import { Toast } from "@nutui/nutui";
  import { ref, reactive } from "vue";
  export default {
    setup() {
      const turntable = ref(null);
      // 转盘大小
      const luckWidth = ref("450px");
      const luckheight = ref("450px");
      // 转盘指针图片样式
      const pointerStyle = {
        width: "80px",
        height: "80px",
        backgroundImage:'url("背景图片")',
        backgroundSize: "contain",
        backgroundRepeat: "no-repeat",
      };
      // 转盘上要展示的奖品数据
      const prizeList = ref([
        {
          id: "xiaomi",
          prizeName: "小米手机",
          prizeImg: "小米手机图片.jpg",
        },
        {
          id: "huawei",
          prizeColor: "rgb(251, 219, 216)",
          prizeName: "华为手机",
          prizeImg: "华为手机图片.jpg",
        },
        {
          id: "apple",
          prizeName: "apple watch",
          prizeImg: "apple watch.jpg",
        },
        {
          id: "shubiao",
          prizeColor: "rgba(246, 142, 46, 0.5)",
          prizeName: "鼠标",
          prizeImg: "鼠标.jpg",
        },
        {
          id: "jianpan",
          prizeName: "键盘.jpg",
          prizeImg:
            "",
        },
        {
          id: "thanks",
          prizeName: "谢谢参与",
          prizeImg: "谢谢参与.jpg",
        },
      ]);
      // 转动圈数
      const turnsNumber = ref(5);
      // 转动需要持续的时间(秒)
      const turnsTime = ref(5);
      // 转盘样式的选项
      const styleOpt = reactive({
        // 转盘中每一块扇形的背景色,根据奖品的index来取每一块的对应颜色
        prizeBgColors: [
          "rgb(255, 231, 149)",
          "rgb(255, 247, 223)",
          "rgb(255, 231, 149)",
          "rgb(255, 247, 223)",
          "rgb(255, 231, 149)",
          "rgb(255, 247, 223)",
        ],
        // 每一个扇形的外边框颜色
        borderColor: "#ff9800",
      });
      // 中奖的奖品的index(此数据可根据后台返回的值重新赋值)
      const prizeIndex = ref(-1);
      // 剩余抽奖次数
      const num = ref(5);
      const startTurns = () => {
        const index = Math.floor(Math.random() * prizeList.value.length);
        prizeIndex.value = index;
        turntable.value.rotateTurn();
      };
      const endTurns = () => {
        console.log("中奖了");
      };
      return {
        turntable,
        luckWidth,
        luckheight,
        pointerStyle,
        prizeList,
        turnsNumber,
        turnsTime,
        styleOpt,
        prizeIndex,
        num,
        startTurns,
        endTurns,
      };
    },
  };
</script>

本文链接:http://so.lmcjl.com/news/6592/

展开阅读全文
相关内容