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.
</div>

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) {
			//nowhere
			e.stop();
			//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
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

  • By
    Using Dotter for Form Submissions

    One of the plugins I'm most proud of is Dotter. Dotter allows you to create the typical "Loading..." text without using animated images. I'm often asked what a sample usage of Dotter would be; form submission create the perfect situation. The following...

Discussion

  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!

    Cheers,

    Keiran

  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.
    Thanks!

  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!