เรียงไพ่
✨ NARIS PONGPITHAK
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Card Game</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css"> <link rel="stylesheet prefetch" href="https://fonts.googleapis.com/css?family=Coda"> <link rel="stylesheet prefetch" href="https://fonts.googleapis.com/css?family=Gloria+Hallelujah|Permanent+Marker" > <link rel="stylesheet" href="css/app.css"> </head> <body> <div class="container"> <header> <h1>CARD GAME</h1> </header> <section class="score-panel"> <span class="moves">0</span> Move(s) <div class="timer"> </div> <div class="restart" onclick=startGame()> <i class="fa fa-repeat"></i> </div> </section> <ul class="deck" id="card-deck"> <li class="card" type="github"> <i class="fa fa-github" style="font-size:60px;"></i> </li> <li class="card match" type="android"> <i class="fa fa-android" style="font-size:60px;"></i> </li> <li class="card" type="apple" > <i class="fa fa-apple" style="font-size:60px;"></i> </li> <li class="card" type="facebook-square"> <i class="fa fa-facebook-square" style="font-size:60px;"></i> </li> <li class="card match" type="android"> <i class="fa fa-android" style="font-size:60px;"></i> </li> <li class="card" type="chrome"> <i class="fa fa-chrome" style="font-size:60px;"></i> </li> <li class="card" type="github"> <i class="fa fa-github" style="font-size:60px;"></i> </li> <li class="card" type="edge"> <i class="fa fa-edge" style="font-size:60px;"></i> </li> <li class="card" type="chrome"> <i class="fa fa-chrome" style="font-size:60px;"></i> </li> <li class="card" type="edge"> <i class="fa fa-edge" style="font-size:60px;"></i> </li> <li class="card open show" type="apple"> <i class="fa fa-apple" style="font-size:60px;"></i> </li> <li class="card" type="facebook-square"> <i class="fa fa-facebook-square" style="font-size:60px;"></i> </li> </ul> <div id="popup1" class="overlay"> <div class="popup"> <h2>Congratulations!!!</h2> <a class="close" href=# ></a> <div class="content-1"> You Win! </div> <div class="content-2"> <p>You made <span id=finalMove> </span> moves </p> <p>in <span id=totalTime> </span> </p> </div> <button id="play-again"onclick="playagain()"> <a>Play again</a> </button> </div> </div> <a href="https://codepen.io/eliortabeka/pen/WwzEEg" target="_blank">Credit_1</a> <a href="https://scotch.io/tutorials/how-to-build-a-memory-matching-game-in-javascript" target="_blank">Credit_2</a> </div> <style> html { box-sizing: border-box; } *, *::before, *::after { box-sizing: inherit; } html, body { width: 100%; height: 100%; margin: 0; padding: 0; } body { background: url(https://media.giphy.com/media/IeSXccYMz3K4U/giphy.gif); font-family: Georgia, serif; font-size: 16px; } .container { display: flex; justify-content: center; align-items: center; flex-direction: column; } h1 { font-family: Georgia, serif; } /* * Styles for the deck of cards */ .deck { width: 85%; background: ; padding: 1rem; border-radius: 20px; box-shadow: 10px 10px 35px 0 rgba(46, 61, 73, 0.5); display: flex; flex-wrap: wrap; justify-content: space-around; align-items: center; margin: 0 0 3em; } .deck .card { height: 3.7rem; width: 3.7rem; margin: 1rem 1rem; background: black; font-size: 0; color: black; border-radius: 20px; cursor: pointer; display: flex; justify-content: center; align-items: center; box-shadow: 5px 2px 20px 0 rgba(46, 61, 73, 0.5); } .deck .card.open { transform: rotateY(0); background: #02b3e4; cursor: default; animation-name: flipInY; -webkit-backface-visibility: visible !important; backface-visibility: visible !important; animation-duration: .75s; } .deck .card.show { font-size: 33px; } .deck .card.match { cursor: default; background: #0EC13B; font-size: 33px; animation-name: rubberBand; -webkit-backface-visibility: visible !important; backface-visibility: visible !important; animation-duration: .75s; } .deck .card.unmatched { animation-name: pulse; -webkit-backface-visibility: visible !important; backface-visibility: visible !important; animation-duration: .75s; background: #e2043b; } .deck .card.disabled { pointer-events: none; opacity: 0.9; } /* * Styles for the Score Panel */ .score-panel { text-align: left; margin-bottom: 10px; } .score-panel .stars { margin: 0; padding: 0; display: inline-block; margin: 0 5px 0 0; } .score-panel .stars li { list-style: none; display: inline-block; } .score-panel .restart { float: right; cursor: pointer; } .fa-star { color: #FFD700; } .timer { display: inline-block; margin: 0 1rem; } .overlay { position: fixed; top: 0; bottom: 0; left: 0; right: 0; background: rgba(0, 0, 0, 0.7); transition: opacity 500ms; visibility: hidden; opacity: 0; } .overlay:target { visibility: visible; opacity: 1; } .popup { margin: 70px auto; padding: 20px; background: #ffffff; border-radius: 5px; width: 85%; position: relative; transition: all 5s ease-in-out; font-family: Georgia, serif; text-align: center; } .popup h2 { margin-top: 0; color: #333; font-family: Georgia, serif; } .popup .close { position: absolute; top: 20px; right: 30px; transition: all 200ms; font-size: 30px; font-weight: bold; text-decoration: none; color: #333; } .popup .close:hover { color: #E5F720; } .popup .content-1, .content-2 { max-height: 30%; overflow: auto; text-align: center; } .show { visibility: visible !important; opacity: 100 !important; } #starRating li { display: inline-block; } #play-again { background-color: #141214; padding: 0.7rem 1rem; font-size: 1.1rem; display: block; margin: 0 auto; width: 50%; font-family: Georgia, serif; color: #ffffff; border-radius: 5px; } /* animations */ @keyframes flipInY { from { transform: perspective(400px) rotate3d(0, 1, 0, 90deg); animation-timing-function: ease-in; opacity: 0; } 40% { transform: perspective(400px) rotate3d(0, 1, 0, -20deg); animation-timing-function: ease-in; } 60% { transform: perspective(400px) rotate3d(0, 1, 0, 10deg); opacity: 1; } 80% { transform: perspective(400px) rotate3d(0, 1, 0, -5deg); } to { transform: perspective(400px); } } @keyframes rubberBand { from { transform: scale3d(1, 1, 1); } 30% { transform: scale3d(1.25, 0.75, 1); } 40% { transform: scale3d(0.75, 1.25, 1); } 50% { transform: scale3d(1.15, 0.85, 1); } 65% { transform: scale3d(.95, 1.05, 1); } 75% { transform: scale3d(1.05, .95, 1); } to { transform: scale3d(1, 1, 1); } } @keyframes pulse { from { transform: scale3d(1, 1, 1); } 50% { transform: scale3d(1.2, 1.2, 1.2); } to { transform: scale3d(1, 1, 1); } } @media (max-width: 320px) { .deck { width: 85%; } .deck .card { height: 4.7rem; width: 4.7rem; } } @media (min-width: 768px) { .container { font-size: 22px; } .deck { width: 660px; height: 680px; } .deck .card { height: 125px; width: 125px; } .popup { width: 60%; } } </style> <script> let card = document.getElementsByClassName("card"); let cards = [...card]; const deck = document.getElementById("card-deck"); let moves = 0; let counter = document.querySelector(".moves"); let matchedCard = document.getElementsByClassName("match"); let closeicon = document.querySelector(".close"); let modal = document.getElementById("popup1") var openedCards = []; function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; while (currentIndex !== 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; }; document.body.onload = startGame(); function startGame(){ openedCards = []; cards = shuffle(cards); for (var i = 0; i < cards.length; i++){ deck.innerHTML = ""; [].forEach.call(cards, function(item) { deck.appendChild(item); }); cards[i].classList.remove("show", "open", "match", "disabled"); } // reset moves moves = 0; counter.innerHTML = moves; //reset timer second = 0; minute = 0; hour = 0; var timer = document.querySelector(".timer"); timer.innerHTML = "0 mins 0 secs"; clearInterval(interval); } var displayCard = function (){ this.classList.toggle("open"); this.classList.toggle("show"); this.classList.toggle("disabled"); }; //OpenedCards list and check if cards are match or not function cardOpen() { openedCards.push(this); var len = openedCards.length; if(len === 2){ moveCounter(); if(openedCards[0].type === openedCards[1].type){ matched(); } else { unmatched(); } } }; function matched(){ openedCards[0].classList.add("match", "disabled"); openedCards[1].classList.add("match", "disabled"); openedCards[0].classList.remove("show", "open", "no-event"); openedCards[1].classList.remove("show", "open", "no-event"); openedCards = []; } function unmatched(){ openedCards[0].classList.add("unmatched"); openedCards[1].classList.add("unmatched"); disable(); setTimeout(function(){ openedCards[0].classList.remove("show", "open", "no-event","unmatched"); openedCards[1].classList.remove("show", "open", "no-event","unmatched"); enable(); openedCards = []; },1100); } function disable(){ Array.prototype.filter.call(cards, function(card){ card.classList.add('disabled'); }); } function enable(){ Array.prototype.filter.call(cards, function(card){ card.classList.remove('disabled'); for(var i = 0; i < matchedCard.length; i++){ matchedCard[i].classList.add("disabled"); } }); } function moveCounter(){ moves++; counter.innerHTML = moves; //start timer on first click if(moves == 1){ second = 0; minute = 0; hour = 0; startTimer(); } // setting rates based on moves if (moves > 54 && moves < 72){ for( i= 0; i < 3; i++){ if(i > 1){ stars[i].style.visibility = "collapse"; } } } else if (moves > 73){ for( i= 0; i < 3; i++){ if(i > 0){ stars[i].style.visibility = "collapse"; } } } } var second = 0, minute = 0; hour = 0; var timer = document.querySelector(".timer"); var interval; function startTimer(){ interval = setInterval(function(){ timer.innerHTML = minute+" mins "+second+" secs"; second++; if(second == 60){ minute++; second=0; } if(minute == 60){ hour++; minute = 0; } },1000); } function congratulations(){ if (matchedCard.length == 12){ clearInterval(interval); finalTime = timer.innerHTML; // show congratulations modal modal.classList.add("show"); //showing move, time on modal document.getElementById("finalMove").innerHTML = moves; document.getElementById("totalTime").innerHTML = finalTime; closeModal(); }; } function closeModal(){ closeicon.addEventListener("click", function(e){ modal.classList.remove("show"); startGame(); }); } function playagain(){ modal.classList.remove("show"); startGame(); } for (var i = 0; i < cards.length; i++){ card = cards[i]; card.addEventListener("click", displayCard); card.addEventListener("click", cardOpen); card.addEventListener("click",congratulations); }; </script> </body> </html>