HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<main class="app" ng-app="app" ng-controller="AppController" ng-class="{'app-ready': appReady}">
<div class="app__hud">
<div class="app__hud__game-info" ng-hide="gameCompleted" ng-click="titleAction()" ng-swipe-left="changeIcons()" ng-swipe-right="changeIcons()">Find the match</div>
<div class="app__hud__game-completed" ng-show="gameCompleted">
Congratulations!
</div>
<div class="app__hud__game-status">clicks: {{clickCount}}, cards: {{cardsLeft}}</div>
<button ng-click="restart()" class="app__hud__btn-restart">Restart</button>
</div>
<div class="app__cards-container">
<div class="app__cards-container__card" role="button" ng-repeat="card in list" id="{{ 'card-' + card.id }}">
<div class="front app__cards-container__card__front" ng-click="click(card, $index)" ng-swipe-right="click(card, $index)" ng-swipe-left="click(card, $index)"> {{card.isKnown ? "" : "?"}}</div>
<div class="back app__cards-container__card__back">
<span ng-class="card.theme">{{card.icon}}</span>
</div>
</div>
</div>
</main>
*,
*:before,
*:after {
box-sizing: border-box;
outline: none;
}
html,
body {
height: 100%;
}
$card-size: 60px;
$card-size-bigger-device: 120px;
$device-bigger: 600px;
[ng-cloak] {
opacity: 0;
}
body {
min-height: 100%;
margin: 0;
font: 20px sans-serif;
background: linear-gradient(to left, dodgerblue, #345);
}
@for $i from 1 through 8 {
.theme#{$i} {
background: hsla(($i - 1)*45, 70%, 50%, .7);
}
}
button {
background: none;
border: none;
padding: 0;
font: inherit;
color: inherit;
border-bottom: 1px solid currentColor;
cursor: pointer;
// Remove blink effect on mobile device button click
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
.app {
display: block;
opacity: 0;
transition: opacity 2s;
color: white;
&.app-ready {
opacity: 1;
}
&__hud {
background: rgba(0, 0, 0, .1);
text-align: center;
padding: 1em;
&__game-info,
&__game-completed {
display: inline-block;
margin-right: 1em;
}
&__game-status {
display: inline-block;
font-size: 70%;
margin-right: 1em;
}
&__btn-restart {
font-size: 70%;
}
}
&__cards-container {
margin: 5px auto 0;
max-width: 320px;
@media(min-width: $device-bigger) {
max-width: $device-bigger;
}
display: flex;
justify-content: center;
align-items: center;
flex-flow: row wrap;
&__card {
display: inline-block;
margin: 5px;
overflow: hidden;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
user-select: none;
width: $card-size;
height: $card-size;
@media(min-width: $device-bigger) {
width: $card-size-bigger-device;
height: $card-size-bigger-device;
}
// ngAnimate
&.ng-leave {
opacity: 1;
transform: scale3d(1, 1, 1);
transition: 2s ease-in-out;
}
&.ng-leave.ng-leave-active {
opacity: 0;
width: 0;
margin: 0;
transform: scale3d(0, 0, 0);
}
&.ng-enter {
transition: 1s ease-in-out;
opacity: 0;
}
&.ng-enter.ng-enter-active {
opacity: 1;
}
@for $i from 1 through 16 {
&:nth-child(#{$i}) {
.front {
animation-delay: #{$i / 1.5}s;
}
}
}
&__front {
text-align: center;
line-height: $card-size;
@media(min-width: $device-bigger) {
line-height: $card-size-bigger-device;
}
background: rgba(255, 255, 255, .1);
cursor: pointer;
animation: front-cover 2s ease-in-out alternate infinite;
&:hover {
background: rgba(255, 255, 255, .3);
}
}
&__back {
text-align: center;
line-height: $card-size;
@media(min-width: $device-bigger) {
line-height: $card-size-bigger-device;
}
font-size: 40px;
@media(min-width: $device-bigger) {
font-size: 60px;
}
> span {
// Using a span in case we want to do fancy stuff like rotating animation
display: block;
animation: back-cover 2s ease-in-out alternate infinite;
}
}
}
}
}
@keyframes back-cover {
0% {
transform: scale(1);
}
100% {
transform: scale(1.2);
}
}
@keyframes front-cover {
0% {
// Play with some animations for the front cover :)
}
100% {}
}
@keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.ng-enter-stagger {
transition-delay: 0.1s;
transition-duration: 0s;
}
.ng-leave-stagger {
transition-delay: 0.1s;
transition-duration: 0s;
}
; {
let
selected1,
selected2;
// http://unicode.org/emoji/charts/full-emoji-list.html
const iconAnimals = [
'🐵',
'🐶',
'🐺',
'🐱',
'🐯',
'🐴',
'🐮',
'🐷',
'🐗',
'🐭',
'🐹',
'🐰',
'🐻',
'🐨',
'🐼',
'🐔',
'🐤',
'🐦',
'🐧',
'🐸',
//'🐢 ',
//'🐍',
//'🐳',
//'🐬',
//'🐠',
//'🐙',
];
const iconFruits = [
'🍇',
'🍈',
'🍉',
'🍊',
'🍋',
'🍌',
'🍍',
'🍎',
'🍏',
'🍑',
'🍒',
'🍓',
];
let icons = iconAnimals;
const app = angular
// Using ngTouch to remove the 300ms click delay on mobile devices
.module('app', ['ngRoute', 'ngAnimate', 'ngTouch'])
.controller('AppController', AppController);
AppController.$inject = ['$scope', '$timeout', '$animate'];
function AppController($scope, $timeout, $animate) {
// This is a prototype, everything is in this controller.
// For cleaner code you probably would refactor stuff out from here.
// I also use DOM manipulation for faster dev (prototyping)
const vm = this;
$timeout(() => {
// Angular DOM has finished rendering
$scope.appReady = true;
});
function showAllCards() {
$scope.list.forEach((card) => {
$("#card-" + card.id).flip(true);
});
}
function startNewGame() {
shuffleArray(icons); // randomize displayed icons
$animate.enabled(false); // disable remove animation
$scope.list = [];
//$animate.enabled(true); // re-enable animation
let list = [],
cardCount = 16;
for (var i = 1; i <= cardCount; i++) {
let type = (i % (cardCount / 2)) + 1;
list.push({
id: i,
type: type,
icon: icons[type - 1],
theme: 'theme' + type,
isKnown: false
});
}
shuffleArray(list); // randomize card on the board
// Update state
selected1 = selected2 = null;
$scope.clickCount = 0;
$scope.cardsLeft = list.length;
$scope.gameCompleted = false;
$scope.titleAction = () => {};
$scope.changeIcons = () => {
icons = icons === iconFruits ? iconAnimals : iconFruits;
};
$timeout(() => {
// Angular DOM has finished rendering
$animate.enabled(true); // re-enable animation
$scope.list = list;
$timeout(() => {
// Angular DOM has finished rendering
// Apply flip to the cards in the DOM
let $card = $(".app__cards-container__card");
$card.flip({
axis: 'y',
trigger: 'manual',
reverse: true
}, () => {
//callback
});
//showAllCards(); // debug
});
});
}
$scope.clickCount = 0;
$scope.cardsLeft = 0;
$scope.gameCompleted = false;
$scope.restart = () => {
startNewGame();
};
$scope.click = function(card, index) {
if (card.flipped) return; // dont allow to flip already flipped cards
$scope.clickCount++;
// Update state
card.flipped = true;
selected2 = selected1;
selected1 = card;
// Update UI
$("#card-" + card.id).flip(true); // use flip api
if (selected1 && selected2 && selected1.type === selected2.type) {
// We found a match
$scope.list.splice($scope.list.indexOf(selected1), 1);
$scope.list.splice($scope.list.indexOf(selected2), 1);
selected1 = selected2 = null;
$scope.cardsLeft = $scope.list.length;
if ($scope.list.length === 0) {
// We have finished the game
$scope.gameCompleted = true;
}
return;
}
if (selected2) {
// Flip back the 2nd shown card
let id = selected2.id;
((id) => {
$timeout(() => {
$scope.list.forEach((card) => {
if (card.id === id) {
// Update state
card.flipped = false;
card.isKnown = true;
}
});
$("#card-" + id).flip(false); // use flip api
}, 800);
})(id);
}
}
startNewGame();
}
/**
* Randomize array element order in-place.
* Durstenfeld shuffle algorithm.
*/
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
};
Also see: Tab Triggers