1. Code
  2. Web Development

Adding to Our Leopard Desktop with jQuery

Scroll to top
Read Time: 15 min

Last Week I got you all to create a neat looking Dashboard/Desktop. You guys are gonna totally FLIP when you hear what's in this jam packed tutorial! More focus on the Dashboard (I swear it's cooler than it sounds, and requires a lot of code), and I'll even go into how to create a stack (seperate from the dock, sorry jqDock doesn't like nestled <ul>s), and some extra little bits to make it all click.


As with the previous tutorial, I must note this disclaimer! I don't own any of the images used in this tutorial, nor do you. They are copyright to their vendors, whether it be Apple, inc, Adobe, etc. Using icons is a bit of an integrity issue, so don't abuse it!

Secondly, a new jQuery.UI file will replace the draggable js file. This is basically all the interaction packs. Download jQuery.UI code. You'll also need the final product of last week's tutorial! Make sure you expand that into it's own directory! We'll be adding onto that. A whole lot if images are needed too. New Images. Make sure you expand that zip into the 'images' directory, so that any new folders are merged with their counter parts from last week. I apologise for the confusion with this. Stupid file structure, my bad. So. Files that need adding:

Just the same, the jQuery.ui links need editing.

Plan of Attack

Though it might not look like it, a whole lot of code is needed for these few things:

  1. Stacks
  2. Dashboard
    1. Opening/Closing the Adding Widgets Panel
    2. Dragging Widgets onto the Dashboard list
    3. Closing Widgets
  3. Some extra bits (improving dock, adding desktop items)


Just before we start, I really do apologise, but there were a few things that needed changing from last week. the #dock css should read:

The #closeZone's zIndex in dashboard.js on line 24 should be 99 not 9999

Step 1 - Stacks

So lets dive right into it, and start with Stacks. Or more a Stack. Unfortunately due to the way that jqDock works, it's impossible to nestle stacks within the jqDock without editing the core js, which is much further than this tutorial intends. So instead, we'll be creating a stack to the bottom right of the page. The harder parts of coding stacks is a) the incrementing height for each item, and the curve. Luckily, a loop combined with maths can do this hard work for us.

Step 1:1 - HTML

Lets begin with adding the HTML structure of the stack. Now due to the nature of the stack, if you wish to use it on a different website, you can! Basically anything inline within the <li>s work. The positioning just needs tweaking. Regardless, we use spans (and you could always wrap the images in <a>s!

The first image is the folder placeholder. This is what activates the dock, so it's necessary. (When we use the jQuery selectors, however, I'm sure you could use :first to get the first dock item if you /really/ don't want a containing basket).

Step 1:2 - CSS

Contrary to the first tutorial, I'm going to include the CSS and jQuery for each step, just so the design doesn't muddle up completely. Open up style.css from last week and add to the bottom:

Your stack will now look like a closed stack, but you can't open it. This just stacks (hah, no pun intended) all the icons on top of each other, so they're compressed into a small square. The last selector is for the jQuery. When the stack is opened, the class 'openStack is added to the ul. I apologise to those CSS3 haters, It's the fastest most efficient way to get it right in most modern browsers.

Step 1:3 - jQuery

In plain english, the stack needs to open when the img is clicked, pushing each (hint...) li up in incrememnts, and to the right a little, whilst resizing to a smaller size. Then when clicked again, everything returns to normal.

The second function is easy, but the first is a pain.

Woo, jampacked with string interrupting, variables and math. Interesting selectors, huh? The ~ is 'siblings of' Erg. Math. Let me explain. The first 2 variables are for the vertical position and the horizontal position (curve).

Top incrementing is the same each time, where as unless you want a horizontal straight line, each horizontal position has to be slightly more than the rest. In this case, it increases by the previous number plus one, times 2. so it'll go 2, 6, 14, 30, 62, 126, etc. I know they're weird numbers, but it works. Use any equation you like!

The 'each' function is similar to, say a WordPress loop. This function happens every time the next element is used. The equation 'value = (value+1)*2', means 'new value equals old value plus one, then this times two.

The first animate line adds the variables (within the plus) every time it's looped via string splitting. The last two lines are just the size. The other half of the toggle function, just resets everything back to normal:

Simple! Now your jQuery stacks will successfully animate, even curving! Unfortunately rotation is a tad more difficult. Though when HTML5 comes out in 2022 (-_-) the canvas tag might have full support for that.

Step 2 - Dashboard

So we're gonna add to the Dashboard a bit. Firstly, an Add Widgets Panel (Wont do the actual adding til later). After that, closing of the widgets will be possible when this panel is open. Finally, being able to add your own widgets from this panel. Uses some very different selecting methods. Adding the widgets also covers Droppables extensively, as the drop function is rather large.

Step 2:1 - Add Widget Panel

Firstly, the HTML. Add this just before the closing #dashboardWrapper div.

The 'openAddWidgets' is the little cross/plus that opens and closes the Panel. The list items are the available widgets (create as many as you'd like!). The images you downloaded are the little thumbs. These will become draggables, and you will be able to drop them onto the #closeZone and eventually, widgets appended to the #widgets list.

At the moment, this looks like a bit of a mess;

But with some CSS we'll fix that right up.

Heavy on with positioning, z-indexing and floating, this should gather an effect like this (Panel's there, not hidden):

Finally, the jQuery to hide and show it. Add the following under the comment '//draggables definition' (for the sake of organisation):

And add this under the '//initial hiding of dashboard + addition of 'closeZone'' block:

Now for the toggle code. In english, when the 'open' button is clicked, slide the panel up. When it's clicked again, slide the panel down. Let's start with the toggle.

Thus the opening function will be in the first gap, whilst the closing in the second. The first:

And the second, reversing above:

Finally, like Leopard, it should close when the user returns to the Desktop, right? Add this to //#closeZone's job: closing the Dashboard' function (within it!):

Now if you click the little plus in the bottom left when the Dashboard is open, it should animate! Awesome!

Step 2:2 - Adding Widgets to the Dashboard List

This proved a beast and a half. Lots of code for this... Yay! Luckily, It's only jQuery! Let's start by defining the Droppable; #closeZone. Place this under the Draggables definitions:

Basically, #closeZone can now accept the thumbs in the Panel as droppables, and we're about to delve into what happens on the drop.

In understandable language, this is how it goes. Variables for the mouse position need to be found so the position of the drop can be where we want it. Another variable for the type of widget to append is needed. On drop, the Widget needs to be appended, a different image depending on the widgetType variable. Now to be different, the stickies will be editable (No way!). A textarea will be appended, to allow writing. Because all the draggable definitions happen on the load of the document, they'll need to be re-defined every time a widget is appended to the DOM, so that it's applied to the newest one of such.

We'll start with the variables.

Sadly we can't get the width and height of the image that's about to be appended too easily (to center the drop). So instead we need to guess, by offsetting the position of the mouse by 100 and 50, so it's not in the top left. The JavaScript variables 'cleintX' and 'clientY' are basically the mouse position. And that interesting selector; ui.draggable, is the element that has just been dragged! Thanks jQuery.ui! Now for appendage:

Allow me to explain how the variables work in the appending. To give a class to the new widget for customisation, adding "...'+widgetType+'Widget'..." will return a class similar to 'stickyWidget' or 'weatherWidget'. The inline style (so sorry it's inline! Don't shoot me!) determines the absolute position of the widget by the variables, which are of course the mouse coordinates. As I mentioned, the newest DOM members need any jQuery modifications or appendages [made on the document load] re-applied, as jQuery doesn't recognise new DOM members. For the last two lines, jQuery needs to append the textarea (so you can edit the text) and the new Widget needs to become a draggable.

In order for all this to work, some CSS is needed. Replace, in style.css, the '.widget' selector and attributes with:

This makes the sticky all looking like a sticky. The you either will or wont have the Marker Felt font, that's what the actually sticky widget uses. For the original widget to remain pretty, wrap the text, rather than in <p>s but with:

And give the li an extra class of 'stickyWidget' to match the css (The li'll now have 2 classes).

All goes according to plan, you should now be able to a) edit stickies, and b) add new widgets to the Dashboard.

Step 2:3 - Closing widgets

Why give this part a whole section to itself? Because the workings of this are weaved into all of the previous functions, clicks, and appendages. So instead of being confused adding this throughout all the parts, why not keep it in one?

Right. So basically a small span'll be applied to widgets when the panel opens, and when a new widget is added to the Dashboard. When this is clicked, the parent widget will disappear! Awesome, huh? When the Add Widgets Panel closes, the crosses will disappear into the realms of .hide().

Working down the document for integrating the close button, we begin with the #closeZone's function. Underneath the #addWidget's disappearing act (code), add:

Next up, within the droppable definition. This snippet of code will apply a close widget button and it's function to all widgets when a new one is dragged on. Underneath the newest draggable definition for the newly created widget (within the drop function), add:

Finally, the open/close panel function is where it really matters, as this will append the thing to all Widgets on open of the Panel (like Leopard). Below both animates, add respectively:


Now, when the Panel is opened, a little clickable cross goes to the bottom right of the Widget, and when you drag a new Widget on, it'll seemingly duplicate. To fix all this, add this CSS:

And TADA! You now have widgets that close, and can reform when you want them too! Awesome stuff!

Step 3 - Some Extra Bits

This is really just for visuals sake, but we'll be adding a Desktop Item that you can create your own function when double clicked, and making the Dock a bit speedier.

Step 3:1 - Desktop Item

Add some HTML, make this the first thing after the opening #wrapper div:

Give it some CSS to look snazzy:

And finally some jQuery to execute your double-click function (honestly, change the alert to whatever you please):

Step 3:2 - Improve the Dock

So last week some of you complained about the clunky Dock, and I maintain there isn't a lot I can do about it. However, to trick your eyes to think it's smooth, you can increase the speed. Simply change the jqDock declaration in dashboard.js to:

And now you should have a faster dock!

Wrap Up

What a mighty tutorial to write... That was tough. But hey! We did it! I'll just use this space to note a few things from last week that came up in comments.

IE. That Bastard. Shame on jQuery too, for not being cross browser like it's meant to. I get the feeling from some of you that complain think my code is shoddy in the regard that it doesn't work in YOUR browsers that YOU code for. I wrote an article about it on my website discussing browser specific coders. Obviously I know that you should be adept at all browsers... But nobody's perfect.

Practability. Obviously, this is just meant to be fun. Stacks might be an option for a site, but ultimately it's meant to be fun. Let me quote a comment from last week (Not shameless promotion I promise!).

Thanks for taking the time to write this tutorial, jQuery is great and its nice to take some time as developers and have some fun with all the code libraries floating around. Lighten up people and have some fun with it, it isnt meant to be practical, just fun and inspiring. Great tut.



I think that's it. Hope y'all (No I'm not TEXAN!) enjoyed this tutorial, wasn't too hard to keep up with, and are gonna get straight back into practical thinking right now!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.