国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

js實(shí)現(xiàn)翻牌小游戲

瀏覽:135日期:2024-04-27 13:42:26

本文實(shí)例為大家分享了js實(shí)現(xiàn)翻牌小游戲的具體代碼,供大家參考,具體內(nèi)容如下

效果圖

js實(shí)現(xiàn)翻牌小游戲

需求分析

1、生成兩組順序隨機(jī)的1-8數(shù)據(jù)2、卡片需要有翻轉(zhuǎn)效果3、兩次翻轉(zhuǎn)數(shù)據(jù)不相等,回復(fù)原狀4、兩次翻轉(zhuǎn)數(shù)據(jù)相等,卡片相等,不能再被點(diǎn)擊5、當(dāng)所有卡片不能被點(diǎn)擊游戲結(jié)束6、限制最大點(diǎn)擊次數(shù)50次

HTML結(jié)構(gòu)

<div class='wrap'> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div> <div> <p class='top'></p> <p class='bottom'></p> </div></div>

css布局

實(shí)現(xiàn)卡片翻轉(zhuǎn)效果需要3個(gè)關(guān)鍵屬性

1.perspective: 1000px; 透視深度,形成3d視角2.transform: rotateY(180deg);旋轉(zhuǎn)3.backface-visibility: hidden; 元素背面不可見(jiàn)

* { margin: 0; padding: 0; } body, html { height: 100%; } .wrap { perspective: 1000px; width: 320px; height: 320px; position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; display: flex; justify-content: center; text-align: center; flex-wrap: wrap; } .wrap div { /* box-shadow: 5px 5px 10px #000; */ transition: 1s; position: relative; text-align: center; line-height: 60px; height: 60px; width: 60px; border-radius: 10px; margin: 10px 10px; } .wrap div .active1 { transform: rotateY(180deg); } .wrap div .active2 { transform: rotateY(360deg); } .wrap p { border-radius: 10px; background-color: blueviolet; transition: 1s; backface-visibility: hidden; display: block; height: 60px; width: 60px; position: absolute; /* transform: rotateY(180deg); */ } .wrap .bottom { transform: rotateY(180deg); } h3{ text-align: center; width: 200px; height: 30px; margin: 150px auto; }

js

class Game { constructor(selector) { this.init(selector); } init(selector) { let arr = this.randomArr(); this.nodes = [...document.querySelectorAll(selector)];//獲取每一個(gè)卡片 this.step = 50;//定義可以被點(diǎn)擊的次數(shù) this.tit = document.querySelector('h3'); this.nodes.forEach((item,index) => {//初始化卡片的值,并保存在num屬性中 item.children[1].innerHTML = arr[index]; item.num = arr[index]; var that = this item.onclick = function () {//給每一個(gè)卡片綁定事件 that.handle(this); } }) } handle(ele) { if(this.step === 0){ alert(’游戲結(jié)束’) return } if(ele.active !== true){ this.step--; this.tit.innerText = `剩余步數(shù):${this.step}` } ele.children[0].classList.add(’active1’);//卡片翻轉(zhuǎn) ele.children[1].classList.add(’active2’); let res = this.nodes.filter(item => item.children[0].classList.contains(’active1’)&& item.active !== true); //獲取翻過(guò)來(lái)的且沒(méi)有配對(duì)成功的卡片 if(res.length === 2 && res[0].num !== res[1].num){//如果有兩張并且數(shù)值不相等,恢復(fù)原狀 setTimeout(()=>{ res[0].children[0].classList.remove(’active1’); res[0].children[1].classList.remove(’active2’); res[1].children[0].classList.remove(’active1’); res[1].children[1].classList.remove(’active2’); },1000)//延遲一秒形成動(dòng)畫(huà) }else if(res.length === 2 && res[0].num === res[1].num){//如果數(shù)值相等則配對(duì)成功 res[0].active = true;//鎖定卡片 res[1].active = true; } } randomArr() { let arr = []; for (let i = 0, n = 8; i < n; i++) { do { var item = randomInt(1, 8); } while (arr.indexOf(item) !== -1) arr.push(item); } arr.push(...arr); return arr; } } new Game(’.wrap div’) function randomInt(min, max) {//產(chǎn)生[min,max]范圍內(nèi)的整數(shù) return Math.round(Math.random() * (max - min)) + min}

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

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 久久夜色精品国产亚洲 | 成人精品 | 在线观看亚洲人成网站 | 国产亚洲福利一区二区免费看 | 国产丝袜美女一区二区三区 | 亚洲欧美日韩国产精品 | 精品无人区一区二区三区a 精品午夜国产在线观看不卡 | 久久依人 | 日本不卡一二三区 | 久久久亚洲国产精品主播 | 黄频漫画| 一区二区三区免费视频观看 | 国产成人性色视频 | 国产亚洲精品久久久久久久网站 | 三级视频在线观看 | 国产aaa女人十八毛片 | 国产亚洲精品国产 | 精品国产综合区久久久久久 | 国产永久免费高清动作片www | 亚洲国产日韩欧美高清片a 亚洲国产日韩欧美在线 | 中文字幕在线成人免费看 | 国产高清一区二区三区视频 | 欧美成人特黄级毛片 | 国产精品高清久久久久久久 | 乱人伦中文视频在线 | 精品国产成人 | 成人免费精品视频 | 国产视频中文字幕 | a级毛片免费观看在线播放 a级毛片免费看 | 狠狠做久久深爱婷婷97动漫 | 日本香蕉一区二区三区 | 2021精品国产品免费观看 | 一区 在线播放 | 黑人一级大毛片 | 成人综合在线观看 | 国产精品99r8免费视频2022 | 久久精品国产99国产精品亚洲 | 岛国在线免费观看 | 欧美亚洲激情视频 | 久久久久久久久久久福利观看 | 久草视频福利在线 |