HomeBlogCreating a jQuery Bobblehead

Creating a jQuery Bobblehead

Published March 30, 2011

Alex Fierro BobbleheadAs you may know if you follow the RustyBrick blog, we've introduced company profile pages for each employee. These pages allow us to write a little about ourselves and explain some of the work we do at RustyBrick.  I was looking to add some "flair" to my profile to make it stand out amongst the other profiles, but wasn't sure what add. Luckily, Ronnie gave me a great idea. My current profile picture was a really nice hand-drawn caricature illustrated by Becca Klein. Ronnie suggested that since it kind of looks oversized like a bobblehead, I should turn it into an actual bobblehead. While it sounds like a simple request, I decided to take it one step further and actually make the head "bobble" when you hover over it!

The custom CMS (Content Management System) we use for our RustyBrick site is tailor-made to allow for users with no coding skills whatsoever to make great looking content. However, these same features can cripple creativity because we have no control over the profile page template itself, just the text content within it. Sure, I could have simply asked Ronnie to alter the template and allow for me to easily add in the bobblehead code for my profile, where's the fun in that? I needed to find a way to change the profile page without actually changing the code that 'creates' it and some quick jQuery allowed me to do just that.
 

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
window.$j = jQuery.noConflict();
jQuery(function($){
   $('.maincontent_right .status > img:first').hide();
   $('.maincontent_right .status').prepend('<div id="bobbleheadDiv"><img src="/pics/userpics/alex_bobblehead.png" id="bobbleheadImg" style="position:relative;z-index:9999;right:20px;"><img id="bobblebody" src="/pics/userpics/alex_bobblebody.jpg" style="position:relative;bottom:34px"></div>');
   $('#bobbleheadImg').mouseover(function() { $(this).attr("src","/pics/userpics/alex_bobblehead_move.gif").css("right","15px")}).mouseout(function() {$(this).attr("src","/pics/userpics/alex_bobblehead.png").css("right","20px")})
});
</script>


The first thing I needed to do is include the jQuery library. I simply added in the tag and hotlinked it from the Google Code Library. However, since the RustyBrick website uses a different Javascript framework called Prototype, I needed to add in the code enabling the setting of noConflict. Next, I created a new jQuery function to execute on page load. Finally, three simple lines of code within the function power the bobblehead effect.

The first line looks for the first image in the status div, which in this case is our original company profile picture. It then immediately hides it. Then line 2, adds some HTML code within the status div to input a bobblehead body image as well as a replacement head. (I photoshopped the bobblehead body from this Big Lebowski bobblehead and the original profile photo head is replaced to a transparent version to allow for it to 'sit on top' of the body.) Finally in line 3, two mouseevents are bound to the new replacement head. When you mouseover the replacement head, the source of the image changes which changes the photo to an animated GIF version of the head which "bobbles". Then, as you mouseout of the image, the source reverts back to the replacement head.

I found this method to be the easiest way to perform the bobblehead effect. With a little bit more jQuery magic, you could likely use jQuery itself to create the actual bobble movement. Instead of replacing the head with an animated GIF, you could try executing a function with uses the jQuery animate feature to bounce the head around. Try experimenting and see what you can come up with!

blog comments powered by Disqus

1 COMMENT

Alex worked on several successful e-commerce websites before joining the RB team. He graduated from SUNY Purchase with a Bachelor's in Math & Computer Science.

This article is under Geek Factor, Creative

There is 1 comment for this post

Connect With Us

Send Us a Message

Do you wish to give us feedback on one of our apps, send us a message or explore a proposal? Fill out the form below and we'll get back to you pronto!

Visit Us

250 West Nyack Road, Suite #200 West Nyack, NY 10994
Get Directions

Call Us Toll Free

877-GO-RUSTY
877-467-8789

Telephone

845-369-6869

Fax

845-228-8177