Get Slick with MooTools Kwicks

By  on  

I've created an easy to use MooTools Kwicks plugin which you may view here.

When I first saw MooTools graphical navigation, I was impressed. I thought it was a very simple yet creative way of using Flash. When I right-clicked and saw that it was JavaScript, I was floored. How could they achieve such a fluid effect using just JavaScript? There began my love of the library and ever since I've used MooTools.

When it came time for my employer to redo their website, I made it my goal to really jazz up the site with MooTools. Nothing too fancy and nothing too bright, just some simple effects that show attention to detail and a little bit of fun. I then thought it was the time to use MooTools "kwicks" to give the website a little kick.

Step 1: Create Images

You'll need an image for kwick, obviously. Make them all the same width. Below are my images, all 285 pixels x 143 pixels. Black borders have been added to show you image dimensions.

Step 2: The HTML

Showing the HTML first will give you a good idea of how kwicks are structured.

<div id="kwick">
	<ul class="kwicks">
		<li><a class="kwick john" href="http://en.wikipedia.org/wiki/John_lennon" title="John Lennon"><span>John Lennon</span></a></li>
		<li><a class="kwick paul" href="http://en.wikipedia.org/wiki/Paul_mccartney" title="Paul McCartney"><span>Paul McCartney</span></a></li>
		<li><a class="kwick george" href="http://en.wikipedia.org/wiki/George_harrison" title="George Harrison"><span>George Harrison</span></a></li>
		<li><a class="kwick ringo" href="http://en.wikipedia.org/wiki/Ringo_starr" title="Ringo Starr"><span>Ringo Starr</span></a></li>
	</ul>
</div>

Step 3: The CSS

There's minimal CSS involved in this process so don't worry about stylesheet bloat.

#kwick				{ width:590px; }
#kwick .kwicks 			{ height:143px; list-style-type:none; margin:0; padding:0; }
#kwick li 			{ float:left; }
#kwick .kwick 			{ display:block; cursor:pointer; overflow:hidden; height:143px; width:134px; }
#kwick .kwick span 		{ display:none; }

#kwick .john 			{ background:url(kwicks/john.gif) no-repeat; }
#kwick .paul 			{ background:url(kwicks/paul.gif) no-repeat; }
#kwick .george 			{ background:url(kwicks/george.gif) no-repeat; }
#kwick .ringo 			{ background:url(kwicks/ringo.gif) no-repeat; }

Note the width of the container (#kwick) is not simply all of the image widths added up. I've experienced odd "wraps" with quicks where they fall to the next line. To prevent that I've added one pixel per kwick to the #kwick width. Also note that a kwick's width is set to 134 pixels -- that becomes default width of all kwicks when no kwick hovered over. You also need to declare a class for each kwick, defining its background image.

Step 3: The MooTools 1.2 JavaScript

The JavaScript can be a bit daunting, but the below code needs very little modification to work for you:

var Kwix = {

	start: function(){
		Kwix.parseKwicks();
	},

	parseKwicks: function(){

		var squeeze_to = 100;
		var max_width = 285;

		//get original widths
		var start_widths = new Array();
		var kwicks = $$('#kwick .kwick');
		var fx = new Fx.Elements(kwicks, {wait: false, duration: 250, transition:Fx.Transitions.Cubic.easeOut});
		kwicks.each(function(kwick, i){

			start_widths[i] = kwick.getStyle('width').toInt();

			//mouse is in, squeeze and expand
			kwick.addEvent('mouseenter', function(e){

				var obj = {};
				obj[i] = {
					'width': [kwick.getStyle('width').toInt(), max_width]
				};

				var counter = 0;

				kwicks.each(function(other, j){
					if (other != kwick){
						var w = other.getStyle('width').toInt();
						if (w != squeeze_to) obj[j] = {'width': [w,squeeze_to] };
					}
				});
				fx.start(obj);
			}
			);
		});

		//mouse is out, squeeze back
		$('kwick').addEvent('mouseleave', function(e){
			var obj = {};
			kwicks.each(function(other, j){
				obj[j] = {'width': [other.getStyle('width').toInt(), start_widths[j]]};
			});
			fx.start(obj);
		});
	}
};

//lock and load!
window.addEvent('domready',Kwix.start);

You only need to edit two lines of code in this example: 9 and 10. Nine defines the "squeeze" width of each kwick: how slim do you want blurred kwicks? Line 10 defines the maximum width of a kwick, which you'll likely want to be the full width of your image.

That's All?

Absolutely! Your navigation goes from...

The Beatles

...to...

The Beatles

Recent Features

Incredible Demos

  • By
    Morphing Elements Using MooTools and CSS

    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 The block of content that will change is...

  • By
    HTML5 Input Types Alternative

    As you may know, HTML5 has introduced several new input types: number, date, color, range, etc. The question is: should you start using these controls or not? As much as I want to say "Yes", I think they are not yet ready for any real life...

Discussion

  1. Thanks for this. I’ve followed and used Mootools since it was just Moo Fx (I think – the memory’s not what it was) and they always seemed lightweight, easy to use and fast to me. Just as much so as jQuery, yet that’s all you hear about from designers and developers these days. “I tried Scriptaculous/Dojo/Ext.Js but I prefer jQuery.”

    Is there something they know that we don’t or is this purely a Mootools marketing issue, do you think?

  2. @evilhuman: A little of both. jQuery has “evangelists” on the team while MooTools has the “use it if you want” attitude.

    They’re both great javascript frameworks — I simply prefer the Moo!

  3. I think a lot of developers don’t realize that “the Moo” has evolved far beyond an effects library. That’s what it was when I had first checked it out, and it wasn’t until very recently that I discovered that it had become much more.

  4. Yeah its a lot more these days. And David, I agree, jQuery is good, and similar in a some ways, but Mootools has the edge for me. In fact, in my head I class jQuery and Mootools as the same “sort” of framework, as opposed to the others out there which don’t seem as efficient in my limited exposure to them.

  5. I totally love this effect and I have implemented it on several websites i’ve built. I guess you beat me to posting it!

  6. Oh yeah, did anyone mention that this degrades perfectly?

  7. @Eric: Don’t let my post stop you from posting it on your site!

  8. Is this using the accordian effect?

  9. @Mark: Nope, the accordion is a different set of code.

  10. I’ve been playing with this recently, and to be honest, it’s more powerful than this.You can make it SEO friendly, you can use the images/text as content (not hiding it from view, not using CSS). If I’m suddenly blest with free time I may have to do a proof-of-concept to take your example up a notch! :)

  11. @keif: I wanted to provide a really simple example for those that don’t need a lot more. I look forward to reading what you put together!

  12. @David: No worries.

    I think someone should put together a multi-line version of this. Maybe I could do it after this weekend. At some point that might get difficult to use for a user though.

  13. Chad Smith

    I’m so glad to see MooTools on here. I used a few of their effects on a couple of my sites. Very easy to get up and running!

  14. Great job, I’ve already seen that kind of menu but not like this one. I loved the space that you’ve left in the photos, so you can read it :).
    Feel free to post in my blog.
    See you on the next post, I’ve you in my RSS reader :)

  15. Gotta love the Kwicks. I came across the MooTools library ages ago and I implemented this quite early on in a project I’m working on- http://am0.co.uk/testinggrounds/martin/ is the link, and it has a fantastic visual presence on the front page of any website.

  16. @alex: it still falls in the trap of using images for content. I’d replace that image with text.

    This is my QUICK AND DIRTY mock-up of what I mean – there is still a LOT of tweaking that could be done to it, effects to be added, etc. etc. but I just wanted to see about duplicating it withoutresorting to background images.

    http://ikeif.net/mootools/beatles.html

    It’s acting funky in IE6 (probably CSS issues). It’s also using mootools 1.1.

  17. This is really sweet. I am just starting to learn javascript and programming in general. After a day of poking around tutorials, starting and ending with this one, I was able to recreate this menu. My biggest difficulty was figuring out where to put the javascript, whether in the head section or in the body section or as a separate js file. I was able to get it working by adding the script tags in the head section or as a separate file without them.

    Thanks again! I’m looking forward to being able to (maybe) implement all the ideas I have for this and mootools in general.

  18. @Valerie: Here’s one thing to keep in mind when it comes to MooTools and other javascript frameworks: make it your goal to keep all javascript in the header.

  19. Is linking to an external file preferred over putting the script in the header?

    Thanks again.

  20. @Valerie: I get as much “library” javascript into the external file as I can for caching purposes. Some things need to be within the top of the page though. My previous comment referred more to using MooTools’ addEvent('mouseenter') instead of the element’s onmouseover attribute.

    Hope this helps!

  21. Thanks for the clarification and for your time. You’ve got a lot of great info here and the site design is great.

    Now back to reading Bulletproof Ajax:)

  22. Korjuin

    Pretty cool effect!
    Unfortunately, I tried to center the background-image in the ‘s but Firefox messes it up when you enter an item from ‘the right’. It stretches the the right-half of the image. Any clues how to solve this problem?

    I have another question: is it possible to add a class="selected" to an item? I’ve noticed that it gets a class="active" onMouseOver, but the script overrides any inline classes of this kind.

    Thanks.

  23. @Korjuin: Can’t give any analysis without a link — have an example somewhere?

  24. Korjuin

    David,
    Here’s an example.
    http://demo.7u.nl/tmp/navtest.htm

    Thanks.
    korjuin

  25. @Korjuin: Nice work! It’s a little glitchy in Windows/Firefox, but nice job.

  26. Korjuin

    Thanks,
    That’s the thing with Firefox. When you hover on an item, wait ’till it’s fully stretched and then select the item left of it, it tends to stretch the right area of the image instead of keeping the image centered.

    Any idea how to tackle this?

  27. @Korjuin: No idea — simply seems like a browser issue to me.

  28. Korjuin

    @David,
    Okay, well centering is nice but not necessary for me.
    Another question: is it possible to make one item ‘selected’. So, when I’m on page #4, nav-item 4 is fully stretched from start?

  29. Korjuin

    @David,
    I managed to add something to script so that one item is ‘selected’ when opening the page ( see: http://demo.7u.nl/tmp/navtest.htm), but once you’ve rolled over another item the selected-mode doesn’t return to its original status.
    The jquery script overrides it.

    Any idea?

  30. Is there a way to make this bad boy default to one image being “open” versus another? I’d like this to be my main nav and have different things “open” by default as the year goes on.

    That is, for three months it’s “George” and three months after that it’s “Paul”.

  31. @Mike: Good question. I’ve done this before, but don’t remember exactly how I did it. I believe you want to start the “featured” element’s CSS class (“#kwick .paul”, for example) to a wider width and the others to a lesser width. They should snap back to that original position.

  32. Gareth Hughes

    Hi,

    Need some help, I am trying to get the kwicks to work, I have copied and pasted the information provided and changed necessary requirements but still cannot get it to work. Apart from the javascript provided do I have to attached another java file?

    Kind Regards,

    Gareth Hughes

  33. @Gareth: You need the MooTools library from mootools.net .

  34. dabobo

    Can you please tell me what I am doing wrong? I uploaded the mootools to my server, and I thought that I had inserted the code correctly, (using the code from the demo). I think that I’m doing something wrong with the math (width and height )of the images?
    http://counterpunchtrading.com/

  35. Denise

    Hi,

    Is it possible to use html content instead of images?

    Denise

  36. dear david

    thanks a million for your excellent tutorial. based on your explanation and the former mootools-frontpage, i developed a slightly different flavour of the kwick-menu. instead of a fixed width, i needed each menuitem to have a different width, depending on the content. combined with a nice css-rollover-effect it works like a charm. thanks again and keep up the good work. all the best from leipzig, germany

    uli

    ps: if you wanna see for yourself, check out http://www.greimdesign.de (website for a furniture designer)

  37. hıms it is script very good. i use it pictures scripts

  38. cd

    Anyone have any luck getting this to work with a version of MooTools greater than 1.2.0? Since Google AJAX Libraries are only provided for 1.2.1 and above, I’m trying to find a way to get these to work together.

  39. @cd: What error have you encountered?

  40. cd

    Result of expression ‘Fx.Elements’ [undefined] is not a constructor.

  41. cd

    Adding MooTools More 1.2.3.1 with only Fx.Elements and More components fixed the problem.

  42. R. bruece

    The mootools licensing looks like it can’t be used for commercial work. Am I misinterpreting it when I take that to mean any paid work? I would think that if your employer paid you to produce the website, and the site is a commercial site you can’t use mootools. Is it a commercial use if the site is internal to your company–which is a commercial concern?

  43. @R. bruece: That’s incorrect — you may use MooTools however you’d like as it’s released under the MIT License.

  44. R. bruece

    Thanks.

    I was looking at the accordion documentation and it said non-commercial license and I mistook it to apply to MooTools itself–but it only applies to the documentation.

  45. Loque

    I’m sorry, but coming from jQuery to tools for a project I’ve really found that jQuery has the edge… if it isn’t the documentation its the user base, if it isnt that its the fact we are using mootools 1.1 and to upgrade requires a major overhaul of a huge JS based project, if it isnt that then its the readability and simplicity of jQuery… I mean.. is it $, $$, $E, getElements, getElement (i know now but it takes a bit of digging to find out which you should be using) and the accordion plugin… omg, kill it! hahaha, just my 2 cents! =`)

    Saying this, the class based stuff seems nice, and bind seems useful also… but its not much… and the documentation I find slows me down so much :( its getting labeled muletools… or pootools, when i know in my heart is anything but… just need to sort out the dam documentation!!! :|

    A well, thx for you demo, it kinda shows me how to use the Fx stuff… wish there were more people like you showing how you go about using the tools…

    sorry for the rant :`P

  46. mesocraig

    Trying to add a div containing a swf into kwicks. see –> http://turtlepens.com/about-2/ for details….the reason in doing this is that is has to include some flvs of interactive animations. Apart from a few flickers it seems to display ok in chrome and IE but the flash sits on top in firefox. Any tried to add a swf into kwicks before?

  47. I made a menu using the kwicks plugin and it works great in Safari, but doesn’t show up at all in Firefox or IE….any reasons why not? HELP!

  48. Im going to use this in one of my sites!
    Im exited!

  49. hi
    i’m wondering what is the use of the //lock and load part?

  50. hi
    i tried copy and paste your code in fiddle, (i also made the pics link absolute), and there is one difference with what’s on your site i wish i could solve:
    on your site, however quickly we get the mouse out once it touched an element, the element closes. however, on my site and on fiddle, if the mouse is removed from the element BEFORE the 250ms duration expires, the element stays open…
    i really wish i knew how you did for the element to close whatever happens, i’ve been looking for one day almost now…

    here’s the link of the fiddle
    http://jsfiddle.net/api/post/mootools/1.4/dependencies/more/

  51. found the solution:
    it comes from the function wait:false which has been abandnned in mootools v 1.3, it has to be replaced by link: ‘cancel’ and it behaves in mootools 1.3 and 1.4 just like in 1.2

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