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.
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="wrapper">
<p>circle.timeline = </p>
<textarea id="input_field" oninput="setTimelineInput(this.value)" onchange="setTimelineInput(this.value)">
[{"key":0 , "x": 0 , "y":0 },
{"key":0.20, "x": 100, "y":100 },
{"key":0.40, "x": 200, "y":0 },
{"key":0.60, "x": 300, "y":200 },
{"key":0.80, "x": 300, "y":100 },
{"key":1.00, "x": 200, "y":100}]
</textarea>
<div class="box">
<div id="circle"><span></span></div>
</div>
<p id="output_field">0</p>
<input id="range" type="range" min="0" max="1000" value="0" oninput="update(this.value)" onchange="update(this.value)">
</div>
</body>
</html>
body{
background-color:#fff;
margin:0;
padding:0;
}
.wrapper{
font-family:"Tahoma";
max-width:300px;
margin:0 auto;
padding:30px 5px 30px 5px;
}
p{
margin:5px 0 5px 0;
font-size:14px;
}
textarea{
width:100%;
padding:5px;
box-sizing: border-box;
min-height:140px;
line-height:16px;
border:1px solid #ccc;
border-radius:3px;
background-color:#f4f4f4;
margin-bottom:10px;
}
.box{
width:100%;
height:200px;
background-image:url(https://www.isotopic.com.br/wordpress/wp-content/uploads/2015/11/zIYXCZv.png);
background-repeat:no-repeat;
margin:0 auto;
}
#circle{
width:10px;height:10px;
transform:translate(0px,0px);
}
#circle span{
display:block;
width:10px;height:10px;
border-radius:50%;
border:4px solid #6699EE;
background-color:#fff;
transform:translate(-8px,-8px);
}
#range{
width:90%;
display:block;
margin: 0 auto;
}
#output_field{
text-align:center;
}
/**
* Author: isotopic.com.br
* Scroll based animation following a 'keyframed timeline' approach.
* All animation keyframes are stored in a json file.
* Movement is triggered by any continuous input (like scroll, slider, etc).
* In a real use case it'd have a lot more verifications. This is just to show the concept.
*/
// Sample object to be animated
var circle = document.getElementById("circle");
circle.timeline = {};
// Input
var input_field = document.getElementById("input_field");
// Output
var output_field = document.getElementById("output_field");
// In this example, json data comes from the input field
function setTimelineInput(json_data){
circle.timeline = JSON.parse(json_data);
}
setTimelineInput(input_field.value);
// In this example, update is fired by the slider event - usually it'd be triggered by scrolling the page.
function update(range_value){
// 'ratio' corresponds to the animation state, from 0 (start) to 1 (end).
var ratio = range_value / 1000;
var keyframes = [];
var local_ratio = 0;
output_field.innerHTML = ratio;
// Loop through all keyframes available for this object
for(i=0;i<circle.timeline.length-1;i++){
// When ratio's value is between two keyframes, it must affect the object's properties
if(ratio >= circle.timeline[i].key && ratio <= circle.timeline[i+1].key){
keyframes[0] = circle.timeline[i];
keyframes[1] = circle.timeline[i+1];
}
if(keyframes.length==2){
// Normalizes the global ratio to a local ratio (the ratio between two keyframes)
local_ratio = (ratio - keyframes[0].key) / (keyframes[1].key - keyframes[0].key);
// A little easing
local_ratio = easeInOutQuad(local_ratio);
// The calculated position will be the initial position, plus the percent of the possible variation between frames
x = (keyframes[0].x + local_ratio*(keyframes[1].x -
keyframes[0].x)); y = (keyframes[0].y + local_ratio*(keyframes[1].y
- keyframes[0].y));
// What a mess
circle.style['-webkit-transform'] = "translate( "+x+"px, "+y+"px)";
circle.style['-ms-transform'] = "translate( "+x+"px, "+y+"px)";
circle.style['transform'] = "translate( "+x+"px, "+y+"px)";
}
}
}
//Robert Penner's simplified to just one input
function easeInOutQuad(t) {
t *= 2;
if (t < 1) return t*t /2;
t--;
return - ( t*(t-2) - 1 ) /2;
}
Also see: Tab Triggers