cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container">

	<span class="js-typed-src">
		<h1 class="page-title">Hey, this is typed using <a href="https://github.com/mattboldt/typed.js/">Typed.js</a>!</h1>
	</span>
	<span class="js-typed"></span>

	<span class="js-typed-src">
		<h2>I wanted to type out an entire page of text rather than a single line which is how it is usually used.</h2>
	</span>
	<span class="js-typed"></span>

	<span class="js-typed-src" data-speed="20">
		<p>I figured out a way to cycle through an array of elements and apply a new Typed instance to each one using a recursive function. This section is typed faster because of a data attribute used to apply a different typing speed per section.</p>
		<img class="page-image" src="http://3.bp.blogspot.com/-DT6OM3ShPZk/VGmsJb1hCkI/AAAAAAAARm0/JxQao_QFhGs/s1600/caloria.jpg">
		<p>You'll notice there are a few paragraph tags in this section, and even an image! The plugin is moving through all of this text and doesn't care about the markup.</p>
		<p>All of the text already exists in an element with the class <code>.js-typed-src</code> that is visually hidden. Then, there is an empty <code>&lt;span></code> below each with the class <code>.js-typed</code>. The plugin retrieves the HTML from the aforementioned <code>.js-typed-src</code> element and types it into the empty <code>span.js-typed</code>. This flow could probably be improved, plus it <em>may</em> read twice for screen readers &dash; I still have to test that.</p>
	</span>
	<span class="js-typed"></span>
	
	<span class="js-typed-src">
		<p>Anyhow, I'm finally learning JavaScript...for real this time!</p>
	</span>
	<span class="js-typed"></span>

</div>
            
          
!
            
              * {
	box-sizing: border-box;
}

body {
	line-height: 1.4;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

a {
	color: skyblue;
	text-decoration: none;
	&:hover {
		color: lightslategray;
	}
}

img {
	max-width: 100%;
	height: auto;
}

// Hidden for accessibility
.js-typed-src {
	opacity: 0;
	position: absolute;
	left: -99999px;
}

.container {
	max-width: 600px;
	margin: 2rem auto;
	padding: 1rem;
}

.page-title {
	margin-top: 0;
}

.page-image {
	display: block;
	margin: 1rem auto;
}
            
          
!
            
              // Get an array of all elements to be typed.
const TYPED_SRCS = document.querySelectorAll('.js-typed-src');
const TYPED_ELS = document.querySelectorAll('.js-typed');
const TYPE_SPEED = 40,
			SHOW_CURSOR = false;

var options = {
	elIndex: 0,
	strings: [TYPED_SRCS[0].innerHTML],
	typeSpeed: TYPE_SPEED,
	showCursor: SHOW_CURSOR,
	onComplete: () => {
		typeNextInArray();
	},
}

// Start the typing elements, starting with index 0
var typed = new Typed(TYPED_ELS[0], options);

// Function to type next item in typedEls array
function typeNextInArray() {

	// Move through the array
	options.elIndex++;
	let newIndex = options.elIndex;
	let currentEl = TYPED_SRCS[newIndex];

	// Allow for data attributes to provide some settings
	let newTypeSpeed = "speed" in currentEl.dataset ? +currentEl.dataset.speed : TYPE_SPEED;
	// More options could be added to / retrieved via data attributes here!

	// New options set
	let newOptions = {
		elIndex: newIndex,
		startDelay: 500,
		strings: [TYPED_SRCS[newIndex].innerHTML],
		typeSpeed: newTypeSpeed,
		showCursor: SHOW_CURSOR,
		onComplete: () => {
			if (newIndex + 1 <= TYPED_ELS.length - 1) {
				return typeNextInArray();
			} else {
				console.log('done');
			}
		}
	}

	let typed = new Typed(TYPED_ELS[newIndex], newOptions);
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console