Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p><button id="flip-a-card">Flip a Random Card (or Ready it for Flipping)</button></p>

<ul class="flip" >
	<li>
		<div>
			1
		</div>
		<div>
			2
		</div>
	</li>
	<li>
		<div>
			3
		</div>
		<div>
			4
		</div>
	</li>
	<li>
		<div>
			5
		</div>
		<div>
			6
		</div>
	</li>
	<li>
		<div>
			7
		</div>
		<div>
			8
		</div>
	</li>
	<li>
		<div>
			9
		</div>
		<div>
			10
		</div>
	</li>
</ul>

<h3>#Pile</h3>

<div id="pile">
	<div >
		11
	</div>
	<div>
		12
	</div>
	<div >
		13
	</div>
	<div>
		14
	</div>
	<div>
		15
	</div>
	<div >
		16
	</div>
	<div >
		17
	</div>
	<div>
		18
	</div>
	<div >
		19
	</div>
	<div>
		20
	</div>
</div><!-- /#pile -->

<h2>Notes</h2>

<p>Abstracted from the random client logo flip display at <a href="http://theauditor.com"> http://theauditor.com</a>.

<p>
	We have 5 cards (blue on the front, red on the back for explanatory purposes). The first card has content item "1" on the front and "2" on the back, etc, until we reach the 5th and final card. A card has a back by having its two child divs positioned absolutely to be one on top of the other and having the second of these rotated 180deg. Conversely a flipped card has its first child div rotated 180deg and its second rotated 0deg.</p>

<p>We have a #pile of any number of more divs (probably not to be displayed in production) here containing items "11" to "20".</p>

<p>A randomly-selected card gets flipped by the jQuery toggling its <tt>flipped</tt> class.</p>

<p>If the randomly-selected card just acquired the <tt>flipped</tt> class, then its second child is displayed instead of its first child.</p>

<p>Conversely, if the randomly-selected card just lost its <tt>flipped</tt> class, then its first item is removed and added to the pile; a pile item that's already been randomly selected is appended to the card; and the new first child shows.</p>

<p>However! The newly unflipped card's first child is its former second child, so we get the same content again. Therefore we set the non-<tt>flipped</tt> transition time to 0.</p>

<p>So the actual visible flip only happens to cards that are not <tt>flipped</tt>. If a card is already <tt>flipped</tt>, flipping it merely feeds it with new content and sets it to be usefully flippable next time it's selected. Awkward, but I can't see how else this can work.</p>
              
            
!

CSS

              
                li {
	display: inline-block;
	list-style: none;
	width: 5rem;
	height: 5rem;
	margin-right: 1rem;
	line-height: 5rem;
	-webkit-transform-style: preserve-3d;
}

li div {
	width: 5rem;
	height: 5rem;
	position: absolute;
	backface-visibility: hidden;
	transform-style: preserve-3d;
	color: #fff;
	font-size: 200%;
	line-height: 5rem;
	text-align: center;
}

li.flipped div
{
	transition: all 1s linear .5s;
}

li div:first-child,
li.flipped div:last-child
{
	transform: rotateY(0deg);
}

li div:first-child
{
	background: blue;
}	

li.flipped div:last-child
{
	background: red;
}

li div:last-child,
li.flipped div:first-child
{
	transform: rotateY(180deg);
}

#pile div
{
	display: inline-block;
	margin-right: 1rem;
}
              
            
!

JS

              
                // With thanks to http://kometschuh.de/Example-Random-CSS3-Image-Flip-Effect.html

function flip()
{
	var flipCards = $('.flip').children('li');
	var flipItems = $('#pile').children('div');

	if (flipCards.length > 1 && flipItems.length > 1)
	{
		
		// Select both a random card and a random item from the pile
		randCard = Math.floor(Math.random() * flipCards.length);
		randItem = Math.floor(Math.random() * flipItems.length);
		var card = flipCards[randCard];
		var item = flipItems[randItem];

		// If the chosen card's already flipped, swap out its hidden/front item with the selected item from the file
		if ( $(card).hasClass("flipped") )
		{
			// Move the item in the card's current now-hidden front to the pile 
			$("#pile").append( $(card).children("div:first") );

			// Pull selected item to the now-showing back of the card
			$(card).append(item);
		}

		// Flip the card either way
		$(card).toggleClass('flipped');
		
	}
}

$("#flip-a-card").on("click", function()
{
	flip();
});
              
            
!
999px

Console