Morphing Elements Using MooTools and CSS

By  on  

Morphing an element between CSS classes is another great trick the MooTools JavaScript library enables you to do. Morphing isn't the most practical use of MooTools, but it's still a trick at your disposal.

Step 1: The XHTML

<a href="javascript:;" class="switch-view" rel="view1">View 1</a> |
<a href="javascript:;" class="switch-view" rel="view2">View 2</a> |
<a href="javascript:;" class="switch-view" rel="view3">View 3</a>
<br /><br />

<div class="view1" id="content-block">
	Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam hendrerit
	egestas nunc. Nam scelerisque neque a nibh. Proin tincidunt. Suspendisse
	facilisis, lorem et venenatis consectetuer, augue justo molestie lacus, id
	consectetuer ligula nibh a tellus.

The block of content that will change is the content-block DIV. Each link above the DIV will control which morph the DIV will undergo. Notice that each link is given the switch-view class so that they can be selected as an array by MooTools. Each link also has a rel attribute that contains the CSS class that the content-block DIV should morph into.

Step 2: The CSS

.view1		{ border:2px solid #ffcc00; color:#000000; background:#fffea1; padding:5px; letter-spacing:3px; line-height:19px; font-family:tahoma; font-size:10px; }
.view2		{ border:9px dashed #0000ff; color:#009900; background:#fcfa56; padding:12px; letter-spacing:1px; line-height:22px; font-family:verdana; font-size:14px; }
.view3		{ border:5px solid #000000; color:#ffffff; background:#ff0000; padding:10px; letter-spacing:8px; line-height:25px; font-family:times; font-size:12px; }

I've defined three CSS classes with the same properties defined. If you fail to define the same properties in each class, you risk allowing the previous class' properties to stay during the morphing process.

Step 3: The MooTools JavaScript

//window on dom ready
window.addEvent('domready', function() {
	//for every switch-view link
	$$('.switch-view').each(function(el) {
		//add click event
		el.addEvent('click', function(e) {
			//morph baby!
			myFx = new Fx.Morph('content-block', {duration: 500, transition: Fx.Transitions.Sine.easeOut}).start('.' + el.get('rel'));

Once the DOM is ready, we use grab every link with the switch-view class. When any of these links are clicked on, the link's rel attribute is morphed into.

Recent Features

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

Incredible Demos

  • By
    Spatial Navigation

    Spatial navigation is the ability to navigate to focusable elements based on their position in a given space.  Spatial navigation is a must when your site or app must respond to arrow keys, a perfect example being a television with directional pad remote.  Firefox OS TV apps are simply...

  • By
    Translate Content with the Google Translate API and JavaScript

    Note:  For this tutorial, I'm using version1 of the Google Translate API.  A newer REST-based version is available. In an ideal world, all websites would have a feature that allowed the user to translate a website into their native language (or even more ideally, translation would be...


  1. Just found this out which had me stumped for a while. When you tell it to morph to a class, you have to point it to the entire rule. So if you have have in your css ".a .b {rule}"

    you have to do el.morph(...,).start('.a .b'); instead of just .start('.b'), even if your element already matches the CSS selector.

  2. Kyle

    This shit doesn’t work

  3. Keiran Lusk

    Hi David. I am a complete newbie to MooTools. Thanks for the excellent tutorials regarding the wonderful world of MooTools. I have been studying the ‘morphing’ example above and was successful in implementing a practice run of your example on my WordPress site.

    I am currently working with a conditional logic form (Gravity Form on a WP site) and would like to morph a CSS element upon loading of a conditional html block (instead of initiating the morph through clicking, as shown in your demo).

    The scenario is:

    A wordpress post contains a thumbnail image, information and a conditional logic form. Site visitors select a picture frame style (in the conditional logic form) and the CSS image border elements are morphed to reflect their selection (a HTML block in the form initiates the script upon conditional selection of options).

    Instead of ‘clicking’ on View 1, View 2 or View 3, I need the various options to initiate automatically upon load. Is that possible? What would I need to alter in the:

    1. XHTML – I have tried many variations of ‘body onload’ but cannot trigger the morph.
    2. MooTools Javascript – would I remove the ‘click’ and replace with ‘load’?

    Any tips or advice would be greatly appreciated!



  4. boske

    nice job :) but i wounder how to make link from other page direct to View 1,View 2 or View 3??

  5. Hi David nice tutorial, but i have a question! Hov to mahe link from other page to open view2 direct.

  6. cec

    but the letters don’t actually morph.
    they change appearance but the font is always the same (arial i guess).
    how can i solve this problem?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!