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.
.decolines {
pointer-events: none;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
pointer-events: none;
}
.decolines--fixed {
position: fixed;
width: 100vw;
height: 100vh;
}
.decoline {
position: absolute;
}
/**
* main.js
* http://www.codrops.com
*
* Licensed under the MIT license.
* https://www.opensource.org/licenses/mit-license.php
*
* Copyright 2016, Codrops
* http://www.codrops.com
*/
;(function(window) {
'use strict';
// Helper vars and functions.
function extend( a, b ) {
for( var key in b ) {
if( b.hasOwnProperty( key ) ) {
a[key] = b[key];
}
}
return a;
}
/**
* Line obj.
*/
function Line(options) {
this.options = extend({}, this.options);
extend(this.options, options);
this._init();
}
Line.prototype.options = {
// top, left, width, height: numerical for pixels or string for % and viewport units. Examples: 2 || '20%' || '50vw'.
// color: the (bg)color of the line.
// hidden: defines if the line is rendered initially or hidden by default.
// animation: animation properties for the line
// duration: animation speed.
// easing: animation easing (animejs easing. To see all possible values console animejs.easings).
// delay: animation delay.
// direction: line animation direction. Possible values: TopBottom || BottomTop || LeftRight || RightLeft || CenterV || CenterH.
width: 1,
height: '100%',
left: '50%',
top: '0%',
color: '#000',
hidden: false,
animation: {
duration: 500,
easing: 'linear',
delay: 0,
direction: 'TopBottom'
}
};
/**
* Set style.
*/
Line.prototype._init = function() {
this.el = document.createElement('div');
this.el.className = 'decoline';
var opts = this.options;
this.el.style.width = typeof opts.width === 'number' ? opts.width + 'px' : opts.width;
this.el.style.height = typeof opts.height === 'number' ? opts.height + 'px' : opts.height;
this.el.style.left = typeof opts.left === 'number' ? opts.left + 'px' : opts.left;
this.el.style.top = typeof opts.top === 'number' ? opts.top + 'px' : opts.top;
this.el.style.background = opts.color || opts.color;
this.el.style.opacity = opts.hidden ? 0 : 1;
this._setOrigin();
this.rendered = !opts.hidden;
};
/**
* Transform origin is set according to the animation direction.
*/
Line.prototype._setOrigin = function() {
var opts = this.options, tOrigin = '50% 50%';
if( opts.animation.direction === 'TopBottom' ) {
tOrigin = '50% 0%';
}
else if( opts.animation.direction === 'BottomTop' ) {
tOrigin = '50% 100%';
}
else if( opts.animation.direction === 'LeftRight' ) {
tOrigin = '0% 50%';
}
else if( opts.animation.direction === 'RightLeft' ) {
tOrigin = '100% 50%';
}
this.el.style.WebkitTransformOrigin = this.el.style.transformOrigin = tOrigin;
};
/**
* Animates the line.
*/
Line.prototype.animate = function(settings) {
if( this.isAnimating ) {
return false;
}
this.isAnimating = true;
var animeProps = {
targets: this.el,
duration: settings && settings.duration != undefined ? settings.duration : this.options.animation.duration,
easing: settings && settings.easing != undefined ? settings.easing : this.options.animation.easing,
delay: settings && settings.delay != undefined ? settings.delay : this.options.animation.delay
};
if( settings && settings.direction ) {
this.options.animation.direction = settings.direction;
}
// Sets origin again. Settings might contain a different animation direction?
this._setOrigin();
if( this.options.animation.direction === 'TopBottom' || this.options.animation.direction === 'BottomTop' || this.options.animation.direction === 'CenterV' ) {
animeProps.scaleY = this.rendered ? [1, 0] : [0, 1];
}
else {
animeProps.scaleX = this.rendered ? [1, 0] : [0, 1];
}
if( !this.rendered ) {
this.el.style.opacity = 1;
}
var self = this;
animeProps.complete = function() {
self.rendered = !self.rendered;
self.isAnimating = false;
if( settings && settings.complete ) {
settings.complete();
}
}
anime(animeProps);
};
/**
* Show the line.
*/
Line.prototype.show = function() {
this.el.style.opacity = 1;
this.el.style.WebkitTransform = this.el.style.transform = 'scale3d(1,1,1)';
this.rendered = true;
};
/**
* Hide the line.
*/
Line.prototype.hide = function() {
this.el.style.opacity = 0;
this.rendered = false;
};
/**
* LineMaker obj.
*/
function LineMaker(options) {
this.options = extend({}, this.options);
extend(this.options, options);
this._init();
}
/**
* LineMaker options.
*/
LineMaker.prototype.options = {
// Where to insert the lines container.
// element: the DOM element or a string to specify the selector, e.g. '#id' or '.classname'.
// position: Whether to prepend or append to the parent.element
parent: {element: document.body, position: 'prepend'},
// position: if fixed the lines container will have fixed position.
position: 'absolute',
// The lines settings.
lines: []
};
/**
* Create the lines and its structure.
*/
LineMaker.prototype._init = function() {
this.lines = [];
this.decolines = document.createElement('div');
this.decolines.className = 'decolines';
if( this.options.position === 'fixed' ) {
this.decolines.className += ' decolines--fixed';
}
for(var i = 0, len = this.options.lines.length; i < len; ++i) {
var lineconfig = this.options.lines[i],
line = new Line(lineconfig);
this.decolines.appendChild(line.el);
this.lines.push(line);
}
var p = this.options.parent,
pEl = typeof p.element === 'string' ? document.querySelector(p.element) : p.element;
if( p.position === 'prepend' ) {
pEl.insertBefore(this.decolines, pEl.firstChild);
}
else {
pEl.appendChild(this.decolines);
}
};
/**
* Shows/Hides one line with an animation.
*/
LineMaker.prototype._animateLine = function(lineIdx, dir, settings) {
var line = this.lines[lineIdx];
if( line && dir === 'in' && !line.rendered || dir === 'out' && line.rendered ) {
line.animate(settings);
}
};
/**
* Shows/Hides all lines with an animation.
*/
LineMaker.prototype._animateLines = function(dir, callback) {
var completed = 0, totalLines = this.lines.length;
if( totalLines === 0 ) {
callback();
return;
}
var checkCompleted = function() {
completed++;
if( completed === totalLines && typeof callback === 'function' ) {
callback();
}
};
for(var i = 0; i < totalLines; ++i) {
var line = this.lines[i];
if( dir === 'in' && !line.rendered || dir === 'out' && line.rendered ) {
line.animate({
complete: function() {
checkCompleted();
}
});
}
else {
checkCompleted();
}
}
};
/**
* Shows/Hides one line.
*/
LineMaker.prototype._toggleLine = function(lineIdx, action) {
var line = this.lines[lineIdx];
if( !line ) { return; }
if( action === 'show' && !line.rendered ) {
console.log('toggle')
line.show();
}
else if( action === 'hide' && line.rendered ) {
line.hide();
}
};
/**
* Shows/Hides all lines.
*/
LineMaker.prototype._toggleLines = function(action) {
for(var i = 0, len = this.lines.length; i < len; ++i) {
this._toggleLine(i, action);
}
};
/**
* Shows one line with an animation.
* lineIndex: index/position of the line in the LineMaker.options.lines array.
* animationSettings is optional: if not passed, the animation settings defined in LineMaker.options.lines for each line will be used.
*/
LineMaker.prototype.animateLineIn = function(lineIdx, settings) {
this._animateLine(lineIdx, 'in', settings);
};
/**
* Hides one line with an animation.
* lineIndex: index/position of the line in the LineMaker.options.lines array.
* animationSettings is optional: if not passed, the animation settings defined in LineMaker.options.lines for each line will be used.
*/
LineMaker.prototype.animateLineOut = function(lineIdx, settings) {
this._animateLine(lineIdx, 'out', settings);
};
/**
* Shows all lines with an animation.
*/
LineMaker.prototype.animateLinesIn = function(callback) {
this._animateLines('in', callback);
};
/**
* Hides all lines with an animation.
*/
LineMaker.prototype.animateLinesOut = function(callback) {
this._animateLines('out', callback);
};
/**
* Shows one line.
* lineIndex: index/position of the line in the LineMaker.options.lines array.
*/
LineMaker.prototype.showLine = function(lineIdx) {
this._toggleLine(lineIdx, 'show');
};
/**
* Hides one line.
* lineIndex: index/position of the line in the LineMaker.options.lines array.
*/
LineMaker.prototype.hideLine = function(lineIdx) {
this._toggleLine(lineIdx, 'hide');
};
/**
* Shows all lines.
*/
LineMaker.prototype.showLines = function() {
this._toggleLines('show');
};
/**
* Hides all lines.
*/
LineMaker.prototype.hideLines = function() {
this._toggleLines('hide');
};
/**
* Removes a line.
* lineIndex: index/position of the line in the LineMaker.options.lines array.
*/
LineMaker.prototype.removeLine = function(lineIdx) {
var line = this.lines[lineIdx];
if( line ) {
this.lines.splice(lineIdx, 1);
this.decolines.removeChild(this.decolines.children[lineIdx]);
}
};
/**
* Removes all lines.
*/
LineMaker.prototype.removeLines = function() {
this.lines = [];
this.decolines.innerHTML = '';
};
/**
* Creates a line.
* settings is optional: same settings passed in LineMaker.options.lines for one line.
*/
LineMaker.prototype.createLine = function(settings) {
var line = new Line(settings);
this.decolines.appendChild(line.el);
this.lines.push(line);
};
/**
* Returns the total number of lines.
*/
LineMaker.prototype.getTotalLines = function() {
return this.lines.length;
}
window.LineMaker = LineMaker;
var lineMaker = new LineMaker({
position: 'fixed',
lines: [
{top: 0, left: '10%', width: 3, height: '100vh', color: '#000', animation: { duration: 2000, easing: 'easeInOutExpo', delay: 0, direction: 'TopBottom' }},
{top: 0, left: '30%', width: 10, height: '100vh', color: 'red', animation: { duration: 2000, easing: 'easeInOutExpo', delay: 0, direction: 'TopBottom' }},
{top: 0, left: '50%', width: 3, height: '100vh', color: '#992211', animation: { duration: 2000, easing: 'easeInOutExpo', delay: 0, direction: 'TopBottom' }},
{top: 0, left: '70%', width: 3, height: '100vh', color: '#11aa00', animation: { duration: 2000, easing: 'easeInOutExpo', delay: 0, direction: 'TopBottom' }},
{top: 0, left: '90%', width: 3, height: '100vh', color: '#0033ff', animation: { duration: 2000, easing: 'easeInOutExpo', delay: 0, direction: 'TopBottom' }},
]
});
setTimeout(function() {
// Animate second line out (animation settings are the ones defined on the initialization)
lineMaker.animateLineOut(1, {
complete: function() {
// show line again
lineMaker.showLine(1);
// Animate second line out (animation settings are the ones passed in the second argument)
lineMaker.animateLineOut(1, {
duration: 600,
easing: 'easeOutExpo',
delay: 700,
direction: 'BottomTop'
});
}
});
}, 2000);
})(window);
Also see: Tab Triggers