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.
<h2 class="heading">Type once with typeTo()</h2>
<code><pre>$('h3').typeTo("Testing Typer.js jQuery Plugin with a 1 time trext string.");</pre></code>
<h3 class="typed"></h3>
<br><br><hr>
<h2 class="heading">Type indefinitely with typer()</h2>
Now let's say you want to loop over a set of strings for the typing effect. Easy!<br><br>
<h2 class="typed" data-typer-targets="Testing Typer.js jQuery Plugin, Pretty sweet little plugin!"></h2>
jQuery Code...
<code><pre>$('[data-typer-targets]').typer();</pre></code><br>
HTML: Comma seperated version (strings seperated with commas)...
<code><pre><h2 data-typer-targets="Testing Typer.js jQuery Plugin, Pretty sweet little plugin!"></h2></pre></code><br>
HTML: JSON version...
<code><pre><h2 data-typer-targets='
{ "targets" : ["Testing Typer.js jQuery Plugin.","Pretty sweet little plugin!", "Stumptown has the best coffee in Flatiron."]}'></h2></pre></code>
<h2 class="typed" data-typer-targets='{ "targets" : ["This is the JSON version...", "Testing Typer.js jQuery Plugin.","Pretty sweet little plugin!", "Stumptown has the best coffee in Flatiro."] }'></h2>
That code will start the effect on all elements with the data-typer-targets attribute.<br><br>
You obviously need to supply it with some source data. The data-typer-targets attribute can be either a comma-separated string or a piece of JSON.<br>
<br><br><hr>
<h2 class="heading">Options</h2>
There are some options that are available to you as well:<br><br>
<code><pre>
// Defaults
{
highlightSpeed : 20,
typeSpeed : 100,
clearDelay : 500,
typeDelay : 200,
clearOnHighlight : true,
typerDataAttr : 'data-typer-targets',
typerInterval : 2000
}</pre>
</code>
<br>
Set the options individually:<br>
<code><pre>$.typer.options.highlightSpeed = 500;</pre></code>
<br><br><hr>
<h2 class="heading">About</h2>
jquery.typer.js was originally developed for LayerVault by Kelly Sutton.<br>
GitHub code - <a href="https://github.com/jasondavis/jquery.typer.js">https://github.com/jasondavis/jquery.typer.js</a>
.heading{
color: #0089FF;
}
.typed{
color: #F1569D;
font-size: 28px;
font-family: sans-serif;
}
pre{
word-break: normal;
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: #F7F7F7;
border-radius: 3px;
font: 16px Consolas, "Liberation Mono", Menlo, Courier, monospace;
line-height: 1.6;
}
String.prototype.rightChars = function(n){
if (n <= 0) {
return "";
}
else if (n > this.length) {
return this;
}
else {
return this.substring(this.length, this.length - n);
}
};
(function($) {
var
options = {
highlightSpeed : 20,
typeSpeed : 100,
clearDelay : 500,
typeDelay : 200,
clearOnHighlight : true,
typerDataAttr : 'data-typer-targets',
typerInterval : 2000
},
highlight,
clearText,
backspace,
type,
spanWithColor,
clearDelay,
typeDelay,
clearData,
isNumber,
typeWithAttribute,
getHighlightInterval,
getTypeInterval,
typerInterval;
spanWithColor = function(color, backgroundColor) {
if (color === 'rgba(0, 0, 0, 0)') {
color = 'rgb(255, 255, 255)';
}
return $('<span></span>')
.css('color', color)
.css('background-color', backgroundColor);
};
isNumber = function (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
clearData = function ($e) {
$e.removeData([
'typePosition',
'highlightPosition',
'leftStop',
'rightStop',
'primaryColor',
'backgroundColor',
'text',
'typing'
]);
};
type = function ($e) {
var
// position = $e.data('typePosition'),
text = $e.data('text'),
oldLeft = $e.data('oldLeft'),
oldRight = $e.data('oldRight');
// if (!isNumber(position)) {
// position = $e.data('leftStop');
// }
if (!text || text.length === 0) {
clearData($e);
return;
}
$e.text(
oldLeft +
text.charAt(0) +
oldRight
).data({
oldLeft: oldLeft + text.charAt(0),
text: text.substring(1)
});
// $e.text($e.text() + text.substring(position, position + 1));
// $e.data('typePosition', position + 1);
setTimeout(function () {
type($e);
}, getTypeInterval());
};
clearText = function ($e) {
$e.find('span').remove();
setTimeout(function () {
type($e);
}, typeDelay());
};
highlight = function ($e) {
var
position = $e.data('highlightPosition'),
leftText,
highlightedText,
rightText;
if (!isNumber(position)) {
position = $e.data('rightStop') + 1;
}
if (position <= $e.data('leftStop')) {
setTimeout(function () {
clearText($e);
}, clearDelay());
return;
}
leftText = $e.text().substring(0, position - 1);
highlightedText = $e.text().substring(position - 1, $e.data('rightStop') + 1);
rightText = $e.text().substring($e.data('rightStop') + 1);
$e.html(leftText)
.append(
spanWithColor(
$e.data('backgroundColor'),
$e.data('primaryColor')
)
.append(highlightedText)
)
.append(rightText);
$e.data('highlightPosition', position - 1);
setTimeout(function () {
return highlight($e);
}, getHighlightInterval());
};
typeWithAttribute = function ($e) {
var targets;
if ($e.data('typing')) {
return;
}
try {
targets = JSON.parse($e.attr($.typer.options.typerDataAttr)).targets;
} catch (e) {}
if (typeof targets === "undefined") {
targets = $.map($e.attr($.typer.options.typerDataAttr).split(','), function (e) {
return $.trim(e);
});
}
$e.typeTo(targets[Math.floor(Math.random()*targets.length)]);
};
// Expose our options to the world.
$.typer = (function () {
return { options: options };
})();
$.extend($.typer, {
options: options
});
//-- Methods to attach to jQuery sets
$.fn.typer = function() {
var $elements = $(this);
return $elements.each(function () {
var $e = $(this);
if (typeof $e.attr($.typer.options.typerDataAttr) === "undefined") {
return;
}
typeWithAttribute($e);
setInterval(function () {
typeWithAttribute($e);
}, typerInterval());
});
};
$.fn.typeTo = function (newString) {
var
$e = $(this),
currentText = $e.text(),
i = 0,
j = 0;
if (currentText === newString) {
console.log("Our strings our equal, nothing to type");
return $e;
}
if (currentText !== $e.html()) {
console.error("Typer does not work on elements with child elements.");
return $e;
}
$e.data('typing', true);
while (currentText.charAt(i) === newString.charAt(i)) {
i++;
}
while (currentText.rightChars(j) === newString.rightChars(j)) {
j++;
}
newString = newString.substring(i, newString.length - j + 1);
$e.data({
oldLeft: currentText.substring(0, i),
oldRight: currentText.rightChars(j - 1),
leftStop: i,
rightStop: currentText.length - j,
primaryColor: $e.css('color'),
backgroundColor: $e.css('background-color'),
text: newString
});
highlight($e);
return $e;
};
//-- Helper methods. These can one day be customized further to include things like ranges of delays.
getHighlightInterval = function () {
return $.typer.options.highlightSpeed;
};
getTypeInterval = function () {
return $.typer.options.typeSpeed;
},
clearDelay = function () {
return $.typer.options.clearDelay;
},
typeDelay = function () {
return $.typer.options.typeDelay;
};
typerInterval = function () {
return $.typer.options.typerInterval;
};
})(jQuery);
$('h3').typeTo("Testing Typer.js jQuery Plugin with a 1 time trext string.");
$('[data-typer-targets]').typer();
Also see: Tab Triggers