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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console