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.
[Countdown.js](https://github.com/HugoGiraudel/Countdown.js)
============
Countdown.js is a little customizable countdown made in pure JavaScript. GitHub repository [here](https://github.com/HugoGiraudel/Countdown.js).
## Examples
// Instanciating a new countdown with all defaults
new Countdown();
<div class="timer"></div>
// Instanciating a custom countdown
new Countdown({
selector: '.new-year',
dateEnd: new Date('Jan 1, 2014 12:00:00'),
msgPattern : 'Happy new year in {days} days, {hours} hours, {minutes} minutes !'
});
<div class="new-year"></div>
You can also play around with the code at [CodePen](https://codepen.io/HugoGiraudel/pen/vCyJq).
## Options
You can pass the constructor number of options, including:
#### `selector`
The selector you want to inject Countdown into.
*Default*: `.timer`
#### `msgBefore`
The message to display before reaching `dateStart`.
*Default*: `"Be ready!"`
#### `msgAfter`
The message to display once reaching `dateEnd`.
*Default*: `"It's over, sorry folks!"`
#### `msgPattern`
The message to display during the countdown where values between braces get replaced by actual numeric values.
Possible patterns:
* `{years}`
* `{months}`
* `{weeks}`
* `{days}`
* `{hours}`
* `{minutes}`
* `{seconds}`
*Default*: `"{days} days, {hours} hours, {minutes} minutes and {seconds} seconds left."`
#### `dateStart`
The date to start the countdown to. Should be an instance of class `Date`. Documentation [here at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date).
*Default*: `new Date()` (now)
#### `dateEnd`
The date to end the countdown to. Should be an instance of class `Date`. Documentation [here at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date).
*Default*: `new Date(new Date().getTime() + (24 * 60 * 60 * 1000))` (tomorrow)
#### `onStart`
The callback to run whenever the countdown start ticking.
*Default*: `null`
#### `onEnd`
The callback to run whenever the countdown stops.
*Default*: `null`
@import "compass/css3";
.timer, .new-year {
color: darken(hotpink, 10%);
text-align: center;
padding: .5em;
font-size: 1.25em;
}
body {
padding: 1em;
max-width: 600px;
margin: 0 auto;
}
pre,
code {
background: #EFEFEF;
border: 1px solid rgba(0,0,0,.1);
border-radius: 4px;
}
pre {
background: #EFEFEF;
border: 1px solid rgba(0,0,0,.1);
padding: .5em;
border-radius: 4px;
text-shadow: 0 1px rgba(255, 255, 255, .75);
line-height: 1.4;
font-size: .9em;
}
pre code {
background: none;
border: none;
padding: 0;
border-radius: 0;
}
(function(global) {
"use strict";
// Vanilla JS alternative to $.extend
global.extend = function(obj, extObj) {
obj = obj || {};
if (arguments.length > 2) {
for (var a = 1; a < arguments.length; a++) {
global.extend(obj, arguments[a]);
}
} else {
for (var i in extObj) {
obj[i] = extObj[i];
}
}
return obj;
};
// Countdown constructor
var Countdown = function(conf) {
this.conf = global.extend({
// Dates
dateStart : new Date(),
dateEnd : new Date(new Date().getTime() + (24 * 60 * 60 * 1000)),
// Default elements
selector : ".timer",
// Messages
msgBefore : "Be ready!",
msgAfter : "It's over, sorry folks!",
msgPattern : "{days} days, {hours} hours, {minutes} minutes and {seconds} seconds left.",
// Callbacks
onStart : null,
onEnd : null
}, conf);
// Private variables
this.selector = document.querySelectorAll(this.conf.selector);
this.interval = 1000;
this.now = new Date();
this.patterns = [
{ pattern: "{years}", secs: 31536000 },
{ pattern: "{months}", secs: 2628000 },
{ pattern: "{weeks}", secs: 604800 },
{ pattern: "{days}", secs: 86400 },
{ pattern: "{hours}", secs: 3600 },
{ pattern: "{minutes}", secs: 60 },
{ pattern: "{seconds}", secs: 1 }
];
// Doing all the things!
this.init();
};
// Initializing the instance
Countdown.prototype.init = function() {
this.defineInterval();
if(this.now < this.conf.dateEnd && this.now >= this.conf.dateStart) {
this.run();
this.callback("start");
} else {
this.outOfInterval();
}
};
// Running the countdown
Countdown.prototype.run = function() {
var now = this.now.valueOf() / 1000,
tar = this.conf.dateEnd.valueOf() / 1000,
sec = Math.abs(tar - now);
// Vanilla JS alternative to $.proxy
var that = this;
var timer = global.setInterval(function() {
sec--;
if(sec > 0) {
that.display(sec);
} else {
clearInterval(timer);
that.outOfInterval();
that.callback("end");
}
}, this.interval);
this.display(sec);
};
// Displaying the countdown
Countdown.prototype.display = function(sec) {
var output = this.conf.msgPattern;
for (var b = 0; b < this.patterns.length; b++) {
var currentPattern = this.patterns[b];
if (this.conf.msgPattern.indexOf(currentPattern.pattern) !== -1) {
var number = Math.floor(sec / currentPattern.secs);
sec -= number * currentPattern.secs;
output = output.replace(currentPattern.pattern, number);
}
}
for(var c = 0; c < this.selector.length; c++) {
this.selector[c].innerHTML = output;
}
};
// Defining the interval to be used for refresh
Countdown.prototype.defineInterval = function() {
for (var e = this.patterns.length; e > 0; e--) {
var currentPattern = this.patterns[e-1];
if (this.conf.msgPattern.indexOf(currentPattern.pattern) !== -1) {
this.interval = currentPattern.secs * 1000;
return;
}
}
};
// Canceling the countdown in case it's over
Countdown.prototype.outOfInterval = function() {
var message = this.now < this.conf.dateStart ? this.conf.msgBefore : this.conf.msgAfter;
for(var d = 0; d < this.selector.length; d++) {
this.selector[d].innerHTML = message;
}
};
// Dealing with events and callbacks
Countdown.prototype.callback = function(event) {
event = event.capitalize();
// onStart callback
if(typeof this.conf["on" + event] === "function") {
this.conf["on" + event]();
}
// Triggering a jQuery event if jQuery is loaded
if(typeof global.jQuery !== "undefined") {
global.jQuery(this.conf.selector).trigger("countdown" + event);
}
};
// Adding a capitalize method to String
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
global.Countdown = Countdown;
}(window));
// Initializing two countdowns
var tomorrow = new Countdown(); // All defaults
var happyNewYear = new Countdown({ // Custom
selector: '.new-year',
dateEnd: new Date('2015/01/01'),
msgPattern : 'Happy new year in {days} days, {hours} hours, {minutes} minutes !',
onStart: function() {
console.log('Starting countdown');
},
onEnd: function() {
console.log('Ending countdown');
}
});
Also see: Tab Triggers