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 id="page">
<h1>This is a demo for the isiaToTop plugin<br />
<span>
<iframe src="https://ghbtns.com/github-btn.html?user=vurghus-minar&repo=isiaToTop&type=fork&count=false&size=large" frameborder="0" scrolling="0" width="158px" height="30px"></iframe>
</span>
</h1>
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
<ul>
<li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li>
<li>Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</li>
<li>Phasellus ultrices nulla quis nibh. Quisque a lectus. Donec consectetuer ligula vulputate sem tristique cursus. Nam nulla quam, gravida non, commodo a, sodales sit amet, nisi.</li>
<li>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</li>
</ul>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</div>
#isiaToTop {
position: fixed;
right: 20px;
bottom: 20px;
float: right;
width: 20px;
height: 20px;
padding: 10px;
cursor: pointer;
-webkit-animation: toTopFadeIn 2s linear;
animation: toTopFadeIn 2s linear;
opacity: 1;
color: #fff;
border: 3px solid #fff;
background: #d13317;
}
#isiaToTop:hover {
background: #df4624;
}
@-webkit-keyframes toTopFadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes toTopFadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
/**
DEMO STYLES
*/
body{
background-color: #e23849;
}
#page {
font-family: "Helvetica Neue", Arial, sans-serif;
width: 100%;
max-width: 600px;
margin: 40px auto;
padding: 20px;
background-color: beige;
box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
}
#page h1 {
text-align: center;
}
#page h1 span iframe{
margin-top: 20px;
}
/**
* Isia Scroll to top plugin.
*
* Plugin is used mainly as back to top click button that shows up once the body has been scrolled a specified height.
*
* @link https://github.com/vurghus-minar/isiaToTop
*
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory());
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.isiaToTop = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
'use strict'
const body = document.body
const html = document.documentElement
const windowHeight = window.innerHeight || html.clientHeight || body.clientHeight
const documentHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight)
const defaults = {
toTopElementTemplate: `<div id="isiaToTop" style="display: none">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAMAAAC6V+0/AAAAVFBMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////////8wXzyWAAAAG3RSTlMAAQIgJic0N0BHUVVZW1xdYm9wcXeFlZ2rrf2L0Jb5AAAAXElEQVQYV7XNRw6AMBBDURN673Xuf09IoiGjwBL+xtLbGPimYOmethH1nqnLfLVGNLyYVLUT+WrtiCqhbEB9qzPWEVidAY3RApO2mD9bjSkwCzOa6y1DiLIEv3QCJfQLDOsEaRoAAAAASUVORK5CYII=" alt="Scroll to top">
</div>`,
scrollStartPosition: 50,
toTopElement: 'isiaToTop',
scrollAnimationSpeed: 500,
onClick: null, // callback overrides _onClick method
onScroll: null // callback appends to the _onScroll method
}
let settings, toTopElement
function _extend (target, defaults) {
for (let i = 1; i < arguments.length; i++) {
for (let key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
arguments[0][key] = arguments[i][key]
}
}
}
return arguments[0]
}
/**
* Initiate the plugin
*/
function _init (options) {
settings = _extend({}, defaults, options)
_bindEvents()
}
/**
* Bind all events
*/
function _bindEvents () {
_onPageLoad()
_onScroll()
_onClick()
}
/**
* Triggered during DOM load
*/
function _onPageLoad(){
const toTopElementTemplate = settings.toTopElementTemplate
if(typeof toTopElementTemplate === 'function'){
toTopElementTemplate.call(this)
} else if (typeof toTopElementTemplate === 'string') {
body.insertAdjacentHTML('beforeend', toTopElementTemplate)
}
toTopElement = document.getElementById(settings.toTopElement)
}
/**
* _onScroll method runs on page scroll.
*/
function _onScroll () {
window.addEventListener('scroll', function () {
const scrollPos = window.scrollY || window.scrollTop || document.getElementsByTagName('html')[0].scrollTop
if (documentHeight >= windowHeight) {
scrollPos > settings.scrollStartPosition ? _showToTopElement() : _hideToTopElement()
}
if (typeof settings.onScroll === 'function'){
settings.onScroll.call(this)
}
})
}
/**
* _onClick method runs when element is clicked.
*/
function _onClick () {
toTopElement.addEventListener('click', function () {
if (typeof settings.onClick === 'function') {
settings.onClick.call(this)
} else {
_onToTop(html, 0, settings.scrollAnimationSpeed)
}
})
}
/**
* Function triggered on scroll event.
*/
function _scroll(){
if(document.getElementById(settings.toTopElement).length !== 0){
_onToTop(html, 0, settings.scrollAnimationSpeed)
}
}
/**
* Activates when the page is scrolled or element is clicked.
*
* @param {object} element - DOM element to scroll.
* @param {integer} to - starting point of animation in milliseconds.
* @param {integer} duration - speed of animation.
*/
function _onToTop (element, to, duration) {
let start = element.scrollTop
let change = to - start
let currentTime = 0
let increment = 20
// Scrolling animation
const __animateScroll = function () {
currentTime += increment
const val = __easeInOutQuad(currentTime, start, change, duration)
element.scrollTop = val
if (currentTime < duration) {
setTimeout(__animateScroll, increment)
}
}
// t = current time
// b = start value
// c = change in value
// d = duration
const __easeInOutQuad = function (t, b, c, d) {
t /= d / 2
if (t < 1) return c / 2 * t * t + b
t--
return -c / 2 * (t * (t - 2) - 1) + b
}
__animateScroll()
}
/**
* Display the ToTopElement
*/
function _showToTopElement () {
toTopElement.setAttribute('style', 'display:block')
}
/**
* Hide the ToTopElement
*/
function _hideToTopElement () {
toTopElement.setAttribute('style', 'display: none;')
}
/**
* Plugin methods to expose publicly.
*/
return {
active: _init,
scroll: _scroll
}
}))
////////////DEMO SET UP/////////////////
const options = {
scrollStartPosition: 20,
scrollAnimationSpeed: 1000,
onClick: overrideClick,
onScroll: function () {
console.log('scrolled')
}
}
isiaToTop.active(options)
function overrideClick(){
console.log('bla')
isiaToTop.scroll()
}
Also see: Tab Triggers