Fun with Overflows
Posted on 16th October 2008 — Making use of the overflow and scrollLeft DOM property to scroll elements is a much more effective use of the CPU, over animating using CSS top/left. So this episode of J4D demonstrates the same effect used in two completely different ways.

Background
The first is a scrollable timeline. A couple of readers requested a demo of how Plurk’s browse timeline works. In addition, in the last month, Google released a 10 year timeline – so I wanted to show how this works.
The second was a request from Trevor Morris who’s involved with/runs Geek in the Park. He asked whether the techniques I used in a jQuery marquee plugin I wrote recently could be used to smooth out CPU spikes that were occurring on his site when the header pattern flowed (see example below)
Watch
Part 1: Scrollable Timeline
Watch the scrollable timeline screencast (alternative flash version)
QuickTime version is approximately 60Mb, flash version is streaming.
View the demo and source code used in the scrollable timeline screencast
Part 2: Trovster’s Header Effect
Watch the Trovster’s header effect screencast (alternative flash version)
QuickTime version is approximately 45Mb, flash version is streaming.
View the demo and source code used in the Trovster’s header effect screencast
Scrollable Timeline
For the scrollable timeline I wanted to support both the Plurk version where the user could use their mouse wheel to scroll and the Google version where they could click and drag.
I’ve taken a large chunk of Google’s history to demonstrate the effect.
We set up the page by creating a wrapping
divthat hasoverflow: auto;(which we’ll change tooverflow: hidden;using jQuery later on). The inner element, theulin this particular case, is styled to have a width that can accommodate all the nestedlis side by side without wrapping on to a ‘new line’ (this is done by floating thelis left and giving them a defined width).Understanding the Problem
The task is such:
CSS
I won’t cover all the CSS used, only the key style:
jQuery
This is the full code listing used in the demo.
We attach 3 built in mouse events: mousedown, mouseup and mousemove. Then we add the jQuery mousewheel plugin before changing the overflow CSS:
Trovster’s Header Effect
Using the
this.scrollLeftDOM attribute again, we can create a completely different effect.This effect and design was created by Trevor Morris, but the first version he had took up a lot of CPU by changing the CSS left position on the ‘rainbow’ image.
The version I cover in the screencast uses overflows and absolute positioning to keep the CPU usage and the effect (still) smooth in addition to working to make it appear the same if JavaScript is turned off.
Markup
The extra empty
divis the wide element and thediv#rainbowis the element with the overflow that will scroll.CSS
In addition to the following CSS, I also hand coded the PNG transparency for IE6 to work when JavaScript is disabled.
In addition I’ve included IE6 specific styles:
JavaScript
Note that I’ve called this section JavaScript rather than jQuery, since we’re dealing with the
scrollLeftproperty on thediv#rainbowelement we only ever need jQuery for the ready event:Taking it Further
I would love to see what else you can do: the Coda Slider is another example of the overflow and scrollLeft being used in the same way to create a completely different effect.
What other ways can the overflow/scrollLeft combo be used?
You should follow me on Twitter here I tweet about jQuery amongst the usual tweet-splurges!