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.
<button id="btn1">Add images</button> <button id="btn2">Reset Container</button>
<div id="imgContainer"></div>
<div id="progressBar"></div>
<canvas id="canvas" width="220" height="220"></canvas>
<div id="percentageContainer">
<div id="percentageLoad">0 %</div>
</div>
<div id="spinnerContainer">
<div class="spinDot"></div>
<div class="spinDot"></div>
<div class="spinDot"></div>
<div class="spinDot"></div>
<div class="spinDot"></div>
<div class="spinDot"></div>
<div class="spinDot"></div>
<div class="spinDot"></div>
<div class="spinDot"></div>
</div>
html
{
height:100%;
}
body
{
margin:0;
padding:0;
background : -webkit-linear-gradient(top, rgb(0, 0, 0) 00%, rgb(200, 200, 200) 100%);
background : -moz-linear-gradient(top, rgb(0, 0, 0) 00%, rgb(200, 200, 200) 100%);
background : -o-linear-gradient(top, rgb(0, 0, 0) 00%, rgb(200, 200, 200) 100%);
background : -ms-linear-gradient(top, rgb(0, 0, 0) 00%, rgb(200, 200, 200) 100%);
background : -linear-gradient(top, rgb(0, 0, 0) 00%, rgb(200, 200, 200) 100%);
background-repeat: no-repeat;
background-attachment: fixed;
}
/* IMAGES STYLE */
#imgContainer
{
text-align:center;
width:80%;
margin-left:10%;
top:50px;
border:solid 1px #fff;
padding:10px;
position:relative;
overflow:hidden;
}
#imgContainer:after
{
content:"";
clear:both;
display:table;
}
#imgContainer img
{
position:relative;
top:-150px;
margin-left:10px;
border:solid 2px #FF9;
}
#imgContainer img:first-child
{
margin-left:0;
}
/*---------------------------------------------------------------------
/* CANVAS */
#canvas
{
position:relative;
margin-top:100px;
left:50%;
margin-left:-110px;
}
/*---------------------------------------------------------------------
/* PERCENTAGE STYLE */
#percentageContainer
{
position:relative;
top:-150px;
text-align:center;
}
#percentageLoad
{
padding:5px 15px;
border:solid 1px;
border-radius:5px;
background:#000;
font:44px bold "Trebuchet MS", Arial, Helvetica, sans-serif;
color:#fff;
display:inline-block;
}
/*---------------------------------------------------------------------
/* SPINNER STYLE */
#spinnerContainer
{
top:-135px;
left:50%;
margin-left:-107.5px;
position:relative;
width:215px;
height:50px;
text-align:center;
}
.spinDot
{
width:15px;
height:15px;
position:absolute;
background:#000;
border-radius:50%;
top:100px;
left:100px;
}
/*---------------------------------------------------------------------
/* BUTTONS STYLE */
button
{
padding:5px 10px;
margin:10px;
border-radius:5px;
background : -webkit-linear-gradient(top, rgb(255, 255, 255) 0%, rgb(200, 200, 200) 100%);
background : -moz-linear-gradient(top, rgb(255, 255, 255) 0%, rgb(200, 200, 200) 100%);
background : -o-linear-gradient(top, rgb(255, 255, 255) 0%, rgb(200, 200, 200) 100%);
background : -ms-linear-gradient(top, rgb(255, 255, 255) 0%, rgb(200, 200, 200) 100%);
background : -linear-gradient(top, rgb(255, 255, 255) 0%, rgb(200, 200, 200) 100%);
border:1px solid #646464;
}
button:hover
{
background : -webkit-linear-gradient(top, rgb(200, 200, 200) 0%, rgb(255, 255, 255) 100%);
background : -moz-linear-gradient(top, rgb(200, 200, 200) 0%, rgb(255, 255, 255) 100%);
background : -o-linear-gradient(top, rgb(200, 200, 200) 0%, rgb(255, 255, 255) 100%);
background : -ms-linear-gradient(top, rgb(200, 200, 200) 0%, rgb(255, 255, 255) 100%);
background : -linear-gradient(top, rgb(200, 200, 200) 0%, rgb(255, 255, 255) 100%);
}
button:active
{
position:relative;
top:1px;
}
/*---------------------------------------------------------------------
/* PROGRESS BAR STYLE */
#progressBar
{
width:100%;
height:5px;
background:#fff;
position:relative;
margin-top:60px;
margin-bottom:-90px;
}
var imgContainer = $("#imgContainer"),
progressBar = $("#progressBar"),
btn1 = $("#btn1"),
btn2 = $("#btn2"),
//images variables
imagesAmount = 6,//total images being loaded
loadedCount = 0,//current number of images loaded
currentImages,//images already present in the container
//we get the full width of the progress bar it depens on display width
progBarWidth = progressBar.outerWidth(),
//how much progress every image means
progressUnit = 1 / imagesAmount,
currentProgress,//the current progress of the total images being downloaded
percentageLoaded;//total percent of images already loaded
/*
---------------------------------------------------------------------
ADD IMAGES CODE
---------------------------------------------------------------------
*/
var getWidth = gsap.utils.random(100, 150, 5, true);
function addImages() {
//show the percentage loaded text
gsap.to([percentageContainer,percentageLoad], {
scale:1,
opacity:1
});
for(var i = 0; i < imagesAmount; i++) {
//this vars are for getting random images
//for real case scenario this is not needed. different approach for that type
var imgUrl = "https://source.unsplash.com/random/300x300&" + i + "/?forest";
var imgString = `<img src=${imgUrl} height="100" />`;
//add the image at the beggining of the container
imgContainer.prepend(imgString);
}
imgContainer.imagesLoaded()//we set up the images loaded instance to work in the image container
.progress(loadProgress);//we add the deferred call to the images loaded function
}
//------ADD IMAGES CODE END----------------------------------------//
/*
---------------------------------------------------------------------
CANVAS CIRCLE CODE
---------------------------------------------------------------------
*/
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),//circle context
segmentObj = {seg:0},
totalSegments = 150;//number of circle segments, more segments smoother animation
//canvas initial set up
ctx.strokeStyle = 'rgb(0,0,200)';
ctx.lineWidth = 5;
ctx.lineCap = 'round';
//canvas works with radians, but we're feeding degrees so they need to be converted before they're used
function getRadians(degreesValue) {
var radiansValue = (Math.PI / 180) * degreesValue;
return radiansValue;
}
//function to draw the arc/circle
//the params are start angle, end angle and boolean for clokwise or counter clockwise
function manualCircle(start, end, clock) {
ctx.clearRect(0,0,220,220);//we have to clear the canvas otherwise the line looks horrible
ctx.beginPath();
ctx.arc(110, 110, 100, start, end, clock);
ctx.stroke();
}
//update arc/circle drawing
function circleUpdate() {
//degrees of the arc, could be less, in this case we create a full circle
var amplitude = 360,
step = amplitude / totalSegments,
endAngle = (step * (segmentObj.seg + 1));//the new angle for the next step in the arc
//draw the new arc
manualCircle(-getRadians(0), -getRadians(endAngle), true);
}
//------CANVAS CIRCLE CODE END-------------------------------------//
/*
---------------------------------------------------------------------
PROGRESS CODE
---------------------------------------------------------------------
*/
//this function executes everytime an image is complete
function loadProgress(imgLoad, image) {
//one more image has been loaded. this counts even for the images already loaded
loadedCount++;
//we put the image in correct position
gsap.to(image.img, { top: 0 });
//since there could be other images in the container we check that
//with this the progress bar and percentage loaded are updated by the
//number of images being loaded and not the total in the container
if( (loadedCount - currentImages.length) > 0 ) {
currentProgress = progressUnit * (loadedCount - currentImages.length);
gsap.to(progressTl, {
duration: 0.7,
progress: currentProgress,
ease: "none"
});
}
}
//progress animation instance. the instance's time is irrelevant, can be anything but 0 to void immediate render
var progressTl = gsap.timeline({
paused:true,
onUpdate:progressUpdate,
onComplete:loadComplete,
defaults: {
duration: 1,
ease: "none",
},
});
progressTl
//tween the progress bar width
.to(progressBar, { width:progBarWidth, })
//tween the amount of circle segments to set the cricle new angle
.to(segmentObj, { seg:totalSegments, snap:'seg', }, 0);
//as the progress bar witdh updates and grows we put the precentage loaded in the screen
function progressUpdate() {
//the percentage loaded based on the tween's progress
percentageLoaded = Math.round(progressTl.progress() * 100);
//we put the percentage in the screen
$("#percentageLoad").html(percentageLoaded + ' %');
circleUpdate();
}
//this function resets progress bar width, set progress and percentage loaded to 0
function resetProgress() {
//set the progress bar to width 0, no progress
gsap.set(progressBar, { width:0 });
progressTl.progress(0);
percentageLoaded = 0;
loadedCount = 0;
}
//------PROGRESS CODE END------------------------------------------//
/*
---------------------------------------------------------------------
BUTTONS EVENTS
---------------------------------------------------------------------
*/
btn1.click(function() {
resetProgress();//reset the progress
currentImages = imgContainer.find("img");//how many images there are
addImages();//run the function to add the images
setupSipnner();//play the spinner line
});
btn2.click(function() {
resetProgress();//reset the progress
imgContainer.empty();//remove the images from the container
});
//------BUTTONS EVENTS END-----------------------------------------//
/*
---------------------------------------------------------------------
SPINNER CODE
---------------------------------------------------------------------
*/
//spinner variables
var spinContainer = $("#spinnerContainer"),
spinElements = $(".spinDot"),
spinAngleSet = 360 / spinElements.length,
spinSpeed = 0.055,//smaller the number faster the spin
spinLine = gsap.timeline({ paused:true, repeat:-1, delay:.55 });
gsap.set(spinContainer, { perspective:300 });
function setupSipnner() {
gsap.set(spinElements, {opacity:1});
$.each(spinElements, function(index, element) {
var alpha = (0.1 * index) + .3;
gsap.to(element, {
rotationY: (( index + 1 ) * spinAngleSet),
transformOrigin: '50% 50% -100px',
autoAlpha: alpha,
});
spinLine.set(spinElements, {
rotationY:('+=' + spinAngleSet),
transformOrigin:'50% 50% -100px',
ease: "none",
delay: spinSpeed,
});
});
spinLine.play();//play the spinner line
}
//------SPINNER CODE END-------------------------------------------//
/*
---------------------------------------------------------------------
LOAD COMPLETE CODE
---------------------------------------------------------------------
*/
//load completes
function loadComplete() {
spinLine.pause();
gsap.to(spinElements, { rotationY: 0 });
gsap.to(spinElements, { opacity: 0, delay: 0.5, duration: 0.25});
spinLine.seek(0);
gsap.to([percentageContainer,percentageLoad], {
scale:0, opacity:0, delay:.5
});
}
//------LOAD COMPLETE CODE END-------------------------------------//
Also see: Tab Triggers