Auto-scrolling Parallax Effect without JavaScript

Here’s another quick CSS3/WebKit trans­itions pro­ject in the con­tro­ver­sial realm of CSS anim­a­tion. This time I have opted to recre­ate the pop­u­lar par­al­lax effect using mul­tiple back­ground images on a single ele­ment and the -webkit-transition prop­erty (doc­u­ment­a­tion). I have based this on Chris Coyier’s par­al­lax tutorial, reusing the star images with per­mis­sion, the tech­nique itself was coined by Paul Annett (explan­a­tion on Think Vit­amin). If you’re not quite sure what par­al­lax is, then Chris and Paul both go into some depth to explain it and Wiki­pe­dia is always helpful.

Res­ult

Exper­i­ment: Auto-scrolling CSS3 Par­al­lax Effect
Exper­i­ment works in Safari 4 Beta and Google Chrome. No JavaS­cript necessary.

Correctly rendered background images for parallax effect

How To

The HTML markup is fairly simple, one DIV for the back­ground and another for the con­tent, the example uses CSS3’s mul­tiple back­grounds, so no need for extra markup to accom­mod­ate all those other images:

<div id="background"></div>
<div id="content">
	Content
</div>

For the CSS the back­ground con­tainer is set to a fixed pos­i­tion (for con­veni­ence more than any­thing) and spread across the bot­tom of the page using the top, left, bot­tom and right prop­er­ties. The back­ground images are defined using the back­ground short­hand prop­erty with mul­tiple declar­a­tions being comma delim­ited, the first being the top-most. Each of the images has a dif­fer­ent pos­i­tion defined in per­cent­age, so as the size of the con­tainer changes (e.g. on win­dow res­ize) the images move dis­pro­por­tion­ately to each other; cre­at­ing the impress­ive par­al­lax effect.

#background {
	background: url('../images/foreground.png') 5% 5%,
		url('../images/midground.png') 20% 20%,
		url('../images/background.png') 90% 110%;
}

Ordin­ar­ily this effect is only seen when the page is re-sized or JavaS­cript is used for anim­a­tion. My first approach to anim­a­tion via CSS was to apply the trans­ition to the background-positions, with background-position being an animat­able prop­erty as defined in the pro­posed spe­cific­a­tion. How­ever this doesn’t yet work in the latest Web­Kit nightly build (r42142), it is a known bug.

As an altern­ate route, albeit a tem­por­ary one, I have opted to use trans­itions to anim­ate the left-most edge of the back­ground con­tainer (for instance from 0px to –100px). This gradu­ally alters the over­all width of the con­tainer caus­ing the back­grounds to shift dis­pro­por­tion­ately as per their per­cent­ages, cre­at­ing the par­al­lax effect. With a large enough dur­a­tion and left pos­i­tion the effect appears to be continuous.

#background {
	left: 0;
	-webkit-transition: left 300s linear;
}

#experiment:target #background {
	left: -5000px;
}

To make things a bit more fun I’ve increased the ‘fly­ing speed’ when the mouse hov­ers over the back­ground area. The final CSS looks like this:

#background {
	background: url('../images/foreground.png') 5% 5%,
		url('../images/midground.png') 20% 20%,
		url('../images/background.png') 90% 110%;
	top: 218px;
	left: 0;
	right: 0;
	bottom: 0;
	position: fixed;
	-webkit-transition: left 300s linear;
}

#experiment:target #background {
	left: -5000px;
}

#experiment:hover #background {
	left: -9999px;
}

5 Comments

  1. Posted April 2, 2009 at 7:09 am | Permalink

    very cool, I didn’t know trans­ition was act­ive in web­kit yet

  2. Posted April 2, 2009 at 10:22 am | Permalink

    Nice sample — hope it’s okay I linked to it over at Web­Kit­Bits.

    Have you con­sidered using “-webkit-animation-iteration-count: infinite;” — I could see that work­ing well.

  3. Paolo
    Posted April 3, 2009 at 1:07 am | Permalink

    Very nice demo! It works with the web­kit ver­sion of epi­phany under Linux as well.

    To install it on ubuntu:

    sudo apt-get install epiphany-webkit

    or use the pack­age man­ager, with way more clicks ;-)

  4. Posted April 3, 2009 at 6:18 am | Permalink

    Well, quite a mis­lead­ing post title. What about being hon­est and rename it to:

    “Auto-scrolling par­al­lax effect with pro­pri­et­ary Web­kit CSS3” ?

    Do not get me wrong, I do admire people like you who take the time to share their find­ings, I’m try­ing to do it myself. But I dis­like bait URLs.

  5. Keith Pickett
    Posted April 4, 2009 at 7:38 am | Permalink

    I have to agree with Arnaud. While I really like this effect, it would have been more useable to me that it was titled spe­cific­ally to CSS3/Webkit. It doesn’t work in FF and I assume IE. I have to develop on a plat­form that is com­mon to all browsers.
    Be that as it may, it’s a great piece of code.

5 Trackbacks

  1. By Ajaxian » Auto Scrolling Parallax Effect in CSS on April 2, 2009 at 10:01 pm

    […] Hayes is off doing more fun things, this time cre­at­ing an auto scrolling par­al­lax effect with CSS, spe­cific­ally using mul­tiple back­ground images on a single ele­ment and the –webkit-transition […]

  2. By AaronRutledge.com » New links for April 3, 2009 on April 3, 2009 at 10:11 pm

    […] Auto-scrolling Par­al­lax Effect without JavaS­cript Nice par­al­lax effect. Works in latest Safari and FF3.1 betas Pos­ted 04 – 04-09 » Links […]

  3. By Best of Week (+/-) #10 | Insel der Engel' on April 11, 2009 at 3:16 pm

    […] Auto-scrolling Par­al­lax Effect without JavaScript […]

  4. […] Paul Hayes took that example and ran with it. He used the extremely cool –webkit-transition attrib­utes to make the par­al­lax effect move without […]

  5. By CSSハックマニア その1 | Nutspress on September 30, 2009 at 7:49 pm

    […] & Text hover Pro­fes­sional dropline with images… Markup Hier­archy – Advant­ages in SEO Auto-scrolling Par­al­lax Effect without JavaS­cript Tips for Devel­op­ing Semi-Liquid Lay­outs CSS Light Box – Click […]

Post a Comment

Your email is never shared. Required fields are marked *

*
*