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.
<div class="wrapper">
<h1>ScrollMe playpen</h1>
<p>Info and docs: <a href="http://scrollme.nckprsn.com">http://scrollme.nckprsn.com</a></p>
<p>Download: <a href="https://github.com/nckprsn/scrollme">https://github.com/nckprsn/scrollme</a></p>
<p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p><p>↓</p>
<div class="scrollme">
<h2>Scroll me!</h2>
<div
class="animateme"
data-when="span"
data-from="0"
data-to="1"
data-easing="easeinout"
data-rotatey="360"
>
<h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2><h2>Animate me!</h2>
</div>
</div>
<p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p><p>↑</p>
</div>
* { text-align:center; }
.wrapper
{
max-width:30em;
margin:2em auto;
}
.scrollme
{
padding:2em;
border:1em solid #e8e8e8;
perspective:600px;
}
.animateme
{
width:80%;
padding:10%;
background:#e8e8e8;
transform-origin:center center center;
}
// ----------------------------------------------------------------------------------------------------
// ScrollMe
// A jQuery plugin for adding simple scrolling effects to web pages
// http://scrollme.nckprsn.com
// ----------------------------------------------------------------------------------------------------
var scrollme = ( function( $ )
{
// ----------------------------------------------------------------------------------------------------
// ScrollMe object
var _this = {};
// ----------------------------------------------------------------------------------------------------
// Properties
var $document = $( document );
var $window = $( window );
_this.body_height = 0;
_this.viewport_height = 0;
_this.viewport_top = 0;
_this.viewport_bottom = 0;
_this.viewport_top_previous = -1;
_this.elements = [];
_this.elements_in_view = [];
_this.property_defaults =
{
'opacity' : 1,
'translatex' : 0,
'translatey' : 0,
'translatez' : 0,
'rotatex' : 0,
'rotatey' : 0,
'rotatez' : 0,
'scale' : 1,
'scalex' : 1,
'scaley' : 1,
'scalez' : 1
};
_this.scrollme_selector = '.scrollme';
_this.animateme_selector = '.animateme';
_this.update_interval = 10;
// Easing functions
_this.easing_functions =
{
'linear' : function( x )
{
return x;
},
'easeout' : function( x )
{
return x * x * x;
},
'easein' : function( x )
{
x = 1 - x;
return 1 - ( x * x * x );
},
'easeinout' : function( x )
{
if( x < 0.5 )
{
return ( 4 * x * x * x );
}
else
{
x = 1 - x;
return 1 - ( 4 * x * x * x ) ;
}
}
};
// Document events to bind initialisation to
_this.init_events =
[
'ready',
'page:load', // Turbolinks
'page:change' // Turbolinks
];
// ----------------------------------------------------------------------------------------------------
// Initialisation conditions
_this.init_if = function() { return true; }
// ----------------------------------------------------------------------------------------------------
// Initialisation
_this.init = function()
{
// Cancel if initialisation conditions not met
if( !_this.init_if() ) return false;
// Load all elements to animate
_this.init_elements();
// Get element & viewport sizes
_this.on_resize();
// Recalculate heights & positions on resize and rotate
$window.on( 'resize orientationchange' , function(){ _this.on_resize(); } );
// Recalculate heights & positions when page is fully loaded + a bit just in case
$window.load( function(){ setTimeout( function(){ _this.on_resize(); } , 100 ) });
// Start animating
setInterval( _this.update , _this.update_interval );
return true;
}
// ----------------------------------------------------------------------------------------------------
// Get list and pre-load animated elements
_this.init_elements = function()
{
// For each reference element
$( _this.scrollme_selector ).each( function()
{
var element = {};
element.element = $( this );
var effects = [];
// For each animated element
$( this ).find( _this.animateme_selector ).addBack( _this.animateme_selector ).each( function()
{
// Get effect details
var effect = {};
effect.element = $( this );
effect.when = effect.element.data( 'when' );
effect.from = effect.element.data( 'from' );
effect.to = effect.element.data( 'to' );
if( effect.element.is( '[data-crop]' ) )
{
effect.crop = effect.element.data( 'crop' );
}
else
{
effect.crop = true;
}
if( effect.element.is( '[data-easing]' ) )
{
effect.easing = _this.easing_functions[ effect.element.data( 'easing' ) ]
}
else
{
effect.easing = _this.easing_functions[ 'easeout' ];
}
// Get animated properties
var properties = {};
if( effect.element.is( '[data-opacity]' ) ) properties.opacity = effect.element.data( 'opacity' );
if( effect.element.is( '[data-translatex]' ) ) properties.translatex = effect.element.data( 'translatex' );
if( effect.element.is( '[data-translatey]' ) ) properties.translatey = effect.element.data( 'translatey' );
if( effect.element.is( '[data-translatez]' ) ) properties.translatez = effect.element.data( 'translatez' );
if( effect.element.is( '[data-rotatex]' ) ) properties.rotatex = effect.element.data( 'rotatex' );
if( effect.element.is( '[data-rotatey]' ) ) properties.rotatey = effect.element.data( 'rotatey' );
if( effect.element.is( '[data-rotatez]' ) ) properties.rotatez = effect.element.data( 'rotatez' );
if( effect.element.is( '[data-scale]' ) ) properties.scale = effect.element.data( 'scale' );
if( effect.element.is( '[data-scalex]' ) ) properties.scalex = effect.element.data( 'scalex' );
if( effect.element.is( '[data-scaley]' ) ) properties.scaley = effect.element.data( 'scaley' );
if( effect.element.is( '[data-scalez]' ) ) properties.scalez = effect.element.data( 'scalez' );
effect.properties = properties;
effects.push( effect );
});
element.effects = effects;
_this.elements.push( element );
});
}
// ----------------------------------------------------------------------------------------------------
// Update elements
_this.update = function()
{
window.requestAnimationFrame( function()
{
_this.update_viewport_position();
if( _this.viewport_top_previous != _this.viewport_top )
{
_this.update_elements_in_view();
_this.animate();
}
_this.viewport_top_previous = _this.viewport_top;
});
}
// ----------------------------------------------------------------------------------------------------
// Animate stuff
_this.animate = function()
{
// For each element in viewport
var elements_in_view_length = _this.elements_in_view.length;
for( var i=0 ; i<elements_in_view_length ; i++ )
{
var element = _this.elements_in_view[i];
// For each effect
var effects_length = element.effects.length;
for( var e=0 ; e<effects_length ; e++ )
{
var effect = element.effects[e];
// Get effect animation boundaries
switch( effect.when )
{
case 'view' : // Maintained for backwards compatibility
case 'span' :
var start = element.top - _this.viewport_height;
var end = element.bottom;
break;
case 'exit' :
var start = element.bottom - _this.viewport_height;
var end = element.bottom;
break;
default :
var start = element.top - _this.viewport_height;
var end = element.top;
break;
}
// Crop boundaries
if( effect.crop )
{
if( start < 0 ) start = 0;
if( end > ( _this.body_height - _this.viewport_height ) ) end = _this.body_height - _this.viewport_height;
}
// Get scroll position of reference selector
var scroll = ( _this.viewport_top - start ) / ( end - start );
// Get relative scroll position for effect
var from = effect[ 'from' ];
var to = effect[ 'to' ];
var length = to - from;
var scroll_relative = ( scroll - from ) / length;
// Apply easing
var scroll_eased = effect.easing( scroll_relative );
// Get new value for each property
var opacity = _this.animate_value( scroll , scroll_eased , from , to , effect , 'opacity' );
var translatey = _this.animate_value( scroll , scroll_eased , from , to , effect , 'translatey' );
var translatex = _this.animate_value( scroll , scroll_eased , from , to , effect , 'translatex' );
var translatez = _this.animate_value( scroll , scroll_eased , from , to , effect , 'translatez' );
var rotatex = _this.animate_value( scroll , scroll_eased , from , to , effect , 'rotatex' );
var rotatey = _this.animate_value( scroll , scroll_eased , from , to , effect , 'rotatey' );
var rotatez = _this.animate_value( scroll , scroll_eased , from , to , effect , 'rotatez' );
var scale = _this.animate_value( scroll , scroll_eased , from , to , effect , 'scale' );
var scalex = _this.animate_value( scroll , scroll_eased , from , to , effect , 'scalex' );
var scaley = _this.animate_value( scroll , scroll_eased , from , to , effect , 'scaley' );
var scalez = _this.animate_value( scroll , scroll_eased , from , to , effect , 'scalez' );
// Override scale values
if( 'scale' in effect.properties )
{
scalex = scale;
scaley = scale;
scalez = scale;
}
// Update properties
effect.element.css(
{
'opacity' : opacity,
'transform' : 'translate3d( '+translatex+'px , '+translatey+'px , '+translatez+'px ) rotateX( '+rotatex+'deg ) rotateY( '+rotatey+'deg ) rotateZ( '+rotatez+'deg ) scale3d( '+scalex+' , '+scaley+' , '+scalez+' )'
} );
}
}
}
// ----------------------------------------------------------------------------------------------------
// Calculate property values
_this.animate_value = function( scroll , scroll_eased , from , to , effect , property )
{
var value_default = _this.property_defaults[ property ];
// Return default value if property is not animated
if( !( property in effect.properties ) ) return value_default;
var value_target = effect.properties[ property ];
var forwards = ( to > from ) ? true : false;
// Return boundary value if outside effect boundaries
if( scroll < from && forwards ) { return value_default; }
if( scroll > to && forwards ) { return value_target; }
if( scroll > from && !forwards ) { return value_default; }
if( scroll < to && !forwards ) { return value_target; }
// Calculate new property value
var new_value = value_default + ( scroll_eased * ( value_target - value_default ) );
// Round as required
switch( property )
{
case 'opacity' : new_value = new_value.toFixed(2); break;
case 'translatex' : new_value = new_value.toFixed(0); break;
case 'translatey' : new_value = new_value.toFixed(0); break;
case 'translatez' : new_value = new_value.toFixed(0); break;
case 'rotatex' : new_value = new_value.toFixed(1); break;
case 'rotatey' : new_value = new_value.toFixed(1); break;
case 'rotatez' : new_value = new_value.toFixed(1); break;
case 'scale' : new_value = new_value.toFixed(3); break;
default : break;
}
// Done
return new_value;
}
// ----------------------------------------------------------------------------------------------------
// Update viewport position
_this.update_viewport_position = function()
{
_this.viewport_top = $window.scrollTop();
_this.viewport_bottom = _this.viewport_top + _this.viewport_height;
}
// ----------------------------------------------------------------------------------------------------
// Update list of elements in view
_this.update_elements_in_view = function()
{
_this.elements_in_view = [];
var elements_length = _this.elements.length;
for( var i=0 ; i<elements_length ; i++ )
{
if ( ( _this.elements[i].top < _this.viewport_bottom ) && ( _this.elements[i].bottom > _this.viewport_top ) )
{
_this.elements_in_view.push( _this.elements[i] );
}
}
}
// ----------------------------------------------------------------------------------------------------
// Stuff to do on resize
_this.on_resize = function()
{
// Update viewport/element data
_this.update_viewport();
_this.update_element_heights();
// Update display
_this.update_viewport_position();
_this.update_elements_in_view();
_this.animate();
}
// ----------------------------------------------------------------------------------------------------
// Update viewport parameters
_this.update_viewport = function()
{
_this.body_height = $document.height();
_this.viewport_height = $window.height();
}
// ----------------------------------------------------------------------------------------------------
// Update height of animated elements
_this.update_element_heights = function()
{
var elements_length = _this.elements.length;
for( var i=0 ; i<elements_length ; i++ )
{
var element_height = _this.elements[i].element.outerHeight();
var position = _this.elements[i].element.offset();
_this.elements[i].height = element_height;
_this.elements[i].top = position.top;
_this.elements[i].bottom = position.top + element_height;
}
}
// ----------------------------------------------------------------------------------------------------
// Bind initialisation
$document.on( _this.init_events.join( ' ' ) , function(){ _this.init(); } );
// ----------------------------------------------------------------------------------------------------
return _this;
// ----------------------------------------------------------------------------------------------------
})( jQuery );
Also see: Tab Triggers