cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <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>
            
          
!
            
              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;
}
            
          
!
            
              // 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
Loading ..................

Console