Google Extension Effect with CSS or jQuery or MooTools JavaScript

Both of the two great browser vendors, Google and Mozilla, have Extensions pages that utilize simple but classy animation effects to enhance the page. One of the extensions used by Google is a basic margin-top animation to switch between two panes: a graphic pane and a detail pane. I've taken a few moments to duplicate that effect with just CSS, and another enhanced version with jQuery and MooTools.

The Base HTML and CSS

I'm using minimal elements within this code since, theoretically, there would be many of these on a given page and large DOM trees can drastically effect page performance:

<div class="item"> <a href="/angry-birds"> <div class="item-billboard"> <img src="angrybirds.jpg" /> <h3>Angry Birds</h3> </div> <div class="item-detail"> <p>There's more detail about the item inside this DIV!</p> </div> </a> </div>

Remember that HTML5 says we can wrap block-level elements with A tags, so don't hate on me!

There's a fair amount of base CSS we need too:

.item { position: relative; width: 240px; overflow: hidden; border: 1px solid #ccc; } .item { height: 200px; } .item a { text-decoration: none; color: #000; } .item-billboard, .item-detail { padding: 10px; height: 180px; } .item-billboard { margin-top: 0; background: #fff; } .item-billboard h3 { font-size: 13px; font-weight: bold; color: #262626; font-family: "Open Sans", arial, sans-serif; } .item-detail { background: #ececec; }

The only restriction with the CSS above is that we're explicitly setting a lesser height for the billboard and detail blocks because we're adding padding to them. We could use the full 200px height of the parent for each of those blocks, and wrap the content for each in an additional DIV, allowing for animating margin-top to 100%, but that would add 2 extra nodes per block.

The CSS-Only Method

Much like my Google Photo Stack post, the CSS-only version gets us 90% there. Browser-powered CSS animations give us the ability to animate to the second pane...

.item-billboard { margin-top: 0; background: #fff; /* add animations! */ transition-property: margin-top; transition-duration: .5s; } /* animate on hover */ .itemCss:hover .item-billboard { margin-top: -200px; }

Since the CSS transition settings are on the .billboard elements, the margin will animate both on hover and then back to its original state during mouseleave. Unfortunately you're out of luck if the client doesn't support CSS transitions. That's where we can use MooTools or jQuery to make it happen!

The jQuery and MooTools JavaScript

Overriding the CSS animation with a bit of JavaScript will allow the same animation in older browsers. We can jQuery or MooTools to make that happen:

// MooTools window.addEvent("domready", function() { $$(".itemJs").addEvents({ mouseenter: function() { var billboard = this.retrieve("billboardElement"); if(!billboard) { billboard = this.getElements(".item-billboard")[0]; this.store("billboardElement", billboard); } billboard.tween("margin-top", "-200px"); }, mouseleave: function() { this.retrieve("billboardElement").tween("margin-top", 0); } }); }); // jQuery jQuery(document).ready(function() { jQuery(".itemJQuery").bind({ mouseenter: function() { var self = jQuery(this), billboard = self.data("billboardElement"); if(!billboard) { billboard = jQuery(jQuery(".item-billboard", this)[0]); self.data("billboardElement", billboard); } jQuery(billboard).stop().animate({ "margin-top": "-200px" }); }, mouseleave: function() { jQuery(this).data("billboardElement").stop().animate({ "margin-top": 0 }); } }); });

In each case, we lazy-find billboard elements when they're hovered over and animate them just as the CSS does. Simple!

I love the design of these blocks by Google. They provide a beautiful default view and allow for more detail when the user shows interest. I can see this strategy and effect being useful in many situations, as behind it is brilliant. A business that sells records could use the same sort of effect, for example. Well done to Google!