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="intro" style="margin-top: 20px">
<!-- Intro -->
<h1>Scrolleo</h1>
<p>This is a demo of videos reacting to scrolling.</p>
<p>Created by <a href="http://markteater.com">Mark Teater</a>, based on a <a href="https://codepen.io/anon/pen/mJwbK">CodePen</a> found on <a href="http://www.reddit.com/r/webdev/comments/2krge1/codepens_killer_html5_video_scrolling_controls_w/">Reddit</a></p>
<br/>
<br/>
<br/>
<h3>Scroll down for demo</h3>
</div>
<div style="margin-top: 100vh">
<h3>Video 1 (Only plays 6 seconds of video with 100px delay</h3>
<!-- Video 1 -->
<video id="scrolleo-1" width="100%" height="100%" autobuffer="autobuffer" preload="preload">
<source src="https://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.mp4">
</video>
</div>
<div style="margin-top: 800px">
<h3>Video 2 (Plays full length video)</h3>
<!-- Video 2 -->
<video id="scrolleo-2" width="100%" height="100%" autobuffer="autobuffer" preload="preload">
<source src="https://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.mp4">
</video>
</div>
<!-- Spacer -->
<div style="margin-top: 800px"></div>
.intro, pre {
max-width: 600px;
margin: 0 auto;
}
h1, h3, p {
font-family: 'Helvetica Neue', Helvetica, Arial;
text-align: center;
}
h1 {
font-weight: 300;
font-size: 3em;
}
h3 {
font-weight: 100;
letter-spacing: 1px;
}
body {
margin: 0;
}
/* Scrolleo - make your video scroll with inertia
* MIT License - by Mark Teater
*/
(function(window, document, undefined) {
"use strict";
var _Scrolleo = function(opts) {
// Defaults
this.acceleration = 0.08; //1 is fastest, 0 is slowest, 0.08 is default
this.secondsPerScreen = null; //Set this to the length of the video. "1" is 1 second.
this.additionalOffset = 0; //Add or subtract pixels to when the video will start. "10" means that the video will start 10px earlier.
this.wrapperEl = null;
// Override defaults
if (opts) {
for (var opt in opts) {
this[opt] = opts[opt];
}
}
};
var targetScrollPos;
_Scrolleo.prototype = {
init: function() {
var self = this;
this.wrapper = document.querySelectorAll(this.wrapperEl);
// get the location of the top of the page
targetScrollPos = window.pageYOffset;
Array.prototype.forEach.call(this.wrapper, function(wr) {
// Set the pixelsPerSecond to the full duration of the video if nothing was set in the options
if (self.secondsPerScreen === null) {
self.wrapper[0].addEventListener("loadedmetadata", function() {
self.secondsPerScreen = self.wrapper[0].duration;
//recalculate values on video with new pixelsPerSecond
self.distanceToTop = getElemDistanceToTop(elem);
self.offsetFromTop = getOffsetFromTop(self.distanceToTop);
self.pixelsPerSecond = getPixelsPerSecond();
});
}
self.pixelsPerSecond = null;
self.scrollPos = null;
self.currentTime = null;
self.offsetFromTop = null;
self.distanceToTop = null;
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ["ms", "moz", "webkit", "o"];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + "RequestAnimationFrame"];
window.cancelAnimationFrame = window[vendors[x] + "CancelAnimationFrame"] || window[vendors[x] + "CancelRequestAnimationFrame"];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
})();
// requestAnim shim layer by Paul Irish
window.requestAnimFrame = (function() {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element) {
window.setTimeout(callback, 1000 / 60);
}
);
})();
// Define functions to be used by Scrolleo
var getElemDistanceToTop = function(elem) {
//http://gomakethings.com/get-distances-to-the-top-of-the-document-with-native-javascript/
var location = 0;
if (elem.offsetParent) {
do {
location += elem.offsetTop;
elem = elem.offsetParent;
} while (elem);
}
return location >= 0 ? location : 0;
},
getOffsetFromTop = function(distanceToTop) {
var offset = distanceToTop - window.innerHeight + self.additionalOffset;
return offset >= 0 ? offset : 0;
},
getPixelsPerSecond = function() {
var pixelsPerSecond = (window.innerHeight + self.wrapper[0].clientHeight) / self.secondsPerScreen;
return pixelsPerSecond >= 0 ? pixelsPerSecond : 0;
},
scrollHandler = function() {
targetScrollPos = window.pageYOffset;
},
resizeHandler = function() {
//recalculate values on resize
self.distanceToTop = getElemDistanceToTop(elem);
self.offsetFromTop = getOffsetFromTop(self.distanceToTop);
self.pixelsPerSecond = getPixelsPerSecond();
},
scrollControl = function() {
//what to do when scrolling
self.scrollPos += (targetScrollPos - self.offsetFromTop - self.scrollPos) * self.acceleration;
self.currentTime = self.scrollPos / self.pixelsPerSecond; //convert scrollPos from pixels to seconds to set self.currentTime
self.wrapper[0].currentTime = self.currentTime;
self.wrapper[0].pause();
};
// Get an element's distance from the top of the page
var elem = self.wrapper[0];
// Calulate the initial size, distance, and offset of each scrolleo video
self.distanceToTop = getElemDistanceToTop(elem);
self.offsetFromTop = getOffsetFromTop(self.distanceToTop);
self.pixelsPerSecond = getPixelsPerSecond();
self.scrollPos = targetScrollPos - self.offsetFromTop;
wr.pause();
// Use requestAnimationFrame to ensure the video is updating when the browser is ready
window.requestAnimFrame(function render() {
window.requestAnimFrame(render);
// Only kickoff scrollControl if the scrollPos of element hasn't reached targetScrollPos
if (Math.round(self.scrollPos + self.offsetFromTop) != targetScrollPos) {
scrollControl();
}
});
window.addEventListener("scroll", scrollHandler, false);
window.addEventListener("resize", resizeHandler, false);
});
}
};
window.Scrolleo = _Scrolleo;
})(window, document);
// Setup video 1
var scrolleo1 = new Scrolleo({
acceleration: 0.08, // 1 = instant, 0 = never
secondsPerScreen: 6, // Defaults to video duration
additionalOffset: 100, // Positive starts the video later, negative starts earlier. default starts when top of video hits bottom of the screen
wrapperEl: "#scrolleo-1" // id of the video you want to control
});
scrolleo1.init();
// Setup Video 2
var scrolleo2 = new Scrolleo({
wrapperEl: "#scrolleo-2"
});
scrolleo2.init();
Also see: Tab Triggers