Ever play Memory as a kid? In this game you’re presented with an even number of cards, and pairs of the cards have the same face. All of the cards start face down. You then reveal two cards - if they match they remain face up. Here’s our HTML
<!DOCTYPE html>
<html>
<head>
<title>Memory</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$('.card').click(function(){
$(this).toggleClass('down');
});
</script>
</head>
<body>
<div id="Playfield">
<div class="card down one" data-face="1"></div>
<div class="card down two" data-face="2"></div>
<div class="card down three" data-face="3"></div>
<div class="card down four" data-face="4"></div>
<div class="card down five" data-face="5"></div>
<div class="card down six" data-face="6"></div>
<div class="card down seven" data-face="7"></div>
<div class="card down eight" data-face="8"></div>
<div class="card down nine" data-face="9"></div>
<div class="card down ten" data-face="10"></div>
<div class="card down eleven" data-face="11"></div>
<div class="card down twelve" data-face="12"></div>
<div class="card down one" data-face="1"></div>
<div class="card down two" data-face="2"></div>
<div class="card down three" data-face="3"></div>
<div class="card down four" data-face="4"></div>
<div class="card down five" data-face="5"></div>
<div class="card down six" data-face="6"></div>
<div class="card down seven" data-face="7"></div>
<div class="card down eight" data-face="8"></div>
<div class="card down nine" data-face="9"></div>
<div class="card down ten" data-face="10"></div>
<div class="card down eleven" data-face="11"></div>
<div class="card down twelve" data-face="12"></div>
</div>
</body>
</html>
Now, this challenge is broken in two. The CSS side of the challenge is to do the following:
Beginner Level
Style the shape of the cards, their backs and fronts. Respond to Java Script toggling the presence of ‘down’ - if the class is present the card should be face down.
Your styling must tolerate Java Script changing the position of the card in the DOM or absolute positioning of the card.
Javascript will also be applying a class of ‘matched’ to the cards when the player successfully matches a pair. You need to style for this (perhaps by changing the border color). To test your styling you’ll need to manually edit the HTML (or do the JS challenge as well )
Advanced and Expert
Animate the flip using a 3D transition.
As usual, use spoiler tags to hide your answers to the challenge that you post.
This…
[noparse]
[spoiler]Like this[/spoiler]
[/noparse]
Renders
[spoiler]Like this[/spoiler]
If you have Java Script programming skills there is a [thread=991431]tie in challenge[/thread] in the Java Script forum.
Note - the data-face attribute is present to make the JS code easier to write - I realize it’s redundant to the final CSS class name. I also know it’s possible to style on attributes, but browser support for that trick is uneven (not that such has stopped us before). Both are presented to keep the difficulty of the challenge down some.
Here is a demo of both the JS and CSS entries working together, in case if anyone’s interested. x] This and my post at the JS challenge are my first posts. I did a few of the challenges but I never posted them for reasons unknown to myself haha.
[spoiler]<!DOCTYPE html>
<html>
<head>
<title>Memory</title>
<style>
body {
font-family: sans-serif;
padding: 20px 20px 10px;
}
#Playfield {
margin: 0 auto;
-webkit-perspective: 1000;
-moz-perspective: 1000;
-ms-perspective: 1000;
-o-perspective: 1000;
-webkit-perspective: 1000;
position: relative;
width: 960px;
}
#Playfield .card {
float: left;
height: 110px;
margin: 0 5px 10px;
position: relative;
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-ms-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
-webkit-transition: -webkit-transform 0.5s;
-moz-transition: -moz-transform 0.5s;
-o-transition: -o-transform 0.5s;
-webkit-transition: -webkit-transform 0.5s;
width: 110px;
}
#Playfield .card div {
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
-o-backface-visibility: hidden;
-webkit-backface-visibility: hidden;
height: 110px;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
#Playfield .card .front {
background-color: #dedede;
z-index: 0;
}
#Playfield .card .back {
background-color: #333;
color: #fff;
font-size: 30px;
line-height: 100px;
text-align: center;
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-ms-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
}
#Playfield .card.down {
-webkit-transform: rotateY(0deg);
-moz-transform: rotateY(0deg);
-ms-transform: rotateY(0deg);
-o-transform: rotateY(0deg);
-webkit-transform: rotateY(0deg);
}
#Playfield .card.down .front {
z-index: 1;
}
</style>
</head>
<body>
<div id="Playfield">
<div class="card down one" data-face="1"></div>
<div class="card down two" data-face="2"></div>
<div class="card down three" data-face="3"></div>
<div class="card down four" data-face="4"></div>
<div class="card down five" data-face="5"></div>
<div class="card down six" data-face="6"></div>
<div class="card down seven" data-face="7"></div>
<div class="card down eight" data-face="8"></div>
<div class="card down nine" data-face="9"></div>
<div class="card down ten" data-face="10"></div>
<div class="card down eleven" data-face="11"></div>
<div class="card down twelve" data-face="12"></div>
<div class="card down one" data-face="1"></div>
<div class="card down two" data-face="2"></div>
<div class="card down three" data-face="3"></div>
<div class="card down four" data-face="4"></div>
<div class="card down five" data-face="5"></div>
<div class="card down six" data-face="6"></div>
<div class="card down seven" data-face="7"></div>
<div class="card down eight" data-face="8"></div>
<div class="card down nine" data-face="9"></div>
<div class="card down ten" data-face="10"></div>
<div class="card down eleven" data-face="11"></div>
<div class="card down twelve" data-face="12"></div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
(function($){
$.fn.shuffle = function() {
return this.each(function(){
var items = $(this).children();
return (items.length)
? $(this).html($.shuffle(items))
: this;
});
}
$.shuffle = function(arr) {
for(
var j, x, i = arr.length; i;
j = parseInt(Math.random() * i),
x = arr[--i], arr[i] = arr[j], arr[j] = x
);
return arr;
}
})(jQuery);
// The total number of cards currently selected and total times failed
var counter = 0,
failed = 0,
limit = 3;
// Shuffle the cards around
$('#Playfield').shuffle();
$('.card').each(function() {
var $this = $(this);
// Create the front side of the card
$('<div />', {'class' : 'front'}).appendTo($this);
// Create the back side of the card
$('<div />', {
'class' : 'back',
html : $this.data('face')
}).appendTo($this);
});
// Bind the click event to the card so we can toogle the state the
// backface-visibility, this property only applies to browsers that
// support it
$(document.body).on('click', '.card', function() {
// If the counter/current class matches something that should ensure the
// click is check for it and prevent anything else from happening
if (counter === 2 || $(this).is('.matched') || !$(this).is('.down'))
return;
// Remove the "down" class since the current card can't be turned off
// unless a second card is selected
$(this).removeClass('down');
// Increment the active cards counter
counter++;
// Find the 2 cards that have been selected if the counter is at 2
if (counter === 2) {
var cards = $('.card:not(.down, .matched)'),
delay = 0,
match = $(cards[0]).data('face') === $(cards[1]).data('face');
if (match) {
cards.addClass('matched');
// Now we need to check if every card was matched, we can do this by
// simply getting a total count of all the cards compared to how many
// have the "matched" class
var $cards = $('.card'),
total = $cards.length,
matched = $('.card.matched').length;
if (total === matched){
alert('You Win!');
$cards.toggleClass('matched down');
}
} else {
delay = 700;
failed++;
setTimeout(function() {
cards.addClass('down');
}, delay);
}
// Check the number of times that the player has failed, if they have hit
// the limit reset the cards and the counter
setTimeout(function() {
if (failed === limit) {
alert('Sorry, but you have wrongly matched too many cards. The cards will now be shuffled around!');
// Reset the cards
$('.card').removeClass('matched').addClass('down');
// Shuffle the cards around
setTimeout(function() {
$('#Playfield').shuffle();
}, 200);
// Reset the counters
counter = failed = 0;
} else {
counter = 0;
}
}, delay);
}
});
</script>
</body>
</html>[/spoiler]
I’m not quite sure what you are asking but the details are explained in post #1 above and in post #1 of the JS section. If that doesn’t answer your question please clarify.