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.
.flex-center.my-container
.main-box
-for(i=1; i<9; i++)
.button
.flex-center.transition.btn-child= i
-for(j=1; j<9; j++)
.flex-center.empty= j
.end-game
h1 Congratulation
p You've Ended game successfully
button.transition.again Play Again
p.more For more pen
a(href="https://codepen.io/wall-e",target="_blank")
|click here
//calculations for width & height
$btn: 76px
$btnBorder: 3px
$btnPad: 5px
$period: $btn + 2 * $btnBorder + $btnPad * 2
$box: 3 * $period
//colors
$boxBg: #03a9f4
$btnBg: #560d0d
$bodyBg: #e88200
$paddingCol: #b8afff
$txtCol: #ff0
*
-webkit-box-sizing: border-box
-moz-box-sizing: border-box
box-sizing: border-box
body
margin: 0
background: $bodyBg
text-align: center
.flex-center
display: -webkit-box
display: -ms-flexbox
display: flex
align-items: center
justify-content: center
.my-container
align-items: center
justify-content: center
min-height: 95vh
.main-box
display: inline-block
width: $box
height: $box
max-width: 100%
position: relative
background: $boxBg
.empty
height: $period
width: $period
position: absolute
.transition
-webkit-transition: all 0.2s linear
-moz-transition: all 0.2s linear
-ms-transition: all 0.2s linear
-o-transition: all 0.2s linear
transition: all 0.2s linear
.button
background: $paddingCol
border: $btnBorder solid $boxBg
padding: $btnPad
position: absolute
cursor: pointer
z-index: 2
.button:hover .btn-child
background: $txtCol
color: $btnBg
box-shadow: 0 0 10px $btnBg
.btn-child
height: $btn
width: $btn
background: $btnBg
color: $txtCol
.btn-child, .empty
font-size: $btn / 2.5
font-weight: bold
-webkit-user-select: none
user-select: none
@for $i from 1 through 9
.button:nth-child(#{$i})
left: $period * (($i - 1)%3)
.empty:nth-child(#{$i+8})
left: $period * (($i - 1)%3)
.end-game
display: none
.again
background: $boxBg / 1.2
color: $btnBg / 1.02
font-weight: bold
border: 2px solid $btnBg
padding: 5px 15px
.again:hover
background: $boxBg / 2
color: $btnBg * 2.5
border-color: $btnBg * 1.5
.more
align-self: flex-end
position: absolute
padding-bottom: 10px
$(document).ready(function(){
var card = $('.button');
var index = 0;
var emptyPosTop;
var emptyPosLeft;
var pos = new Array (9);
card.each(function(){
var topVal = card.outerWidth()*i(index/3);
var t = $(this);
pos[index] = new Array(2);
//set top position of each button (left position is set by sass)
$(this).css( 'top', topVal )//.children('div').html(rn[index]);
pos[index] = [f(t.css('top')), f(t.css('left'))]; //saving positions
index++;
});
emptyPosTop = f( card.outerWidth()*parseInt(index/3) );
emptyPosLeft = f($('.button:nth-child(' + index + ')').css('left')) + f(card.outerWidth());
pos[index] = [emptyPosTop, emptyPosLeft];
index = 0;
$('.empty').each(function(){
$(this).css('top', card.outerWidth()*i(index/3));
index++;
});
Shuffle ();
card.bind('click', ClickFunc);
$('.again').click(function(){
card.bind('click', ClickFunc);
$('.end-game').css('display', 'none');
card.removeClass('transition'); //removing transition before shuffling
Shuffle ();
});
// shuffle to get a valid shuffled board
function Shuffle ()
{
for(var i=0; i<100; i++)
{
var possibleMovingCard = [];
for(var j=1; j<9; j++)
{
var b = $( NthChild(j) );
if(IsValidCard( f(b.css('top')), f(b.css('left')), [emptyPosTop, emptyPosLeft] ))
{
possibleMovingCard.push( NthChild(j) );
}
}
var rndm = RandomInt(0,possibleMovingCard.length-1);
var movingCard = $(possibleMovingCard[rndm]);
var temp = [f(movingCard.css('top')), f(movingCard.css('left'))];
//moving to empty position
movingCard.css({
'top' : emptyPosTop,
'left' : emptyPosLeft
});
emptyPosTop = temp[0];
emptyPosLeft = temp[1];
}
//adding transition at first will break some card's positions
card.addClass('transition');
}
//click function
function ClickFunc ()
{
Swap ($(this), [emptyPosTop, emptyPosLeft]);
setTimeout(function() { //calls click event after a certain time
CheckGame();
}, 1000);
}
//move card to empty position
function Swap (_card, ep) //selected card, emptyposition
{
var thisTop = f(_card.css('top'));
var thisLeft = f(_card.css('left'));
if(IsValidCard(thisTop, thisLeft, ep))
{
var temp = [ thisTop, thisLeft ];
_card.css({ //go to empty position
'top' : ep[0],
'left' : ep[1]
});
//save the new empty position
emptyPosTop = temp[0];
emptyPosLeft = temp[1];
}
}
//check the card is valid to move or not
function IsValidCard (ct, cl, e) //card top, card left, empty position
{
return (
//top to bottom & bottom to top
Math.abs(e[0] - ct) == card.outerWidth() &&
cl == e[1]
) || (
// left to right & right to bottom
ct == e[0] &&
Math.abs(e[1] - cl) == card.outerWidth()
)
}
//check game is ended or not
function CheckGame ()
{
var count = 0;
for(var i=0; i<8; i++)
{
var b = $ (NthChild(i+1) );
if(
f(b.css('top')) == pos[i][0] &&
f(b.css('left')) == pos[i][1]
)
{
count++;
}
else
{
count = 0;
return;
}
}
if(count == 8)
EndGame ();
}
//after ending game
function EndGame ()
{
$('.button').unbind( "click" );
$('.end-game').css('display', 'block');
}
//nth child of button class as string
function NthChild (num)
{
return '.button:nth-child(' + num + ')';
}
//parse to integer
function i (param)
{
return parseInt(param);
}
//parse to float
function f (param)
{
return parseFloat(param);
}
//random integer number
function RandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
});
Also see: Tab Triggers