vue制作抓娃娃機(jī)的示例代碼
去年為聯(lián)通制作雙十一活動(dòng),做四個(gè)小游戲:‘配對(duì)消消樂(lè)'、移動(dòng)拼圖、抓娃娃、倒計(jì)時(shí)。 現(xiàn)在先做來(lái)分享一下制作抓娃娃游戲時(shí)的經(jīng)驗(yàn) 先上效果圖
游戲規(guī)則:在指定時(shí)間內(nèi)抓到上圖四張卡片為挑戰(zhàn)成功。 現(xiàn)在直接說(shuō)游戲主要內(nèi)容:娃娃滾動(dòng)、爪子向下抓取、抓到卡片 廢話不多說(shuō)直接上代碼?。ù藰邮绞歉鶕?jù)需求而定)
<!--布局樣式--> <div class="game"> <!--爪子--> <div class="paw"> <div class="pawer"></div> <div class="pawerPic"> <img src="./../assets/img/zhuashou.png" class="lose" /> <div class="win" v-if="gzShow2"> <img :src="t_img" /> </div> </div> </div> <!--區(qū)域--> <div class="area"> <!--娃娃滾動(dòng)--> <div id="pack" ref="pack"> <div id="sel1" class="father" ref="imgs"> <img v-for="img in imgs" :class="img.isSuc ? 'yes' : 'no'" :src="img.img" :key="img.id" :alt="img.isSuc" /> </div> </div> </div> <span class="button" @click="zhua"></span> </div>
// css .game { width: 80%; height: 730px; background: url(./../assets/img/interface_1.png) no-repeat; background-size: 100%; animation: bg infinite 0.6s; position: relative; top: -60px; left: 0; z-index: 2; .paw { position: relative; top: 10%; } .pawer { width: 20px; background: rgb(170, 219, 254); height: 10px; position: absolute; top: 20px; left: 51%; margin-left: -15px; border-left: 1px solid rgba(42, 59, 156, 0.7); border-right: 1px solid rgba(42, 59, 156, 0.7); } .pawerPic { width: 95px; // height:85px; position: absolute; top: 30px; left: 51%; margin-left: -55px; img { width: 100%; } .win { position: absolute; bottom: -60px; } } .area { width: 100%; height: 500px; // overflow:hidden; position: absolute; // top:40px; left: 0; bottom: 40px; } /*娃娃滾動(dòng)*/ #pack { width: 80%; white-space: nowrap; overflow: hidden; position: absolute; bottom: 60px; left: 10%; #sel1 { display: block; img { display: block; width: 130px; height: 150px; float: left; margin-left: 20px; } } } .button { display: block; width: 130px; height: 90px; background: url(./../assets/img/button.png) no-repeat; background-size: 100%; position: absolute; bottom: 20px; left: 40%; } }
接下來(lái)就是卡片滾動(dòng),設(shè)置定時(shí)器,給卡片模塊設(shè)置transform 讓卡片從右向左勻速移動(dòng)。
mounted() { this.wawa(); this.talon = $(".pawerPic").offset().left + 100; // 首先獲取爪子的位置(這里是固定的) } wawa() { var pack = this.$refs.pack, width = $("#pack").width(), imgsWidth = (this.imgs.length * width) / 3, initLeft = 0; this.chatTimer = setInterval(function() { initLeft++; if (imgsWidth - initLeft < width + 60) { initLeft = 0; } $("#sel1").css({ width: imgsWidth, transform: "translateX(-" + initLeft + "px)" }); }, 15); },
再接著就是點(diǎn)擊按鈕控制爪子上下移動(dòng)抓取卡片。 首先固定爪子的位置,上圖代碼中mounted里面的talon,然后設(shè)置爪子到下面的終止距離(差不多能夠到卡片),為爪子設(shè)置動(dòng)畫改變top值,拉長(zhǎng)繩子改變height值(因?yàn)樽ψ雍屠K子是分開(kāi)的)。 當(dāng)爪子到達(dá)下面時(shí),獲取每個(gè)卡片的位置,與爪子當(dāng)前位置作比較,看爪子是否在某個(gè)卡片的指定抓取區(qū)域內(nèi)來(lái)判斷是否抓到卡片,然后上升。 上代碼!?。?/p>
代碼全部貼上了,很多功能都是因?yàn)樽约旱男枨笕ゼ拥摹?第一次寫文章,可能具體的表述不清楚。
到此這篇關(guān)于vue制作抓娃娃機(jī)的示例代碼的文章就介紹到這了,更多相關(guān)vue 抓娃娃機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3?中?watch?與?watchEffect?區(qū)別及用法小結(jié)
這篇文章主要介紹了Vue3?中?watch?與?watchEffect?有什么區(qū)別?watch中需要指明監(jiān)視的屬性,也需要指明監(jiān)視的回調(diào),而watchEffect中不需要指明監(jiān)視的屬性,只需要指明監(jiān)視的回調(diào),回調(diào)函數(shù)中用到哪個(gè)屬性,就監(jiān)視哪個(gè)屬性,本文給大家詳細(xì)介紹,需要的朋友參考下2022-06-06vue項(xiàng)目打包發(fā)布后接口報(bào)405錯(cuò)誤的解決
這篇文章主要介紹了vue項(xiàng)目打包發(fā)布后接口報(bào)405錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue項(xiàng)目中按需引入element-ui的正確實(shí)現(xiàn)方法
這篇文章主要介紹了vue項(xiàng)目中按需引入element-ui的正確實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01解決vue-cli創(chuàng)建項(xiàng)目的loader問(wèn)題
下面小編就為大家分享一篇解決vue-cli創(chuàng)建項(xiàng)目的loader問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看看吧2018-03-03Vue組件中的data必須是一個(gè)function的原因淺析
這篇文章主要介紹了Vue組件中的data必須是一個(gè)function的原因淺析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09antd Select下拉菜單動(dòng)態(tài)添加option里的內(nèi)容操作
這篇文章主要介紹了antd Select下拉菜單動(dòng)態(tài)添加option里的內(nèi)容操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用)
這篇文章主要介紹了vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01