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 URL's 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 it's URL and the proper URL extention.
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.
%div{ :align => 'center' }
.base
.center
.controls
%h1
Simon
%sup ®
.round-count --
%p Round
.on-off
.on
.off
%p Off
%p On
.start
%p Start
.strict
.strict-led
%p Strict
.simon-row
.green{ 'data-index' => 0 }
.red{ 'data-index' => 1 }
.simon-row
.yellow{ 'data-index' => 2 }
.blue{ 'data-index' => 3 }
body
background-color: #ffb54b
.color-active
cursor: pointer
background: radial-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0))
.green
border-top-left-radius: 100%
background-color: green
.red
border-top-right-radius: 100%
background-color: red
.yellow
border-bottom-left-radius: 100%
background-color: yellow
.blue
border-bottom-right-radius: 100%
background-color: blue
.green, .red, .yellow, .blue
position: relative
display: inline-block
width: 225px
height: 225px
z-index: 10
.base
background-color: #333
border-radius: 50%
width: 500px
height: 500px
box-shadow: 0 0 15px 0 rgba(0,0,0,1)
.simon-row
position: relative
top: 24px
.center
width: 200px
height: 200px
background-color: #333
border-radius: 50%
position: relative
top: 150px
margin-bottom: -200px
z-index: 15
.controls
background-color: white
width: 190px
height: 190px
border-radius: 50%
position: relative
top: -25px
left: -25px
font-family: 'Squada One', monospace
z-index: 5
border: 25px solid #333
h1
color: #222
padding: 0
margin: 0
display: inline
position: relative
left: 33px
top: 11px
sup
font-family: monospace
.round-count
position: relative
display: inline-block
background-color: #222
padding: 3px
border-radius: 3px
color: #555
top: 43px
left: -45px
font-family: 'Share Tech Mono', monospace
font-weight: bold
p
position: relative
top: 43px
left: -110px
display: inline-block
.on-off
background-color: #222
position: relative
width: 25px
height: 10px
top: 20px
left: 43px
>p:nth-child(3)
position: relative
top: -42px
left: -15px
>p:last-child
position: relative
top: -91px
left: 13px
.on, .off
width: 10px
height: 10px
position: relative
cursor: pointer
.on
left: 8px
display: none
background-color: green
&:hover
background-color: darkgreen
.off
display: block
left: -7px
background-color: red
&:hover
background-color: darkred
.start
display: inline-block
width: 32px
height: 32px
background-color: #555
border-radius: 50%
position: relative
top: 60px
left: -20px
p:nth-child(6)
position: relative
top: 25px
left: -54px
.strict
width: 32px
height: 32px
background-color: #555
border-radius: 50%
position: relative
top: 9px
left: 40px
.strict-led
width: 8px
height: 8px
background-color: #222
border-radius: 50%
position: relative
top: -10px
left: 67px
p:nth-child(9)
position: relative
top: -65px
left: 40px
function SimonGame () {
this.var = {};
this.var.start_cycles = -1;
this.var.round = 0;
this.var.strict = false;
this.var.start = false;
this.var.sequence = [];
this.var.user_sequence = 0;
this.audio = [ new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'),
new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'),
new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'),
new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3') ];
this.obj = {};
this.obj.btn = [ $('.green'), $('.red'), $('.yellow'), $('.blue') ];
this.obj.start = $('.start');
this.obj.strict = $('.strict');
this.obj.strictLed = $('.strict-led');
this.obj.round = $('.round-count');
this.obj.on = $('.on');
this.obj.off = $('.off');
/* functions */
var self = this;
this.on = function () {
self.var.start_cycles = 0;
var int_id = setInterval (function () {
self.obj.off.hide ();
self.obj.on.show ();
if ( self.var.start_cycles == 6 ) {
self.var.start_cycles = -1;
self.var.start_cycles = -1;
self.var.round = 0;
self.var.strict = false;
self.var.start = false;
self.var.sequence = [];
self.var.user_sequence = 0;
self.obj.start.css ({ 'background-color': 'red', 'cursor': 'pointer' });
self.obj.strict.css ({ 'background-color': 'yellow', 'cursor': 'pointer' });
self.obj.start.bind ('click', self.start);
self.obj.strict.bind ('click', function () {
self.var.strict = self.var.strict === false;
if ( self.var.strict )
self.obj.strictLed.css ({ 'background-color': 'red' });
else self.obj.strictLed.css ({ 'background-color': '#222' });
});
clearInterval (int_id);
return;
}
var color = ( self.var.start_cycles % 2 ) ? '#e00' : '#400';
self.obj.round.css ({ 'background-color': '#400', 'color': color });
self.var.start_cycles++;
}, 500);
};
this.off = function () {
var int_id = setInterval (function () {
if ( self.var.start_cycles == -1 ) {
self.obj.round.css ({ 'background-color': '#222', 'color': '#555' });
self.var.round = 0;
self.var.strict = false;
self.var.start = false;
self.var.sequence = [];
self.obj.on.hide ();
self.obj.off.show ();
self.obj.round.text ("--");
self.obj.strictLed.css ({ 'background-color': '#222' });
self.obj.start.css ({ 'background-color': '#555', 'cursor': 'default' });
self.obj.strict.css ({ 'background-color': '#555', 'cursor': 'default' });
self.obj.start.unbind ('click');
self.obj.strict.unbind ('click');
for ( var j = 0; j < 4; ++j ) {
self.obj.btn[j].unbind ('click');
self.obj.btn[j].css ({ 'cursor': 'default' });
self.obj.btn[j].removeClass ('color-active');
self.audio[j].pause ();
self.audio[j].currentTime = 0;
}
clearInterval (int_id);
}
}, 100);
};
this.start = function () {
self.var.sequence = [];
self.var.sequence.push (Math.round (Math.random () * 3));
self.var.round = 1;
for ( var j = 0; j < 4; ++j ) {
self.obj.btn[j].unbind ('click');
self.obj.btn[j].css ({ 'cursor': 'default' });
self.obj.btn[j].removeClass ('color-active');
self.audio[j].pause ();
self.audio[j].currentTime = 0;
}
self.play_sequence ();
};
this.display = function (n) {
if ( n < 10 ) return '0' + n;
return n;
};
this.play_sequence = function () {
self.obj.round.text (self.display (self.var.round));
var curr_sequence_pos = 0;
var int_id = setInterval (function () {
if ( curr_sequence_pos >= self.var.sequence.length ) {
if ( curr_sequence_pos == self.var.sequence.length )
self.obj.btn[self.var.sequence[curr_sequence_pos - 1]].removeClass ('color-active');
self.var.user_sequence = 0;
for ( var j = 0; j < 4; ++j ) {
self.obj.btn[j].css ({ 'cursor': 'pointer' });
self.obj.btn[j].bind ('click', self.btn_clicked);
}
clearInterval (int_id);
return;
}
if ( curr_sequence_pos > 0 ) {
self.obj.btn[self.var.sequence[curr_sequence_pos - 1]].removeClass ('color-active');
self.audio[self.var.sequence[curr_sequence_pos - 1]].pause ();
self.audio[self.var.sequence[curr_sequence_pos - 1]].currentTime = 0;
}
var curr_index = self.var.sequence[curr_sequence_pos];
setTimeout (function () {
self.audio[curr_index].play ();
self.obj.btn[curr_index].addClass ('color-active');
}, 100);
curr_sequence_pos++;
}, 700);
};
this.btn_clicked = function () {
var index = $(this).data ('index');
self.obj.btn[index].addClass ('color-active');
self.audio[index].play ();
setTimeout (function () {
self.obj.btn[index].removeClass ('color-active');
if ( self.var.sequence[self.var.user_sequence] != index ) {
self.var.user_sequence = 0;
for ( var j = 0; j < 4; ++j ) {
self.obj.btn[j].css ({'cursor': 'default'});
self.obj.btn[j].unbind ('click');
}
var blink = 0;
var int_id = setInterval (function () {
if ( blink == 7 ) {
if ( self.var.strict )
self.start ();
else self.play_sequence ();
self.obj.round.text (self.display (self.var.round));
clearInterval (int_id);
return;
}
self.obj.round.text ((blink % 2) ? '!!' : '--' );
blink++;
}, 500);
}
else {
self.var.user_sequence++;
if ( self.var.user_sequence == self.var.sequence.length ) {
self.var.user_sequence = 0;
for ( var j = 0; j < 4; ++j ) {
self.obj.btn[j].css ({'cursor': 'default'});
self.obj.btn[j].unbind ('click');
}
self.var.sequence.push (Math.round (Math.random () * 3));
self.var.round++;
if ( self.var.round > 20 ) {
var blink = 0;
var int_id = setInterval (function () {
if ( blink == 7 ) {
self.start ();
self.obj.round.text (self.display (self.var.round));
clearInterval (int_id);
return;
}
self.obj.round.text ((blink % 2) ? '**' : '--' );
blink++;
}, 500);
}
else self.play_sequence ();
}
}
}, 500);
};
/* events */
this.obj.off.click (this.on);
this.obj.on.click (this.off);
}
$(document).ready (function () {
var game = new SimonGame ();
});
Also see: Tab Triggers