亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

js實現翻牌小游戲

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

本文實例為大家分享了js實現翻牌小游戲的具體代碼,供大家參考,具體內容如下

效果圖

js實現翻牌小游戲

需求分析

1、生成兩組順序隨機的1-8數據2、卡片需要有翻轉效果3、兩次翻轉數據不相等,回復原狀4、兩次翻轉數據相等,卡片相等,不能再被點擊5、當所有卡片不能被點擊游戲結束6、限制最大點擊次數50次

HTML結構

<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布局

實現卡片翻轉效果需要3個關鍵屬性

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

* { 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)];//獲取每一個卡片 this.step = 50;//定義可以被點擊的次數 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 () {//給每一個卡片綁定事件 that.handle(this); } }) } handle(ele) { if(this.step === 0){ alert(’游戲結束’) return } if(ele.active !== true){ this.step--; this.tit.innerText = `剩余步數:${this.step}` } ele.children[0].classList.add(’active1’);//卡片翻轉 ele.children[1].classList.add(’active2’); let res = this.nodes.filter(item => item.children[0].classList.contains(’active1’)&& item.active !== true); //獲取翻過來的且沒有配對成功的卡片 if(res.length === 2 && res[0].num !== res[1].num){//如果有兩張并且數值不相等,恢復原狀 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)//延遲一秒形成動畫 }else if(res.length === 2 && res[0].num === res[1].num){//如果數值相等則配對成功 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) {//產生[min,max]范圍內的整數 return Math.round(Math.random() * (max - min)) + min}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 中文字幕亚洲另类天堂 | 被黑人做的白浆直流在线播放 | 国产 欧美 日产中文 | 91性视频| 欧美—级v免费大片 | 中日韩美中文字幕 | 久久国产美女免费观看精品 | 97射射 | 国产精品免费视频能看 | 国产成人精品亚洲午夜麻豆 | 国产一级一片免费播放视频 | 亚洲国产成人精品久久 | 国产免费看网站v片不遮挡 国产免费高清 | 国产精品视频九九九 | 国产在线观看成人免费视频 | 不卡一区二区在线观看 | 亚洲已满18点击进入在线观看 | 亚洲+国产+图片 | 国产精品三级在线 | 狼人久草 | 亚洲欧美另类在线 | 免费网站观看 | 欧美三级黄色大片 | 欧美日韩免费一区二区在线观看 | 日本一级毛片a免费播放 | 黄色片视频在线播放 | 又黄又爽的成人免费视频播放 | 青草精品| 久久精品乱子伦免费 | 免费黄色大片网站 | 成人深夜网站 | 国内真实下药迷j在线观看 国内主播大秀福利视频在线看 | 亚洲精品1区 | 日本视频中文字幕一区二区 | 99这里只有精品在线 | 免费看啪啪网站 | 免费亚洲网站 | 色精品一区二区三区 | 台湾永久内衣秀130部vk | 青青热久麻豆精品视频在线观看 | 高清精品一区二区三区一区 |