JavaScript
Article

An Introduction to Parallax Scrolling Using Stellar.js

By Aurelio De Rosa

One of the most discussed web design trends of the last few years is the parallax scrolling effect. Whether you like it or not, it is used by a lot of websites. In this tutorial I’ll give you a brief introduction to parallax scrolling and show how we can reproduce it in a website using a jQuery plugin called Stellar.js.

What’s Parallax Scrolling?

Parallax scrolling involves the background moving at a different rate than the foreground, creating a 3D effect as you scroll down the page. This effect can be a nice addition to any website, but unfortunately if abused it can be quite annoying. From time to time you’ll find websites completely powered by this effect and your experience won’t be graceful. Because this effect usually animates images in the background, the total weight of the website is heavily increased, resulting in a very slow load.

Some examples of such abuse, in my opinion, are the Saukoni website that presented the Kinvara 3, and the well-known Oakley – I am invincible website whose weight is ~20Mb (previously it was ~50Mb!).

Now that you have an idea of what this effect looks like, let’s see how we can employ Stellar.js to create it.

What is Stellar.js?

Stellar.js is a jQuery plugin that allows you to easily add a parallax scrolling effect to your website. Despite the fact that it’s no longer maintained, it’s still very stable, compatible with the latest versions of jQuery, and a lot of developers employ it in their websites. This jQuery plugin is popular on the jQuery plugin registry, so you may have already heard of it.

Now that I’ve described what this plugin does, let’s see how you can use it in your website.

Getting Started with Stellar.js

Getting started with Stellar.js is very easy. The first step is to download the plugin and include it in your page. You can obtain Stellar.js by visiting its GitHub repository or through Bower. If you want to employ Bower, you have to type the command:

bower install jquery.stellar

Once you have downloaded it, you have to include Stellar.js in your page using the usual script element after you have included jQuery as shown below:

<script src="path/to/jquery/jquery.min.js"></script>
<script src="path/to/jquery.stellar.min.js"></script>

After completing this step you’re all set up to adopt a parallax scrolling effect in your pages. This plugin allows you to apply the effect to any scrolling element, for example the window object or any other element you may have. To do that you have to select the element using jQuery’s constructor and then invoke the stellar() method on it.

A minimal example that employs this library upon the window object is shown below:

$('#someElement').stellar();

For the window object, the library also provides a nice shorthand, shown below:

$.stellar();

At this point the library will search for any parallax backgrounds or elements within the specified element and reposition them when the element scrolls.

If you want an example of a page that uses Stellar.js to create a parallax scrolling effect you can take a look at this one.

Options

Stellar.js, like many other plugins out there, has a certain degree of flexibility. In fact, it allows you to set several parameters and tweak how the plugin works to fit your needs. Stellar.js allows you to define general options, which are options used for every element, and per-element options. The general configuration you want to set must be passed to the stellar() method when you invoke it, while the options on a per-element basis are set through the use of some data-* attributes. In this section I won’t cover all of the options available, so if you’re curious to investigate further, you can read this specific section of the repository.

The first general options you can define is to decide the direction you want the effect to be allowed. The classic scroll effect is from the top to bottom and vice versa, but you can also specify a left to right effect and vice versa, or even both. To do that you can adjust the two Boolean properties horizontalScrolling and verticalScrolling. Their default value is true.

Another interesting option is responsive. It’s used to specify if the parallax content must be refreshed when the load and the resize event are fired on the window object. Its default value is false.

The last general option I want to discuss is hideDistantElements. The default value of this property is true and specify that you want to hide parallax elements that move outside the viewport. If you don’t want such behavior you have to set this option to false.

The only per-element option I want to cover, because it’s used often, is data-stellar-background-ratio. The latter accepts a positive number as its value and allows you to change the speed of the effect on the element it is applied to. For example data-stellar-background-ratio="0.5" means change the speed to a value which is half the natural scroll speed. If you want to use this attribute with values lower than 1, the documentation suggests to set background-attachment: fixed; in the style of the element.

Now that you know what this plugin does and how you can configure it, it’s time to see it in action again.

Demo

In this section we’ll develop a demo that use the Stellar.js plugin and some of the options we’ve discussed in the previous section. First, we need to set up the markup. In the following code we’ll create six divs that contain some text:

<div class="content" id="content1">
    <p>TEXT HERE</p>
</div>
<div class="content" id="content2">
    <p>TEXT HERE</p>
</div>
<div class="content" id="content3" data-stellar-background-ratio="0.5">
    <p>TEXT HERE</p>
</div>
<div class="content" id="content4" data-stellar-background-ratio="0.5">
    <p>TEXT HERE</p>
</div>
<div class="content" id="content5" data-stellar-background-ratio="0.5">
    <p>TEXT HERE</p>
</div>
<div class="content" id="content6" data-stellar-background-ratio="0.5">
    <p>TEXT HERE</p>
</div>

With this HTML in place, we need to write some CSS to define the background images used. In our demo we’ll use three images that are repeated two times each. Because we’ll use the data-stellar-background-ratio attributes on the last three divs, we’ll also add background-attachment: fixed; in the CSS.

The final CSS code is shown below:

body {
    font-size: 20px;
    color: white;
    text-shadow: 0 1px 0 black, 0 0 5px black;
}
p {
    padding: 0 0.5em;
    margin: 0;
}
.content {
    background-attachment: fixed;
    height: 400px;
}
#content1 {
    background-image: url("http://www.tamperlock.com/blog/wp-content/uploads/2014/08/london-england.jpg");
}
#content2 {
    background-image: url("http://ocdn.eu/images/pulscms/ZjU7MDQsMCwzMiwzODQsMWZhOzA2LDMyMCwxYzI_/1eb29a70dabd0994cdefaad01ca3c884.jpg");
}
#content3 {
    background-image: url("http://www.zeus.aegee.org/magazine/wp-content/uploads/napoli-golfo-vesuvio.jpg");
}
#content4 {
    background-image: url("http://www.tamperlock.com/blog/wp-content/uploads/2014/08/london-england.jpg");
}
#content5 {
    background-image: url("http://ocdn.eu/images/pulscms/ZjU7MDQsMCwzMiwzODQsMWZhOzA2LDMyMCwxYzI_/1eb29a70dabd0994cdefaad01ca3c884.jpg");
}
#content6 {
    background-image: url("http://www.zeus.aegee.org/magazine/wp-content/uploads/napoli-golfo-vesuvio.jpg");
}

Finally, we need to kick start the effect by invoking stellar(). In this demo we’ll also set a few general options:

$.stellar({
    horizontalScrolling: false,
    responsive: true
});

A live demo of the previous code is shown below and also available as a JSFiddle.

Conclusion

In this article I introduced you to Stellar.js, a jQuery plugin that allows you to create a parallax scrolling effect on the pages of a website. I haven’t covered all the options and the effects available, but this tutorial should have interested you enough to learn more about it.

What do you think about Stellar.js? Have you ever heard of or used it?

Meet the author
I'm a (full-stack) web and app developer with more than 5 years' experience programming for the web using HTML, CSS, Sass, JavaScript, and PHP. I'm an expert of JavaScript and HTML5 APIs but my interests include web security, accessibility, performance, and SEO. I'm also a regular writer for several networks, speaker, and author of the books jQuery in Action, third edition and Instant jQuery Selectors.
  • Chu Quang Tú

    The project seems no-longer maintanied. Is it stable for using in production ?

    • Aurelio De Rosa

      I stated it in the second sentence of the “What is Stellar.js?” section.

      • Chu Quang Tú

        Oops… I go through to its Github repo and didn’t notice you mentioned it. Really sorry for this :(

  • http://www.7eyetechnologies.com Kristina Roy

    I am really impressed with your post. It is very useful and informative post for all web developers. Thanks to share this important code for parallax scrolling using stellar.js. Keep up sharing such great informative blog post with us.

  • Alex Hall

    Ah parallax. The thing that isn’t.

    The examples here are not an example of parallax. Parallax requires two elements to scroll at different speeds in conjunction with each other to create the effect. Here is an example of parallax in action:

    http://codepen.io/scottkellum/pen/bHEcA

    Notice how there are a few elements scrolling at different speeds in relation to each other? That is parallax.
    Wikipedia sums it up pretty well too (http://en.wikipedia.org/wiki/Parallax):

    “Parallax is a displacement or difference in the apparent position of an object viewed along two different lines of sight”

    Having an image scroll at a slightly different speed to the page does not denote “two different lines of sight”. However, the examples are very nice and an every-increasing trend that is becoming quite boring now.

  • Barton Phillips

    Interesting article and paradigm. I agree that it could be nice in limited use on a site. I will play around with it a little. Thanks for the article.

  • http://www.7eyetechnologies.com/ 7eye Technologies

    Hi Aurelio,
    You really shared Great post about stellar.js. I think, this is very helpful tool to all designers. Keep up sharing such great informative blog post with us.

  • Oli

    Hi Aurelio
    Thanks for your article. I am trying using stellar.js, but resposive: true doesn’t work in my case:
    http://www.webgarten.ch/responsive/
    It seems that I am the only one with that problem. Do you have any idea what I am doing wrong?

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.