欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue簡(jiǎn)單實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)

 更新時(shí)間:2022年03月09日 10:02:01   作者:海龜先生plus  
這篇文章主要為大家詳細(xì)介紹了vue簡(jiǎn)單實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue簡(jiǎn)單實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)的具體代碼,供大家參考,具體內(nèi)容如下

1.0 思路整理

轉(zhuǎn)盤抽獎(jiǎng)很常見,之前也沒(méi)寫過(guò),現(xiàn)在有空來(lái)寫寫,分析如下:

1.1 轉(zhuǎn)盤旋轉(zhuǎn) ----- 可以用 transform 的 rotate 來(lái)解決
1.2 旋轉(zhuǎn)動(dòng)畫 ----- transition 過(guò)渡來(lái)處理
1.3 停留目標(biāo)位置及中獎(jiǎng)提示 ? ------ 通過(guò)控制旋轉(zhuǎn)角度控制停留位置,中獎(jiǎng)提示,考慮添加回調(diào)

1.1 開始行動(dòng)

上面的思考,我們知道了大概要實(shí)現(xiàn)的步驟,首先我們搞張圖片

這個(gè)圓盤有 10 份,每一份 360/10 = 36deg,假設(shè)要停留在第二個(gè)---->20金幣,順時(shí)針(含初始位置并計(jì)為1),即 共需要旋轉(zhuǎn) (2 - 1)* 36 = 36,這樣,我們可以得出,停留位置需要旋轉(zhuǎn)的角度 = (target - 1)*36。

1.2 中獎(jiǎng)回調(diào)

上面的步驟,我們知道了如何控制到目標(biāo)位置,那接下來(lái)就是事件通知,起初想的是,固定轉(zhuǎn)動(dòng)時(shí)間,然后開啟定時(shí)器計(jì)時(shí),很顯然不靠譜,有沒(méi)有什么可以在動(dòng)畫結(jié)束后就通知呢?transitionend,我找到了這個(gè)事件,可以監(jiān)聽元素動(dòng)畫結(jié)束事件,只不過(guò)有些兼容 這個(gè)好辦

/**
動(dòng)畫結(jié)束事件兼容
**/
whichTransitionEvent(){
 let el = document.createElement('span'),
 transitions = {
 'transition':'transitionend',
 'OTransition':'oTransitionEnd',
 'MozTransition':'transitionend',
 'WebkitTransition':'webkitTransitionEnd'
 };
 for(let t in transitions){
 if( el.style[t] !== undefined ){
  return transitions[t];
 }
 }
 el = null;
}

2.0 完整示例

控制轉(zhuǎn)動(dòng)位置和事件通知都找到方法了,接下來(lái)開干!

栗子:

完整代碼

<template>
 <div>
 <h3>轉(zhuǎn)盤抽獎(jiǎng)</h3>
 <div class="round_box" >
  <img class="img_rotate" ref="rotImg" src="../assets/zhuan.png" alt="">
  <div class="center">
  <div class="pointer" ></div>
  </div>
 </div>
 <button @click="toDraw" >點(diǎn)擊抽獎(jiǎng)</button>
 </div>
</template>

<script>
export default {
 name:'rotaryDraw',
 data() {
 return {
  rotate: 0,
  resetRotate: 0,
  hitId: 1,// 1-10
  drawStatus: false
 }
 },
 async mounted() {
 await this.$nextTick();
 let evenTransition = this.whichTransitionEvent();
 let img = this.$refs.rotImg;
 let that = this;
 const hitAre = [ '30M流量包','20金幣','20M流量包','10M流量包','5金幣',
    '謝謝參與','10金幣','50M流量包','2金幣','100M流量包'
   ];
 // 監(jiān)聽 動(dòng)畫結(jié)束 
 img.addEventListener(evenTransition,tranHand,false);

 function tranHand() {
  // 復(fù)位
  that.resetRotate = that.rotate > 360 ? that.rotate % 360 : 0;
  img.style.transition = "none 0s ease 0s";
  img.style.transform = `rotate(${that.resetRotate}deg)`; 
  alert(`抽獎(jiǎng)結(jié)果【 ${hitAre[that.hitId - 1]} 】`);
  that.drawStatus = false
 }
 },
 methods: {
 start() {
  this.$refs.rotImg.style.transition = "all 3s ease 0s";
  this.$refs.rotImg.style.transform = `rotate(${this.rotate}deg)`;
 },
 toDraw() {
  if(this.drawStatus){
  console.log('正在抽獎(jiǎng)中');
  return
  }
  // 標(biāo)記狀態(tài)
  this.drawStatus = true
  /**
  * 圓盤共 10 份 每份 36度, 停位置(id)度數(shù) (id - 1)*36 
  * 基數(shù) 3圈 360*4
  * this.rotate 當(dāng)前角度
  * **/ 
  let reset = 360 * 4;
  let idx = this.getRandomInt(1,11);
  // 設(shè)置命中
  this.hitId = idx;
  // 需要多轉(zhuǎn)角度
  let addRotate = this.resetRotate > 0 ? 360 - this.resetRotate : 0;
  // 總共角度
  let allRotate = this.rotate + (idx - 1) * 36 + reset + addRotate;
  // 角度限制
  this.rotate = this.setRotate(allRotate);

  this.start()
 },
 // 遞歸計(jì)算角度 不超過(guò) 360*6
 setRotate(deg) {
  let rest = deg - 360;
  return rest > 360*6 ? this.setRotate(rest) : deg;
 },
 getRandomInt(min, max) {
  // 向上收
  min = Math.ceil(min);
  // 向下收
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //不含最大值,含最小值
 },
 // 動(dòng)畫兼容
 whichTransitionEvent(){
  let el = document.createElement('span'),
  transitions = {
  'transition':'transitionend',
  'OTransition':'oTransitionEnd',
  'MozTransition':'transitionend',
  'WebkitTransition':'webkitTransitionEnd'
  };
  for(let t in transitions){
  if( el.style[t] !== undefined ){
   return transitions[t];
  }
  }
  el = null;
 }
 }

}
</script>

<style lang="scss" >
.img_rotate{
 transform: rotate(0deg);
}
.round_box{
 width: 100%;
 max-width: 375px;
 position: relative;
 overflow: hidden;
 img{
 width: 100%;
 }
 .center{
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%,-50%);
 .pointer{
  width: 5px;
  height: 90px;
  background-color: #f40;
  position: absolute;
  top: -90px;
 }
 .pointer::before{
  content:'';
  width: 0;
  height: 0;
  border-top: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid #f40;
  border-left: 15px solid transparent;
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
 }
 }
}

</style>

3.0 tips

總體來(lái)說(shuō)有幾個(gè)點(diǎn)需要注意

1、動(dòng)畫開始前上鎖

2、動(dòng)畫結(jié)束后通知,狀態(tài)復(fù)位

/**
比如:
基數(shù)3圈 reset 360*3
停留位置 第二個(gè) (2 - 1)* 36 = 36
總共角度 360*3 + 36
動(dòng)畫停止后,因?yàn)檫€要繼續(xù)旋轉(zhuǎn),所以不可能把角度一直增加,因此需要復(fù)位 
360*3 + 36 其實(shí)可以考慮 就轉(zhuǎn)了 36度,然后再增加需要轉(zhuǎn)的角度
**/

3、繼續(xù)旋轉(zhuǎn),因?yàn)槲覀冇?jì)算是以 30M流量 為初始值的,所以在此旋轉(zhuǎn) 仍然需要以 30M為起點(diǎn),此時(shí)假設(shè) 現(xiàn)在停留位置是 300度,也就是說(shuō) 再轉(zhuǎn) 60度,也就回到了初始位置,本人也是按照這個(gè)思路進(jìn)行復(fù)位的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue?路由跳轉(zhuǎn)打開新窗口被瀏覽器攔截問(wèn)題處理

    vue?路由跳轉(zhuǎn)打開新窗口被瀏覽器攔截問(wèn)題處理

    這篇文章主要介紹了vue?路由跳轉(zhuǎn)打開新窗口被瀏覽器攔截問(wèn)題處理,下面文章操作中所遇到相關(guān)問(wèn)題解決的內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下
    2022-03-03
  • Vue3中element-plus全局使用Icon圖標(biāo)的過(guò)程詳解

    Vue3中element-plus全局使用Icon圖標(biāo)的過(guò)程詳解

    我們?cè)谟胿ue開發(fā)網(wǎng)站的時(shí)候,往往圖標(biāo)是起著很重要的作,這篇文章主要給大家介紹了關(guān)于Vue3中element-plus全局使用Icon圖標(biāo)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • Vue中組件之間數(shù)據(jù)的傳遞的示例代碼

    Vue中組件之間數(shù)據(jù)的傳遞的示例代碼

    本篇文章主要介紹了Vue中組件數(shù)據(jù)的傳遞的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • Vue精簡(jiǎn)版風(fēng)格概述

    Vue精簡(jiǎn)版風(fēng)格概述

    本篇文章給大家講解了一下Vue精簡(jiǎn)版風(fēng)格的相關(guān)知識(shí)點(diǎn)內(nèi)容以及分享了實(shí)例代碼,有興趣的朋友參考下。
    2018-01-01
  • Vue實(shí)現(xiàn)輪播圖組件的封裝

    Vue實(shí)現(xiàn)輪播圖組件的封裝

    Vue輪播圖組件的封裝可通過(guò)封裝組件、使用插件、配置化等方式實(shí)現(xiàn),主要包括圖片預(yù)加載、定時(shí)輪播、無(wú)限滾動(dòng)、手勢(shì)滑動(dòng)、響應(yīng)式布局等功能,實(shí)現(xiàn)方式可使用Vue的生命周期函數(shù)、自定義事件、計(jì)算屬性等技術(shù)
    2023-04-04
  • VueUse使用及造輪子選擇對(duì)比示例詳解

    VueUse使用及造輪子選擇對(duì)比示例詳解

    這篇文章主要為大家介紹了VueUse使用及造輪子選擇對(duì)比示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • vue中封裝axios并實(shí)現(xiàn)api接口的統(tǒng)一管理

    vue中封裝axios并實(shí)現(xiàn)api接口的統(tǒng)一管理

    這篇文章主要介紹了vue中封裝axios并實(shí)現(xiàn)api接口的統(tǒng)一管理的方法,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果

    vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果

    這篇文章主要介紹了vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue動(dòng)態(tài)設(shè)置頁(yè)面title的方法實(shí)例

    vue動(dòng)態(tài)設(shè)置頁(yè)面title的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于vue動(dòng)態(tài)設(shè)置頁(yè)面title的相關(guān)資料,文中通過(guò)實(shí)例代碼結(jié)束的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • vue中eventbus被多次觸發(fā)以及踩過(guò)的坑

    vue中eventbus被多次觸發(fā)以及踩過(guò)的坑

    這篇文章主要介紹了vue中eventbus被多次觸發(fā)以及踩過(guò)的坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論