O'Reilly

Create an Animated Sliding Button Using MooTools

By on  
MooTools Sliding Button

Buttons (or links) are usually the elements on our sites that we want to draw a lot of attention to. Unfortunately many times they end up looking the most boring. You don't have to let that happen though! I recently found a Tympanus post which provided a great method for making button have an unexpected pop. Here's a quick tutorial on how to duplicate that look using MooTools.

The HTML





The system works off of one DIV, two As and SPAN elements. Note the IDs and CSS classes for the CSS.

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{ /* display:none; */ visibility:hidden; 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{ /* display:none; */ visibility:hidden; 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 above mirrors the original code with the exception that "display:none;" was changed to "visibility:hidden", per the differences in MooTools and jQuery fading methodology. Feel free to modify the basic color/formatting styles in any way you'd like.

The MooTools JavaScript

window.addEvent('domready',function() {
	$$('.slidebttn').each(function(btn) {
		var prev = btn.getPrevious('a').set('tween',{ duration: 200 });
		var span = prev.getElement('span');
		btn.addEvents({
			mouseenter: function(e) { 
				btn.addClass('button_c');
				prev.tween('width',225);
				span.fade('in');
			},
			mouseleave:function(e) {
				btn.removeClass('button_c');
				prev.tween('width',70);
				span.fade('out');
			}
		});
	});
});

The first step is grabbing all button containers and identifying the SPAN and A elements within them. Then we add mouseenter and mouseleave events to the container which do the animation of the primary A/SPAN elements.

Great work by Tympanus. The effect is classy and well executed. Now go and use this on your next project!

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome,...

  • Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?...

Incredible Demos

Discussion

  1. Very good, Mr Walsh.

  2. I like this, but I think it comes with a very slight usability issue – when I hovered over and the “Use Twitter account” text popped out, I tried a couple of times to move over and click it like it was offering an alternative sign-in method. Could be a teeny bit frustrating for an impatient user thinking they’re just slipping off the button when it shoots back in.

  3. adlib

    this one doesn’t work very well with opera…

  4. derdummkopf

    sorry for this stupid question, i’am a newbe
    but how looks the function if i have only one button
    $$('.slidebttn').each(function(btn) {...

    i think each is then not necessary or?

  5. I like. However, IE not allowing border-radius kinda kills the effect. Looks much better in firefox or chrome.

  6. @derdummkopf: How would you accomplish this without each? The only way I can see of doing so would repeat walks through the DOM to get the previous items.

    @John: It may be something where you don’t show IE users this effect.

  7. Nice little dilly hoper here David.

    @derdummkopf
    you could just take the function from within .each() and name it. then pass it a single mootooled element $("one_element_ID").

    function makeSlideBTN(btn) {
    	var prev = btn.getPrevious('a').set('tween',{ duration: 200 });
    	var span = prev.getElement('span');
    	btn.addEvents({
    		mouseenter: function(e) { 
    			btn.addClass('button_c');
    			prev.tween('width',225);
    			span.fade('in');
    		},
    		mouseleave:function(e) {
    			btn.removeClass('button_c');
    			prev.tween('width',70);
    			span.fade('out');
    		}
    	});
    }
    
    window.addEvent("domready",function(){
         //make an element slide
         makeSlide($("whattever-ID"))
    })
    
    

    Remember to keep the classname or it’ll not have any style

  8. That is a really neat user interface object, I am sure clients will understand what you want from them when buttons start opening and closing for them.

  9. Insane :) Hehe.. have to love it..

    Great for exercise … Soon to be implemented on my secret project :D

  10. Hi,

    Nice work,

    Can u please say what the below line will do in above code.

    var prev = btn.getPrevious(‘a’).set(‘tween’,{ duration: 200 });

    Specially getPrevious function

  11. ethan

    how can i make it work with master/content pages?

  12. Verry nice and simple! With a little customization it fits perfect on my site! Thanx.

  13. Joe

    How do I add a link to these? I tried but keep failing :(

  14. Joe

    How do I add a link to this button?

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

Recently on David Walsh Blog

  • Open Files from Command Line on OS X

    I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately.  Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I...

  • Get Stock Quotes From Command Line

    When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door.  But as my career has gone on, I've been more aware of money, investing, and retirement.  I've recently...

  • Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

  • Create an Image Preview from a Video

    Visuals are everything when it comes to media.  When I'm trying to decide whether to watch a video on Netflix, it would be awesome to see a trailer of some kind, but alas that isn't available.  When I'm looking to download a video on my computer,...

  • New:  Webdesigner News!

    A new and exciting website has recently been launched for web designers and developers. You likely spend hours every morning browsing through hundreds of posts on your RSS feeds, hoping to stumble across relevant stories. Webdesigner News was built to provide web designers and developers with...