Simple jQuery Spy Effect
Posted on 2nd December 2008 — A few years ago Digg released a very cool little visualisation tool they dubbed the Digg Spy (it’s since been upgraded to the Big Spy). Recently Realmac Software released the site QuickSnapper to accompany LittleSnapper.
It’s the QuickSnapper site (the left hand side) that makes use of the similar spy technique that I’ll explain how to produce.

Watch
Watch the jQuery spy screencast (alternative flash version)
QuickTime version is approximately 60Mb, flash version is streaming.
View the demo and source code used in the jQuery spy screencast
Simple Spy
The great thing about Realmac’s QuickSnapper site is that if JavaScript is turned off, the list of snaps is visible by default. So we’ll follow suit.
It’s also worth noting that their version only keep pulling in new items until it hits the end. I’ll show you how you can keep the list looping, and in a follow up tutorial I’ll show you how to hook this in to an Ajax hit that doesn’t hammer your server and keeps the effect nice and smooth.
Development Tasks
I’ve broken down what needs to happen to be able to recreate this effect:
Setup
lielements (for running the effect).ulto only show Nlielements.Running the Effect
HTML
The HTML is very simple for the effect – since the non-JS version of the page the HTML appears the same, except with a longer list.
As such, rather than the complete listing, which you can see in the live demo, we’re just using a simple list element:
jQuery
For this example of code, we’re creating a reusable plugin. So I’ve wrapped our code in the follow pattern:
This allows me to reference the
$variable knowing that it won’t conflict with other libraries such as Prototype – because we’ve passed thejQueryvariable in to the function in the following line:Version 1: simultaneously height animate
This is the first version of the plugin. It resizes the first and last item simultaneously:
Version 2: fixed height
The second version has the following changes, and allows us to remove one of the animations, reducing the work the browser has to do.
We do this by created a fixed height wrapper around the
ul.spy, and it works because the styling is on an outerdiv.We change:
To add a line before that wraps our spy:
Then we need to comment out the animate height to zero.
We change:
To only remove the element:
Version 3: loop once
Though I didn’t cover this in the screencast, it’s simple to change the spy to run once, by changing the following:
To, if we’ve hit the limit, then don’t set a new timeout:
You should follow me on Twitter here I tweet about jQuery amongst the usual tweet-splurges!