首页 > 其他分享 >vue手写转盘抽奖

vue手写转盘抽奖

时间:2024-08-08 16:54:20浏览次数:11  
标签:vue prize top key position 手写 转盘 absolute

目前抽奖最常见的两种:九宫格抽奖 和 转盘抽奖,但转盘抽奖的大多是采用的是将做好的图放在页面上,如果需要变动奖池里面的奖品或数量,就会让设计师重新出一张图片。

分享我自己在自定义转盘抽奖的样式布局思路

<template>
  <div class="main">
    <Top massage="转盘抽奖" />
    <div class="lottery">
      <div
        class="lottery-wheel"
        :style="{
          '-webkit-transition': wheelTransition,
          '-webkit-transform': rotateAngle,
        }"
      >
        <ul class="prize-list">
          <li
            class="prize-item"
            v-for="(item, index) in prizeList"
            :key="index"
          >
            <img :src="item.prize_pic" alt />
          </li>
        </ul>
      </div>
      <div class="lotterydraw">
        <img
          @click="lotterydraw"
          src="https://rzlhhr.oss-cn-beijing.aliyuncs.com/20220622/45526aa2-8c45-4edf-8582-d807f33f070c.png"
          alt="抽奖按钮"
        />
      </div>
    </div>
    <!-- 中奖结果 -->
    <div class="lotteryname" v-if="lotteryshow">恭喜你中奖{{ lotteryname }}</div>
  </div>
</template>

<script>
import Top from "../../components/Top.vue";
export default {
  components: {
    Top,
  },
  data() {
    return {
      LuckyIndex: 0, // 回馈下标
      wheelStartRotateDegree: 0, // 转盘初始化角度
      wheelTransition: "transform  5s ease-in-out", // 初始化转盘
      rotateAngle: 0, // 转盘要转的角度
      rotating: false, //转盘完成在次开始判断
      prizeList: [
        {
          prize_pic:
            "https://rzlhhr.oss-cn-beijing.aliyuncs.com/20240808/b946efd8-84a9-4132-9d9a-2e8519b83768.png",
          prize_key: "1",
        },
        {
          prize_pic:
            "https://rzlhhr.oss-cn-beijing.aliyuncs.com/20240808/0c6b4386-62c7-4220-8b13-845c5172f038.png",
          prize_key: "2",
        },
        {
          prize_pic:
            "https://rzlhhr.oss-cn-beijing.aliyuncs.com/20240808/1dcb8b1f-9fe3-4571-bf06-67f8fc686ed3.png",
          prize_key: "3",
        },
        {
          prize_pic:
            "https://rzlhhr.oss-cn-beijing.aliyuncs.com/20240808/62b883ff-f414-4181-b509-15797e8940ec.png",
          prize_key: "4",
        },
        {
          prize_pic:
            "https://rzlhhr.oss-cn-beijing.aliyuncs.com/20240808/0658113a-c36d-4c51-8cf7-b54bde999d68.png",
          prize_key: "5",
        },
        {
          prize_pic:
            "https://rzlhhr.oss-cn-beijing.aliyuncs.com/20240808/1d23e14e-2bd7-4db2-a4fe-eef27e24b102.png",
          prize_key: "6",
        },
      ], //转盘数据
      lotteryname: "", //中奖奖品名称
      lotteryshow: false,
    };
  },
  methods: {
    lotterydraw() {
      if (!this.rotating) {
        //这我设置的是15元,自己可以改参数,这里也可以调后端接口,中奖概率后端来配置,返回前端中奖结果,然后开始转盘动画
        let prize_key = "4"; //中奖标识
        this.lotteryname = "15元"; //中奖奖品名称
        // 转盘方法
        this.gameRotate(prize_key, this.prizeList);
      }
    },
    // 转盘逻辑
    gameRotate(prize_key, prizeList) {
      this.rotating = true;
      const len = prizeList.length;
      //这里循环判断转盘数据的prize_key和中奖的prize_key匹配
      for (let i = 0; i < len; i++) {
        if (prizeList[i].prize_key === prize_key) {
          this.LuckyIndex = i;
        }
      }
      const result = [360, 300, 240, 180, 120, 60];
      let FIXED_TIMES = 5;
      const addDegree = (FIXED_TIMES + 1) * 360;
      let rotateAngleNum =
        this.wheelStartRotateDegree +
        addDegree +
        result[this.LuckyIndex] -
        (this.wheelStartRotateDegree % 360);
      this.wheelStartRotateDegree = rotateAngleNum; // 转盘初始化角度
      this.rotateAngle = `rotate(${rotateAngleNum}deg)`; //转盘要转的角度
      setTimeout(() => {
        this.gameover();
      }, FIXED_TIMES * 1000 + 600); // 延时,保证转盘转完
    },
    // 转盘转完可再次转
    gameover() {
      this.rotating = false;
    //   中奖奖品
      this.lotteryshow = true;
    },
  },
  created() {},
  mounted() {},
};
</script>

<style lang='scss' scoped>
//转盘
.lottery {
  width: 100%;
  position: absolute;
  top: 15vw;
  z-index: 1;
}
.lottery-wheel {
  width: 95vw;
  height: 95vw;
  margin: auto;
  background: url(https://rzlhhr.oss-cn-beijing.aliyuncs.com/20220622/b6427910-e086-4df1-9537-8700108e2690.png)
    0 0 no-repeat;
  background-size: 95vw 95vw;
  .prize-list {
    position: relative;
    width: 100%;
    height: 100%;
    .prize-item {
      display: flex;
      align-items: center;
      flex-direction: column;
      img {
        width: 15vw;
        height: 15vw;
      }
    }
    // 奖品样式
    .prize-item:nth-child(1) {
      position: absolute;
      top: 18%;
      left: 42%;
      transform: rotate(0);
    }
    .prize-item:nth-child(2) {
      position: absolute;
      top: 31%;
      left: 62%;
      transform: rotate(60deg);
    }
    .prize-item:nth-child(3) {
      position: absolute;
      top: 55%;
      left: 63%;
      transform: rotate(120deg);
    }
    .prize-item:nth-child(4) {
      position: absolute;
      top: 64%;
      left: 42%;
      transform: rotate(180deg);
    }
    .prize-item:nth-child(5) {
      position: absolute;
      top: 55%;
      left: 21%;
      transform: rotate(240deg);
    }
    .prize-item:nth-child(6) {
      position: absolute;
      top: 31%;
      left: 22%;
      transform: rotate(300deg);
    }
  }
}
.lotterydraw {
  position: absolute;
  left: 50%;
  top: 48%;
  margin-top: -10vw;
  margin-left: -10vw;
  z-index: 5;
  img {
    width: 20vw;
  }
}
.lotteryname{
    position: absolute;
    top: 120vw;
    width: 100%;
    text-align: center;
}
</style>

整个转盘抽奖布局分为两部分:底部转盘背景图 和 奖品内容布局

转盘效果

 

<iframe allowfullscreen="true" data-mediaembed="csdn" frameborder="0" id="qa2joCnx-1723106976009" src="https://live.csdn.net/v/embed/416012"></iframe>

转盘效果

标签:vue,prize,top,key,position,手写,转盘,absolute
From: https://blog.csdn.net/qq_47111583/article/details/141030381

相关文章

  • 一个基于 vue 的强大表单和高性能表格组件,简洁API设计,支持虚拟树,列拖拽,懒加载,快捷
    前言在现代Web应用开发中,表单和表格是两个核心组件,它们对于数据展示和用户交互至关重要。然而,现有的解-决方案往往存在一些痛点,如不够灵活、性能问题、以及难以实现复杂功能等。这些问题限制了开发者的创造力,也影响了用户体验。为了解决这些痛点,开发者需要一款功能强大、灵活......
  • Vue2和Vue3中的生命周期钩子图示
    原up写的非常简单易懂,还有简单代码示例:reference:https://cloud.tencent.com/developer/article/1514771一、Vue2和Vue3中的生命周期附原图:......
  • vue中axios二次封装【简洁、附代码】+api解耦
    reference:https://www.bilibili.com/video/BV1my421h7hK/?share_source=copy_web&vd_source=334dbcc5ec1e90276a3fca594c89e11e下一篇:继axios二次封装后跨域问题解决——配置代理、环境变量文章目录一、axios请求接口1下载2引入3使用二、axios二次封装1.下......
  • 利用vscode-icons-js在Vue3项目中实现文件图标展示
    背景:在开发文件管理系统或类似的项目时,我们常常需要根据文件类型展示对应的文件图标,这样可以提高用户体验。本文将介绍如何在Vue3项目中利用vscode-icons-js库,实现类似VSCode的文件图标展示效果。先看效果:一、引入vscode-icons-js首先,我们需要安装vscode-icons-js库。......
  • vue 项目使用@vue-office/docx word 纯前端v 也支持后端接口方式
    只是做个记录,防止忘记。安装依赖 @vue-office/docxvue2的写法vue3同理自己改造。记得一定放在public文件夹下 下面代码<template> <divstyle="height:100%">  <el-buttontype="primary"@click="downWord">下载文档</el-button>  <vue......
  • 【uniapp】uniapp+vue2微信小程序实现分享功能
    uniapp+vue2做的微信小程序实现分享功能问题描述uniapp+vue2做的微信小程序,发布以后点击右上角三个点,分享小程序的时候,转发和分享按钮都是灰色解决方案转发、分享、复制链接这几个功能需要自己来手动写方法,考虑到每个页面都需要能够实现分享功能,以下我会使用Vue.js的......
  • 基于SpringBoot+MySQL+SSM+Vue.js的校园二手图书交易管理系统(附论文)
    获取见最下方名片信息获取见最下方名片信息获取见最下方名片信息演示视频基于SpringBoot+MySQL+SSM+Vue.js的校园二手图书交易管理系统(附论文)技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+M......
  • 基于SpringBoot+Vue+MySQL的在线网盘系统
    获取见最下方名片信息获取见最下方名片信息获取见最下方名片信息演示视频基于SpringBoot+Vue+MySQL的在线网盘系统技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+SpringBoot文字描述......
  • 从Vue到Element
    Vue-ElementAjax原生AjaxAxios案例Vue项目启动配置端口  开发流程Element快速入门组件表格分页对话框表单案例Vue路由打包部署Ajax原生Ajax1.创建XMLHttpRequestvarxmlHttpRequest=newXMLHttpRequest();2.发送异步请求xml......
  • springboot+vue农产品商城【程序+论文+开题】-计算机毕业设计
    系统程序文件列表开题报告内容研究背景随着信息技术的飞速发展和互联网普及率的不断提高,电子商务已成为推动全球经济的重要力量。在农业领域,传统农产品销售模式面临着信息不对称、流通环节多、成本高、效率低下等问题,严重制约了农产品的市场竞争力与农民增收。因此,构建一个......