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.
// DOM Based Actors for GreenSock Animation Platform
// https://greensock.com/forums/topic/11661-textplugin-and-superscript/?p=47595
// DOCUMENT READY
// Classes aren't hoisted, so you need to let them be evaluted first
document.addEventListener("DOMContentLoaded", () => {
// STYLES
// You can apply these styles to other actors
// by adding them to their style property
var flexStyle = {
display: "flex",
alignItems: "center",
justifyContent: "center",
position: "absolute",
overflow: "hidden"
};
var textStyle = {
color: "white",
fontWeight: 400,
display: "inline-block",
textAlign: "center",
letterSpacing: -0.2,
lineHeight: 1,
fontFamily: "Open Sans",
textRendering: "optimizeLegibility",
top: 0,
};
// STAGE
// This is where all the action takes place
// If you don't create it, one will be created automatically
var stage = new Stage({
background: "black",
padding: "25px",
style: { overflow: "auto" }
});
// SMALL SHOP CONTAINER
var container = new Actor()
.setPosition(25, 90)
.setPercentage(0, 0)
.set("background", "black")
.set(flexStyle);
var text = new Text({
text: "SHOP SMALL",
fontSize: 30,
style: textStyle
});
var regMark = new Text({
text: "®",
element: "sup",
fontSize: 11,
padding: 3,
verticalAlign: "top",
style: textStyle
});
var content = new Actor()
.set("padding", 6)
.addTo(container)
.addChildren(text, regMark);
// PLAY BUTTON
var button = new Button({
text: "PLAY",
fontWeight: "bold",
padding: "10px 16px",
background: "yellowgreen",
border: "0px solid transparent",
onClick: onClick
});
function onClick() {
var tl = new TimelineMax({
repeat: 1,
repeatDelay: 0.7,
yoyo: true,
onStart: () => this.disabled = true,
onComplete: () => this.disabled = false
});
tl.to(this.target, 0.5, { opacity: 0.4 }, 0)
.to(container.target, 1, { width: 500, height: 300, left: "50%", top: "50%" }, 0)
.to(container.target, 1, { x: 0, y: 0, xPercent: -50, yPercent: -50 }, 0)
.to(container.target, 1, { backgroundColor: "#555" }, 0)
.to(text.target, 1, { text: { value: "SPEND BIG", padSpace: true } }, 0);
}
});
// ========================================================================
// ACTOR
// ========================================================================
class Actor {
constructor(config = {}) {
var names = "background,border,display,height,width,padding,margin,position,x,y";
var props = Actor.concat(names, config.styleProps);
var style = _.defaults(config.style || {}, {
boxSizing : "border-box",
display : "block",
position : "relative",
x: 0
});
_.defaults(config, { element: "div", type: "actor" });
_.assign(config, { style: style, styleProps: props });
this.actorId = _.uniqueId();
this.config = config;
this.timeline = new TimelineMax({ autoRemoveChildren: true });
this.create(config);
}
create(config) {
// Set the parent, which defaults to the stage if not provided
this.parent = config.parent || Stage.instance || new Stage();
// Create the element
this.type = config.type;
this.target = document.createElement(config.element);
this.target.id = _.kebabCase(config.id || this.type + "-" + this.actorId);
// The stage does not have a parent actor
if (this.parent !== document.body) this.parent.addChild(this);
// Get style property values from config
var style = _.pick(config, config.styleProps);
// Create getter/setter for properties and set style
this.createStyleProps.apply(this, config.styleProps);
this.set(_.defaults(style, config.style));
// GSAP transform values
this.transform = this.target._gsTransform;
return this;
}
addChild(child) {
if (this.target === child.target) return this;
this.target.appendChild(child.target);
child.parent = this;
return this;
}
addChildren(...children) {
children.forEach(child => this.addChild(child));
return this;
}
addTo(parent) {
parent.addChild(this);
return this;
}
getStyleValue(name) {
var value = window.getComputedStyle(this.target, null).getPropertyValue(name);
return this.transform[name] || value;
}
createStyleProps(...names) {
names.forEach(name => {
Object.defineProperty(this, name, {
get: () => this.getStyleValue(name),
set: value => this.set({ [name]: value }),
});
});
return this;
}
set(vars, value) {
if (_.isString(vars) && value) vars = { [vars]: value };
TweenLite.set(this.target, vars);
return this;
}
setPercentage(x, y) {
this.set({ left: x + "%", top: y + "%", xPercent: -x, yPercent: -y });
return this;
}
setPosition(x, y) {
this.set({ x, y });
return this;
}
setSize(width, height) {
this.set({ width, height });
return this;
}
to(duration, vars, label) {
this.timeline.to(this.target, duration, vars, label);
return this;
}
// Splits 2 strings up and concatenates them into an array
static concat(prop1 = [], prop2 = []) {
if (_.isString(prop1)) prop1 = prop1.split(",");
if (_.isString(prop2)) prop2 = prop2.split(",");
return prop1.concat(prop2);
}
}
// ========================================================================
// STAGE
// ========================================================================
class Stage extends Actor {
constructor(config = {}) {
// Only allow one stage to be created
if (Stage.instance) return Stage.instance;
_.defaults(config, {
height : "100vh",
width : "100vw",
element : "main",
id : "stage",
type : "stage"
});
_.assign(config, { parent: document.body });
super(config);
// Set static stage property
Stage.instance = this;
// Make the stage the first element
document.body.insertBefore(this.target, document.body.firstChild);
}
}
// ========================================================================
// TEXT
// ========================================================================
class Text extends Actor {
constructor(config = {}) {
var names = "color,fontSize,fontWeight,fontFamily,textAlign,verticalAlign";
var props = Text.concat(names, config.styleProps);
_.defaults(config, { type: "text" });
_.assign(config, { styleProps: props });
super(config);
}
create(config) {
super.create(config);
// Set text value
this.text = config.text;
}
get text() { return this.target.textContent; }
set text(text) { this.set({ text });
}
}
// ========================================================================
// BUTTON
// ========================================================================
class Button extends Text {
constructor(config = {}) {
_.defaults(config, {
element : "button",
disabled : false,
text : "",
type : "button",
display : "inline-block",
});
super(config);
}
create(config) {
super.create(config);
// Set disabled attribute
this.disabled = config.disabled;
// Setup click event
this.target.addEventListener("click", event => {
var callback = config.onClick;
var scope = config.onClickScope || this;
var params = config.onClickParams || [event];
if (_.isFunction(callback)) callback.apply(scope, params);
});
}
get disabled() { return this.target.hasAttribute("disabled"); }
set disabled(flag) { flag
? this.target.setAttribute("disabled", "")
: this.target.removeAttribute("disabled");
}
}
Also see: Tab Triggers