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

Vue實(shí)現(xiàn)紅包雨小游戲的示例代碼

 更新時(shí)間:2022年01月26日 09:58:18   作者:FrigidWinter  
紅包也叫壓歲錢,是過農(nóng)歷春節(jié)時(shí)長輩給小孩兒用紅紙包裹的禮金。本文將通過Vue制作一個紅包雨小游戲,文中的示例代碼講解詳細(xì),需要的可以參考一下

0 寫在前面

紅包也叫壓歲錢,是過農(nóng)歷春節(jié)時(shí)長輩給小孩兒用紅紙包裹的禮金。據(jù)傳明清時(shí)期,壓歲錢大多數(shù)是用紅繩串著賜給孩子。民國以后,則演變?yōu)橛眉t紙包裹。中國的紅包傳統(tǒng)民族文化在民間如此,社區(qū)、公司也奉行如儀。除了春節(jié)以外,在其他喜慶場合,例如婚禮、新店開張等亦有送紅包的習(xí)俗。

本期迎新春專題用代碼制作一個 紅包雨小游戲 ,效果如下所示。看完本文相信你也可以完成這樣一個小游戲設(shè)計(jì)。

1 準(zhǔn)備工作

使用 Vue 構(gòu)建工程。流程為

vue init webpack vue-demo

cd vue-demo

cnpm install # npm install

從網(wǎng)絡(luò)上下載一些喜慶的圖片作為背景和紅包樣式,這些樣式可以任選,給想整活的同學(xué)們充足的自由度。

2 設(shè)計(jì)HTML+CSS樣式

html樣式很簡單,主要分為兩個部分:紅包雨 和 搶紅包面板。

<!-- 紅包雨 -->
<div id="wrapper"></div>

<!-- 搶紅包面板 -->
<div id="panel">
?? ?<div id="hb">
? ? ?? ?<span id="text">{{ result }}</span>
? ? ? ? <div id="btn" @click="gameOn">繼續(xù)搶紅包</div>
? ? </div>
</div>

CSS樣式稍微復(fù)雜一些,放在下文完整代碼中,需要的自取。其中比較少用的是annimation動畫渲染樣式

animation: dropDowm 3s forwards; /* 旋轉(zhuǎn)動畫 */
@keyframes dropDowm {
  0% {
    top: 0px;
    transform: translateY(-100%) rotate(0deg);
  }
  100% {
    top: 110%;
    transform: translateY(0%) rotate(360deg);
  }
}

這里講解一下,annimation常見參數(shù)如下:

animation-name:關(guān)鍵幀動畫名稱

animation-duration:動畫執(zhí)行時(shí)間

animation-timing-function: 動畫的執(zhí)行速度函數(shù)

animation-delay: 動畫延遲時(shí)間

animation-iteration-count:動畫執(zhí)行次數(shù)

animation-direction: 動畫執(zhí)行方向,包含alternate(間隔運(yùn)動)、 reverse(反向運(yùn)動)、reverse-alternate(反向間隔運(yùn)動)

animation-fill-mode:規(guī)定當(dāng)動畫不播放時(shí)(當(dāng)動畫完成時(shí),或當(dāng)動畫有一個延遲未開始播放時(shí)),要應(yīng)用到元素的樣式,包含forwards(動畫停止在最后一個關(guān)鍵幀的位置)、backwards(動畫第一個關(guān)鍵幀立即執(zhí)行)、both(第一個關(guān)鍵幀也停止在最后一個關(guān)鍵幀)

設(shè)計(jì)完成后運(yùn)行結(jié)果如下圖所示,分別為背景和面板。

3 設(shè)計(jì)JavaScript邏輯

程序的邏輯如下所示

上述最關(guān)鍵的就是監(jiān)聽用戶搶紅包的行為,并判斷是否搶到了紅包,監(jiān)聽函數(shù)設(shè)計(jì)如下所示,如果成功搶到紅包,則總金額自動累加。

mouseHandler(e) {
      var event = e || window.event,
        money = event.target.dataset.money;
      if (money) {
        this.result = "恭喜搶到紅包" + money + "元";
        for (var i = 0, len = this.imgList.length; i < len; i++) {
          this.imgList[i].style.animationPlayState = "paused";
        }
        panel.style.display = "block";
        this.totalMoney += Number(money);
      }
    }

接下來要考慮如何讓紅包隨機(jī)掉落,核心代碼如下:

 for (var i = 0; i < num; i++) {
        let img = new Image();
        img.src = this.imgUrl;
        // 隨機(jī)設(shè)置紅包分布
        img.style.left = this.ranNum(0, window.innerWidth) + "px";
        let delay = this.ranNum(0, 100) / 10;
        // 設(shè)置紅包出現(xiàn)時(shí)間
        img.style.animationDelay = delay + "s";
        if (this.delayTime < delay) {
          this.delayTime = delay;
          this.lastImg = img;
        }
        //設(shè)置每個紅包的金額
        img.dataset.money = this.ranNum(0, 1000) / 100;

其他函數(shù)基本都是服務(wù)于這兩個核心功能的,這里不贅述。

4 完整代碼

<template>
  <div id="app">
    <!-- 紅包雨 -->
    <div id="wrapper"></div>
    <!-- 搶紅包面板 -->
    <div id="panel">
      <div id="hb">
        <span id="text">{{ result }}</span>
        <div id="btn" @click="gameOn">繼續(xù)搶紅包</div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
? name: "App",
? data() {
? ? return {
? ? ? totalMoney: 0, // 所有搶到紅包的總金額
? ? ? delayTime: 0, // 延時(shí)
? ? ? lastImg: null, // 最后一張掉落的圖片
? ? ? imgList: null, // 紅包隨機(jī)序列
? ? ? result: "", // 游戲結(jié)果
? ? ? imgUrl: require("./assets/hongbao.jpg"),
? ? };
? },
? methods: {
? ? // @breif:開始游戲
? ? start() {
? ? ? let dom = this.createDom(20);
? ? ? this.imgList = document.getElementsByTagName("img");
? ? ? document.getElementById("wrapper").appendChild(dom);
? ? },
? ? // @breif: 創(chuàng)建紅包序列
? ? createDom(num) {
? ? ? // 創(chuàng)建文檔碎片
? ? ? let frag = document.createDocumentFragment();
? ? ? for (var i = 0; i < num; i++) {
? ? ? ? let img = new Image();
? ? ? ? img.src = this.imgUrl;
? ? ? ? // 隨機(jī)設(shè)置紅包分布
? ? ? ? img.style.left = this.ranNum(0, window.innerWidth) + "px";
? ? ? ? let delay = this.ranNum(0, 100) / 10;
? ? ? ? // 設(shè)置紅包出現(xiàn)時(shí)間
? ? ? ? img.style.animationDelay = delay + "s";
? ? ? ? if (this.delayTime < delay) {
? ? ? ? ? this.delayTime = delay;
? ? ? ? ? this.lastImg = img;
? ? ? ? }
? ? ? ? //設(shè)置每個紅包的金額
? ? ? ? img.dataset.money = this.ranNum(0, 1000) / 100;
? ? ? ? frag.appendChild(img);
? ? ? }
? ? ? return frag;
? ? },
? ? // @breif:繼續(xù)游戲
? ? gameOn() {
? ? ? document.getElementById("panel").style.display = "none";
? ? ? for (let i = 0, len = this.imgList.length; i < len; i++) {
? ? ? ? this.imgList[i].style.animationPlayState = "running";
? ? ? }
? ? },
? ? // 監(jiān)聽鼠標(biāo)事件
? ? mouseHandler(e) {
? ? ? var event = e || window.event,
? ? ? ? money = event.target.dataset.money;
? ? ? if (money) {
? ? ? ? this.result = "恭喜搶到紅包" + money + "元";
? ? ? ? for (var i = 0, len = this.imgList.length; i < len; i++) {
? ? ? ? ? this.imgList[i].style.animationPlayState = "paused";
? ? ? ? }
? ? ? ? panel.style.display = "block";
? ? ? ? this.totalMoney += Number(money);
? ? ? }
? ? },
? ? // 監(jiān)聽動畫事件
? ? annimationHandler(e) {
? ? ? document.getElementById("panel").style.display = "block";
? ? ? this.result = "恭喜總共搶到了" + this.totalMoney.toFixed(2) + "元";
? ? },? ? // @breif:產(chǎn)生min~max間的隨機(jī)數(shù)
? ? ranNum(min, max) {
? ? ? return Math.ceil(Math.random() * (max - min) + min);
? ? },
? },
? mounted() {
? ? this.start();
? ? window.addEventListener("mousedown", this.mouseHandler);
? ? this.lastImg.addEventListener("webkitAnimationEnd", this.annimationHandler);
? },
};
</script>
<style>
* {
? padding: 0;
? margin: 0;
}

body {
? height: 100%;
? width: 100%;
? background: url("./assets/background.jpg");
? background-size: cover;
? overflow: hidden;
}

#wrapper img {
? position: absolute;
? transform: translateY(-100%); /* 下落動畫 */
? animation: dropDowm 3s forwards; /* 旋轉(zhuǎn)動畫 */
}
@keyframes dropDowm {
? 0% {
? ? top: 0px;
? ? transform: translateY(-100%) rotate(0deg);
? }
? 100% {
? ? top: 110%;
? ? transform: translateY(0%) rotate(360deg);
? }
}

#panel {
? display: none;
}

#panel::before {
? content: "";
? position: fixed;
? top: 0;
? left: 0;
? right: 0;
? bottom: 0;
? background-color: rgba(0, 0, 0, 0.5);
}

#hb {
? width: 350px;
? height: 450px;
? border-radius: 20px;
? background-color: #e7223e;
? color: #fad755;
? position: fixed;
? left: 50%;
? top: 50%;
? margin-top: -225px;
? margin-left: -175px;
? font-size: 30px;
? font-weight: 900;
? display: flex;
? flex-direction: column;
? justify-content: center;
? align-items: center;
}
#btn {
? background-color: #fad755;
? color: #e7223e;
? font-size: 18px;
? margin-top: 10px;
? padding: 10px;
? border: none;
? outline: none;
? cursor: pointer;
}
</style>

以上就是Vue實(shí)現(xiàn)紅包雨小游戲的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Vue紅包雨游戲的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • axios 封裝上傳文件的請求方法

    axios 封裝上傳文件的請求方法

    今天小編就為大家分享一篇axios 封裝上傳文件的請求方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vite結(jié)合whistle實(shí)現(xiàn)一勞永逸開發(fā)環(huán)境代理方案

    Vite結(jié)合whistle實(shí)現(xiàn)一勞永逸開發(fā)環(huán)境代理方案

    這篇文章主要為大家介紹了Vite結(jié)合whistle實(shí)現(xiàn)一勞永逸開發(fā)環(huán)境代理方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 解決vue打包報(bào)錯Unexpected token: punc的問題

    解決vue打包報(bào)錯Unexpected token: punc的問題

    這篇文章主要介紹了解決vue打包報(bào)錯Unexpected token: punc的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue實(shí)現(xiàn)彈框遮罩點(diǎn)擊其他區(qū)域彈框關(guān)閉及v-if與v-show的區(qū)別介紹

    vue實(shí)現(xiàn)彈框遮罩點(diǎn)擊其他區(qū)域彈框關(guān)閉及v-if與v-show的區(qū)別介紹

    vue如何簡單的實(shí)現(xiàn)彈框,遮罩,點(diǎn)擊其他區(qū)域關(guān)閉彈框, 簡單的思路是以一個div作為遮罩,這篇文章給大家詳細(xì)介紹了vue實(shí)現(xiàn)彈框遮罩點(diǎn)擊其他區(qū)域彈框關(guān)閉及v-if與v-show的區(qū)別介紹,感興趣的朋友一起看看吧
    2018-09-09
  • vue二級路由設(shè)置方法

    vue二級路由設(shè)置方法

    下面小編就為大家分享一篇vue二級路由設(shè)置方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 在antd中setFieldsValue和defaultVal的用法

    在antd中setFieldsValue和defaultVal的用法

    這篇文章主要介紹了在antd中setFieldsValue和defaultVal的用法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • VUE3中watch監(jiān)聽使用實(shí)例詳解

    VUE3中watch監(jiān)聽使用實(shí)例詳解

    watch函數(shù)用來偵聽特定的數(shù)據(jù)源,并在回調(diào)函數(shù)中執(zhí)行副作用,下面這篇文章主要給大家介紹了關(guān)于VUE3中watch監(jiān)聽使用的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Vue3中echarts無法縮放的問題及解決方案

    Vue3中echarts無法縮放的問題及解決方案

    很多朋友在使用vue3+echarts5技術(shù)時(shí)會遇到echarts無法綻放的問題,今天小編就給大家分享下問題描述及解決方案,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • 一文帶你了解Vue?和?React的區(qū)別

    一文帶你了解Vue?和?React的區(qū)別

    這篇文章主要介紹了Vue和React的區(qū)別,畢竟是兩個框架,不像兩個?API,要說細(xì)節(jié)上的區(qū)別就太多太多了,本文就簡單的帶大家說說Vue?和?React?的區(qū)別,需要的朋友可以參考下
    2023-05-05
  • 關(guān)于Vue中keep-alive的作用及使用方法

    關(guān)于Vue中keep-alive的作用及使用方法

    keep-alive是Vue的內(nèi)置組件,當(dāng)它包裹動態(tài)組件時(shí),會緩存不活動的組件實(shí)例,該組件將不會銷毀,這篇文章主要介紹了關(guān)于Vue中keep-alive的作用是什么?怎么使用,需要的朋友可以參考下
    2023-04-04

最新評論