Create Twitter-Style Buttons with the Dojo Toolkit

By  on  

I love that JavaScript toolkits make enhancing web pages incredibly easy. Today I'll cover an effect that I've already coded with MooTools: creating a Twitter-style animated "Sign In" button. Check out this five minute tutorial so you can take your static buttons to the next level!

The HTML

<!-- left version -->
<div class="button_wrap">
    <a class="button_aLeft" id="button_aLeft"><span>Use your Twitter ID</span></a>
    <a class="button_bLeft slidebttn" id="button_bLeft">Sign <span>in</span></a>
</div>
<!-- right version -->
<div class="button_wrap">

    <a class="button_aRight" id="button_aRight"><span>Use your Twitter ID</span></a>
    <a class="button_bRight slidebttn" id="button_bRight">Sign <span>in</span></a>
</div>

The "button" consists of one DIV with two sets SPANs wrapped by A elements.

The CSS

.button_wrap{ position:relative; width:225px; height:36px; overflow:hidden; font-weight:bold; font-size:11px; margin:10px; }
.button_aLeft{ width:70px; height:36px; -moz-border-radius:20px; -webkit-border-radius:20px; background-color:#093d6f; color:#fff; top:0px; right:0px; position:absolute; line-height:36px; text-align:left; }
.button_aLeft span{ z-index:200; padding-left:20px; color:#fff; }
.button_aRight{ width:70px; height:36px; -moz-border-radius:20px; -webkit-border-radius:20px; background-color:#093d6f; color:#fff; top:0px; left:0px; position:absolute; line-height:36px; text-align:right; }
.button_aRight span{ z-index:200; padding-right:20px; color:#fff; }

.button_bLeft{ width:64px; height:30px; background-color:#fff; -moz-border-radius:20px; -webkit-border-radius:20px; color:#000; position:absolute; top:3px; right:3px; text-transform:uppercase; line-height:30px; text-align:center; cursor:pointer; }
.button_bLeft span{ color:#008ddd; }
.button_bRight{ width:64px; height:30px; background-color:#fff; -moz-border-radius:20px; -webkit-border-radius:20px; color:#000; position:absolute; top:3px; left:3px; text-transform:uppercase; line-height:30px; text-align:center; cursor:pointer; }
.button_bRight span{ color:#008ddd; }

.button_c{ background-color:#008ddd; color:#fff; text-transform:uppercase; }
.button_c span{ color:#093d6f; }

The CSS code is a mess -- lot of CSS goes into styling the button. As always, style however you'd like. Be careful with this set though -- I recommend sticking to just changing colors.

The Dojo JavaScript

dojo.addOnLoad(function() {
	dojo.forEach(dojo.query('.button_wrap'),function(wrap) {
		var a = dojo.query('a',wrap)[0];
		var span = dojo.query('span',a)[0];
		var button = dojo.query('a',wrap)[1];
		
		dojo.anim(span,{ opacity:0 },1);
		
		dojo.connect(button,'onmouseenter',function() {
			dojo.addClass(button,'button_c');
			dojo.anim(a,{ width:200 });
			dojo.anim(span,{ opacity: 1 });
		});
		dojo.connect(button,'onmouseleave',function() {
			dojo.removeClass(button,'button_c');
			dojo.anim(a,{ width:70 });
			dojo.anim(span,{ opacity: 0 });
		});
	});
});

The first step is to collect each "button" DIV and sift through the its child elements to get links and SPAN elements. When the user's mouse enters the "Sign In" link, the first link within the wrapping DIV grows in width and shows the supporting text.

It's little enhancements like these that take a website from a 6 to an 8, transforming the website from static to dynamic. Can you think of any other small enhancements like this? Share!

Recent Features

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

Incredible Demos

  • By
    Element Position Swapping Using MooTools 1.2

    We all know that MooTools 1.2 can do some pretty awesome animations. What if we want to quickly make two element swap positions without a lot of fuss? Now you can by implementing a MooTools swap() method. MooTools 1.2 Implementation MooTools 1.2 Usage To call the swap...

  • By
    Create a Download Package Using MooTools Moousture

    Zohaib Sibt-e-Hassan recently released a great mouse gestures library for MooTools called Moousture. Moousture allows you to trigger functionality by moving your mouse in specified custom patterns. Too illustrate Moousture's value, I've created an image download builder using Mooustures and PHP. The XHTML We provide...

Discussion

  1. Great Job! Didnt I see these on the Adobe website though?

  2. Good! Maybe you could make a Dojo tutorial (and a Moo one as well)!

  3. @ctult: There are MooTools and jQuery tutorials that mirror this functionality. Check the “Related Posts” section above.

  4. @David Walsh: Oh. Yeah. Erm…I..uh knew that.

  5. Cool effect. I was looking for a different type of a twitter login. This definitely goes into the list.

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