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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<body>
<div id="simon">
<div class="container-fluid row no-gutter">
<div class="col-xs-6">
<div id="green" class="green click pull-right"></div>
</div>
<div class="col-xs-6">
<div id="red" class="red click col-xs-6 pull-left"></div>
</div>
<div class="col-xs-6">
<div id="yellow" class="yellow click col-xs-6 pull-right"></div>
</div>
<div class="col-xs-6">
<div id="blue" class="blue click col-xs-6 pull-left"></div>
</div>
</div>
</div>
<div class="center container row" id="buttons">
<div class="col-xs-12">
<button class="btn btn-info outline" id="go" onclick="start()">GO</button>
<button class="btn btn-info outline" id="reset" onclick="reset()">RESET</button>
<div class="vcenter" id="moves">0</div>
<div class="onoffswitch vcenter">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
<label class="onoffswitch-label" for="myonoffswitch">STRICT</label>
</div>
</div>
</div>
</body>
/*Textures from http://lea.verou.me/css3patterns/
body{} has the cube texture.
*/
body {
background-color: #556;
background-image: linear-gradient(30deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445), linear-gradient(150deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445), linear-gradient(30deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445), linear-gradient(150deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445), linear-gradient(60deg, #99a 25%, transparent 25.5%, transparent 75%, #99a 75%, #99a), linear-gradient(60deg, #99a 25%, transparent 25.5%, transparent 75%, #99a 75%, #99a);
background-size: 80px 140px;
background-position: 0 0, 0 0, 40px 70px, 40px 70px, 0 0, 40px 70px;
}
.center {
text-align: center;
}
.vcenter {
display: inline-block;
vertical-align: middle;
float: none;
}
#simon {
margin-top: 20px;
margin-bottom: 20px;
}
#moves {
background-color: grey;
display: inline-block;
width: 50px;
height: 50px;
font-size: 30px;
font-weight: bold;
text-align: center;
border-radius: 100%;
line-height: 50px; /*vertical center*/
white-space: nowrap; /* */
color: white;
}
/*for GO & reset buttons*/
.btn.outline {
background: none;
border-radius: 100%;
height: 50px;
border: 3px solid white;
color: white;
}
.btn.outline:hover,
.btn.outline:focus,
.btn.outline:active,
.btn.outline.active {
color: grey;
background: none;
border-color: grey;
}
.btn.outline:active,
.btn.outline.active {
border-color: grey;
color: grey;
background: none;
box-shadow: none;
}
/* */
/*buttons container*/
#buttons {
height: 50px;
margin: 0 auto;
}
#go {
font-size: 20px;
font-weight: bold;
}
#reset {
font-size: 12px;
font-weight: bold;
}
#green, #red, #yellow, #blue {
height: 120px;
width: 120px;
display: inline-block;
border: 4px solid grey;
-webkit-box-shadow: 10px 10px 24px 0px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 10px 10px 24px 0px rgba(0, 0, 0, 0.75);
box-shadow: 10px 10px 24px 0px rgba(0, 0, 0, 0.75);
opacity: .8;
}
/* Color-specific border-radius & color textures below.
The textures could possibly be combined into one class */
#green {
background-color: green;
border-radius: 100% 10% 10% 10%;
background: linear-gradient(63deg, green 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, green 78%), linear-gradient(63deg, transparent 34%, green 38%, green 58%, transparent 62%), #005900;
background-size: 16px 48px;
}
#red {
background-color: red;
border-radius: 10% 100% 10% 10%;
background: linear-gradient(63deg, #b20000 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #b20000 78%), linear-gradient(63deg, transparent 34%, #b20000 38%, #b20000 58%, transparent 62%), #990000;
background-size: 16px 48px;
}
#yellow {
background-color: orange;
border-radius: 10% 10% 10% 100%;
background: linear-gradient(63deg, orange 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, orange 78%), linear-gradient(63deg, transparent 34%, orange 38%, orange 58%, transparent 62%), #b27300;
background-size: 16px 48px;
}
#blue {
background-color: blue;
border-radius: 10% 10% 100% 10%;
background: linear-gradient(63deg, blue 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, blue 78%), linear-gradient(63deg, transparent 34%, blue 38%, blue 58%, transparent 62%), #0000b2;
background-size: 16px 48px;
}
/*Color-specific user-click transitions below. */
.click {
-webkit-transition: all .07s cubic-bezier(0.645, 0.045, 0.355, 1);
transition: all .07s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.click:active {
box-shadow: none;
}
/*The four classes below take care of both
tap-click and press-hold clicks. The .push
classes are added by jquery. */
.red.push, .red.click:active {
transform: scale(0.95) translateY(2px) translateX(-2px);
background: linear-gradient(63deg, #ff4d4d 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #ff4d4d 78%), linear-gradient(63deg, transparent 34%, #ff4d4d 38%, #ff4d4d 58%, transparent 62%), #990000 !important;
background-size: 16px 48px !important;
}
.green.push, .green.click:active {
transform: scale(0.95) translateY(2px) translateX(2px);
background: linear-gradient(63deg, #00cd00 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #00cd00 78%), linear-gradient(63deg, transparent 34%, #00cd00 38%, #00cd00 58%, transparent 62%), #005900 !important;
background-size: 16px 48px !important;
}
.yellow.push, .yellow.click:active {
transform: scale(0.95) translateY(-2px) translateX(2px);
background: linear-gradient(63deg, #ffc04d 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #ffc04d 78%), linear-gradient(63deg, transparent 34%, #ffc04d 38%, #ffc04d 58%, transparent 62%), #b27300 !important;
background-size: 16px 48px !important;
}
.blue.push, .blue.click:active {
transform: scale(0.95) translateY(-2px) translateX(-2px);
background: linear-gradient(63deg, #4d4dff 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #4d4dff 78%), linear-gradient(63deg, transparent 34%, #4d4dff 38%, #4d4dff 58%, transparent 62%), #0000b2 !important;
background-size: 16px 48px !important;
}
/*Color-specific pop-out transitions below. */
.showMe {
transform: scale(1.05);/* translateY(-2px) translateX(2px);*/
}
.green.showMe {
/*transform: scale(1.05) translateY(-2px) translateX(-2px);*/
background: linear-gradient(63deg, #00cd00 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #00cd00 78%), linear-gradient(63deg, transparent 34%, #00cd00 38%, #00cd00 58%, transparent 62%), #005900 !important;
background-size: 16px 48px !important;
}
.red.showMe {
/*transform: scale(1.05) translateY(-2px) translateX(2px);*/
background: linear-gradient(63deg, #ff4d4d 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #ff4d4d 78%), linear-gradient(63deg, transparent 34%, #ff4d4d 38%, #ff4d4d 58%, transparent 62%), #990000 !important;
background-size: 16px 48px !important;
}
.yellow.showMe {
/*transform: scale(1.05) translateY(-2px) translateX(2px);*/
background: linear-gradient(63deg, #ffc04d 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #ffc04d 78%), linear-gradient(63deg, transparent 34%, #ffc04d 38%, #ffc04d 58%, transparent 62%), #b27300 !important;
background-size: 16px 48px !important;
}
.blue.showMe {
/*transform: scale(1.05) translateY(-2px) translateX(2px);*/
background: linear-gradient(63deg, #4d4dff 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #4d4dff 78%), linear-gradient(63deg, transparent 34%, #4d4dff 38%, #4d4dff 58%, transparent 62%), #0000b2 !important;
background-size: 16px 48px !important;
}
/* Toggle switch below.
Generated using https://proto.io/freebies/onoff/
*/
.onoffswitch {
display: inline-block;
position: relative;
width: 50px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block;
overflow: hidden;
cursor: pointer;
height: 24px;
padding: 0;
line-height: 24px;
border: 2px solid #CCCCCC;
border-radius: 24px;
background-color: blue;
transition: background-color 0.2s linear;
color: white;
font-size: 12px;
}
.onoffswitch-label:before {
color: white;
opacity: .5;
content: "";
display: block;
width: 26px;
height: 24px;
margin: 0px;
background: #FFFFFF;
position: absolute;
top: 0;
bottom: 0;
right: 24px;
border: 2px solid green;
border-radius: 24px;
transition: all 0.2s linear;
}
.onoffswitch-checkbox:checked + .onoffswitch-label {
background-color: red;
}
.onoffswitch-checkbox:checked + .onoffswitch-label,
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
border-color: green;
}
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
right: 0px;
}
/*Non-strict error animation below.
Applied with mistake()
*/
.mistake {
background: linear-gradient(63deg, #808080 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #808080 78%), linear-gradient(63deg, transparent 34%, #808080 38%, #808080 58%, transparent 62%), #a6a6a6 !important;
background-size: 16px 48px !important;
}
/*remove space between bootstrap columns
from http://curabites.com/development/2015/06/14/how-to-remove-gutter-space-between-columns-in-bootstrap-3/
*/
.row.no-gutter {
margin-left: 0;
margin-right: 0;
}
.row.no-gutter [class*='col-']:not(:first-child),
.row.no-gutter [class*='col-']:not(:last-child) {
padding-right: 0;
padding-left: 0;
}
var strict = false;
var compSequence = [];
var userSequence = [];
var lightInt = 500; //light playback delay
var counter = 0; //how many correct presses you've made
var winAt = 20; //sequence length needed to win
var sound_yellow = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
var sound_green = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
var sound_red = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
var sound_blue = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
var sound_gameover = new Audio("https://bit.ly/2i6znut");
var sound_mistake = new Audio("https://bit.ly/2hDtBwZ");
var sound_toggle = new Audio("https://bit.ly/2hVnoQ0");
var sound_reset = new Audio("https://bit.ly/2ieTILd");
var sound_win = new Audio("https://bit.ly/2isBt4v");
//start with colored buttons turned off
$(document).ready(function() {
disable();
});
$('.click').click(function() {
var input = "";
var len = compSequence.length;
if($(this).hasClass('red')) input = "red";
if($(this).hasClass('green')) input = "green";
if($(this).hasClass('yellow')) input = "yellow";
if($(this).hasClass('blue')) input = "blue";
userSequence.push(input);
if(input !== compSequence[counter]) { //if you miss one, replay...
if(strict) ending(); //...start over if you're in strict mode
else mistake1(); //...or make ouch sound in not in strict
} else {
clickMe(input);
playSound(input);
++counter; //otherwise, try matching next in sequence
if(userSequence.length >= compSequence.length) {
if(compSequence.length == winAt) win(); //if sequence is winning length, win
else addPush(); //else, add another
readOut();
}
}
});
//This does the inward-click animation.
//The css handles a tap-click.
function clickMe(color) {
$('#' + color).addClass('push');
setTimeout(function() {
$('#' + color).removeClass('push');
}, lightInt / 2);
}
//toggle strict mode
$("#myonoffswitch").click(function() {
if(strict === false) strict = true;
else strict = false;
sound_toggle.play();
});
//Adds a step to the computer's sequence
function addPush() {
disable();
var rnd = Math.floor(Math.random() * 4); //since there are 4 buttons
var toPush = "";
switch(rnd) {
case 0:
toPush = "red";
break;
case 1:
toPush = "green";
break;
case 2:
toPush = "yellow";
break;
case 3:
toPush = "blue";
break;
default:
toPush = "ERROR";
}
compSequence.push(toPush);
document.getElementById("moves").innerHTML = Number(document.getElementById("moves").innerHTML) + 1; //increment move counter
}
//Wrapper for rdOut(). It returns true so that enable() can wait for it
//to finish.
function readOut() {
disable();
userSequence = [];
counter = 0;
if(compSequence === []) return;
setTimeout(function() {
rdOut(compSequence, 0); //starts at index 0
}, lightInt);
}
//A recursive function using setTimeout.
//Can't use setInterval since the interval
//isn't even.
function rdOut(seq, index) {
var len = seq.length;
if(len === 0 || index >= len) {
enable();
return;
}
setTimeout(function() {
$('#' + seq[index]).addClass('showMe');
playSound(seq[index]);
setTimeout(function() {
$('#' + seq[index]).removeClass('showMe');
rdOut(seq, index + 1);
}, lightInt / 2);
}, lightInt);
}
//reset everything
function reset() {
sound_reset.play();
userSequence = [];
compSequence = [];
document.getElementById("moves").innerHTML = "0";
$("#go").prop('disabled', false);
enable();
}
//for the first move
function start() {
enable();
document.getElementById("moves").innerHTML = "0";
$("#go").prop('disabled', true);
addPush();
if(readOut()) enable();
}
//when you lose in strict mode
function ending() {
mistake2();
disable();
compSequence = [];
userSequence = [];
counter = 0;
$("#go").prop('disabled', false);
}
//for a mistake in non-strict mode
function mistake1() {
sound_mistake.play();
var temp = document.getElementById("moves").innerHTML;
document.getElementById("moves").innerHTML = ":(";
$("#red").addClass("mistake");
$("#green").addClass("mistake");
$("#yellow").addClass("mistake");
$("#blue").addClass("mistake");
setTimeout(function() {
$("#red").removeClass("mistake");
$("#green").removeClass("mistake");
$("#yellow").removeClass("mistake");
$("#blue").removeClass("mistake");
setTimeout(function() {
document.getElementById("moves").innerHTML = temp;
if(readOut()) enable();
}, lightInt);
}, lightInt);
}
//if you make a mistake in strict mode, the
//buttons turn grey and you have to push reset or go
function mistake2() {
sound_gameover.play();
document.getElementById("moves").innerHTML = ":(";
$("#red").addClass("mistake");
$("#green").addClass("mistake");
$("#yellow").addClass("mistake");
$("#blue").addClass("mistake");
disable();
}
//disable all the buttons except reset
function disable() {
document.getElementById("red").style.pointerEvents = 'none';
document.getElementById("green").style.pointerEvents = 'none';
document.getElementById("yellow").style.pointerEvents = 'none';
document.getElementById("blue").style.pointerEvents = 'none';
$("#slider").prop('disabled', true);
}
//re-enable buttons
function enable() {
console.log("enabled");
document.getElementById("red").style.pointerEvents = 'auto';
document.getElementById("green").style.pointerEvents = 'auto';
document.getElementById("yellow").style.pointerEvents = 'auto';
document.getElementById("blue").style.pointerEvents = 'auto';
if($("#red").hasClass("mistake")) $("#red").removeClass("mistake");
if($("#green").hasClass("mistake")) $("#green").removeClass("mistake");
if($("#yellow").hasClass("mistake")) $("#yellow").removeClass("mistake");
if($("#blue").hasClass("mistake")) $("#blue").removeClass("mistake");
$("#slider").prop('disabled', false);
}
//Plays the sound matching the color. Maybe all the sounds could be consolidated here.
function playSound(color) {
switch(color) {
case 'yellow':
if(!sound_yellow.ended) {
var nother = sound_yellow.cloneNode();
nother.play();
} else sound_yellow.play();
break;
case 'green':
if(!sound_green.ended) {
var nother = sound_green.cloneNode();
nother.play();
} else sound_green.play();
break;
case 'red':
if(!sound_red.ended) {
var nother = sound_red.cloneNode();
nother.play();
} else sound_red.play();
break;
case 'blue':
if(!sound_blue.ended) {
var nother = sound_blue.cloneNode();
nother.play();
} else sound_blue.play();
break;
default:
console.log("sounds are broken");
}
}
//HAPPY
function win() {
sound_win.play();
document.getElementById("moves").innerHTML = ":)";
disable();
compSequence = [];
userSequence = [];
counter = 0;
$("#go").prop('disabled', false);
}
Also see: Tab Triggers