# Create a Parallax Scrolling Website Using Stellar.js

One of the biggest trends in recent modern web design is use of parallax scrolling effects. In this tutorial I'm going to show you how you can create the effect on your own website, with a bit of imagination and a little help from Stellar.js.

## A Modern Parallax jQuery Plugin

Interested in saving some time and making sure you're using modern best practices? There's a great responsive jQuery slider plugin on Envato Market that could do the job perfectly! It's already used on over 150,000 websites.

## Introduction

The parallax scrolling effect has been popular ever since sites such as Nike's Better World introduced it on their websites a few years ago. The parallax effect with regard to interfaces has been around since the 1980's when it was first used in video game titles and subsequently in games themselves. More recently it started to make an appearance in web interfaces - you'll be familiar with silverbackapp which used the effect as part of the header.

When combined with the scrolling functionality of a website, parallax scrolling effects can have a strong visual impact, especially when combined with some form of story which takes you on a journey.

## Para...What?

Parallax is a displacement or difference in the apparent position of an object viewed along two different lines of sight. - Wikipedia

So what exactly is the parallax effect? Well it's probably one of the things that your clients refer to when they blindly say "I want my site HTML5". Whenever clients ask me for an "HTML5" site, I have to ask them specifically how they interpret the meaning HTML5 - at the moment it just seems to be the buzzword that clients keep saying to me without really understanding what it means.

So is it HTML5? Sure HTML5 does have a part to play in the parallax scrolling effect but it's more than HTML5, it also utilizes some form of javascript, such as jQuery, and wouldn't be possible without a little bit of CSS3.

The actual word parallax is derived from the Greek παραλλαξη (parallaxis) which means alteration. Objects which are closer to the eye have a larger parallax than objects which are in the distance. This means that objects which are closer to us can appear to move faster than objects in the background.

Layering up multiple backgrounds and objects (such as images) and then allowing them to move at different speeds creates a sensation of depth and dimension.

## Parallax in Action

Take a look at some examples which demonstrate parallax effects.

## Every Website Tells a Story

The examples above all take you on some form of journey or story, and they do so in a variety of different ways. This, in my opinion, is what makes a parallax scrolling site a success. The key to making it interesting and unique is by focussing on a good story and concept, then using the effect in creative and imaginative ways.

Wieden+Kennedy (W&K), the guys behind the Nike Better World Website support this:

In our opinion, technologies are independent of concept. Our primary focus was on creating a great interactive storytelling experience. - Wieden+Kennedy (W&K)

## How Our Site Will Work

To demonstrate one way of implementing parallax scrolling into your website I've opted to show you a simple four slide website that uses the effect on different backgrounds and images. I've also added a navigation in the left hand corner which will scroll to a specific slide on the site and will also change size to reflect the active slide number. I'm also using the web font Bebas, though you're free to use something else if you wish.

Here's how our folder structure will appear:

## The Plugins Used

### Stellar.js

To help me achieve this I'm using the Stellar.js, a jquery plugin by Mark Dalgleish which makes it easy to create parallax scrolling sites. There are other plugins available to help you do this which I've listed at the bottom of this article. I've opted to use Stellar.js as it's fairly straightforward to implement and, although not demonstrated during this tutorial, it can be optimized to work on smart device platforms such as iOS.

### Waypoints.js

I'm also going to use jQuery waypoints by Caleb Troughton. Waypoints is another jQuery plugin which executes a function whenever you scroll to an element. This allows the navigation on the site to highlight which slide we are on according to the position of the scrollbar.

### jQuery Easing

jQuery easing is a plugin from GSGD which offers advanced easing options. We will be using this to add a nice easing movement when transitioning from slide to slide.

## The HTML Markup

Kicking off our index.html we add the HTML5 doctype and then create the head section. This consists of a CSS Reset followed by our style sheet 'styles.css'. We then add the jQuery library followed by our custom jQuery file 'js.js'. This is then followed by the three plugins, 'jquery.stellar.min.js','waypoints.min.js' and 'jquery.easing.1.3.js'.

The next element in our html is the image of the Envato logo which remains in a constant fixed position throughout the whole site. To this we add a class of 'envatologo' so that we can set the positioning of it later when we code up the CSS.

### The Slides

The four slides use the same markup:

We use a class of 'slide' that will be used as a general style for all the slides. Each slide is then given an id of 'slide' followed by the slide number ie 'Slide1'. We use the HTML5 data attribute and name it 'data-slide'. This will allow us to target it using jQuery. Another data attribute of 'data-stellar-background-ratio' is added. This is specific to the stellar.js jQuery plugin and tells the plugin at what ratio the speed of the element should scroll.

The ratio is relative to the natural scroll speed, so a ratio of 0.5 would cause the element to scroll at half-speed, a ratio of 1 would have no effect, and a ratio of 2 would cause the element to scroll at twice the speed.

All slides except for slide four have a button that will allow us to scroll to the next slide. To this we add the 'data-slide' attribute with the value of the next slide number. This is so the button knows what slide is next so that we can pass this value to jQuery. The majority of the slides also have a span with a class of 'slideno'; simply a large text version of the slide number which appears in the bottom left hand corner of most slides. This could also be used for titles.

On slides three and four we also add some image elements to the 'slide' div. These images will give a real insight into the parallax effect that we are creating. We wrap these inside a 'wrapper' div that will be centralized and is '960px' wide, this is just to ensure that it displays OK across all desktop monitor sizes.

Each image has a 'data-stellar-ratio' attribute attached to it. This again is stellar.js specific and tells the plugin at what ratio of speed we should scroll the element at.

## The CSS

Luckily for us there's not too much involved with the CSS. It's basically styling a few simple elements, but the majority of it is for positioning some of the image elements.

The first thing we need to do with our CSS is bring in the BEBAS font using @font-face. We then add that to the html to set the font for the site. We also set the width and height of the html and body to 100%. This will allow our slides to adopt the full width and height of the user's screen.

The main navigation is given a 'fixed' position to keep it in the same place throughout the site. We offset this 20px from the top to give a little bit of space above it and set the z-index to 1 to ensure that this is on the top layer of the site.

The actual list is just the styling of the text with a border across the bottom to act as an underline. This has a width of 53px. A transition is also added so that it animates from its standard state to its hover state. I've used the -webkit- prefix here just to keep the code short, but the full source code which can be downloaded above contains all vendor prefixes.

The hover state also uses the same properties as the class 'active'; basically just an increase in font size and width. The 'active' class is used by jQuery to style the relevant slide which is in view of the browser window.

The envato logo is given some positioning styles just to ensure that it remains in the centre of the screen. Similarly to the navigation this is also given a z-index of '1' to ensure it stays on top.

Now we move onto styling of the actual slides. We give them a background-attachment 'fixed' property. The background-attachment property sets whether a background image is fixed or scrolls with the rest of the page, so comes in useful for background-images (such as used on slide four). For this example we've used a wallpaper by Philipp Seiffert which can be downloaded here. We set the position of the slide to 'relative'. This is to that we can absolutely position the classes 'slideno' and 'button' against the slide as opposed the actual document.

The box shadow is purely for decoration purposes and adds a nice drop shadow to the top inset of each slide.

The .button is for the button at the bottom of the page which allows us to progress to the next slide. We've positioned it at the bottom centre of each slide and have used an image of an arrow as the indicator.

The styling for each individual slide is relatively simple and follows the same pattern each time. Slide one has a background color of '#5c9900'. Slide two also has a background color set on it. Slide two also contains images and we can target each one by using the CSS Selector nth-child(n). This selector can be described as

This pseudo-class matches elements on the basis of their positions within a parent element's list of child elements.

So we're basically styling each image in the order they appear in our markup. We're simply positioning them relative to the wrapper of the slide.

Slide three follows the same suit as slide two.

Slide four is slightly different than the previous two slides as it doesn't contain any image elements or a background-color, but instead uses a background-image. We've also given it the CSS3 property 'background-size:cover'. This basically sets the background-image to cover the entire browser window and will resize as the browser window resizes. We have also added a line of text on the last slide which we have styled and given a class of 'parallaxbg'

## The jQuery

The jQuery really is where this thing starts to come to life. I've commented the code so that you can see exactly what is happening.

## Just a Few Points

If you look at slide two of our example, you will notice the 3D bubbles. I have added a slight gaussian blur to some of these, mainly to those at the very foreground and background. Combining these with sharply focused bubbles adds to the sense of depth that parallax creates. This is something that you should maybe consider when trying to get good depth to your site.

A lot of sites that utilize this effect do tend to be quite image heavy, make sure you compress your images as much as you can (without sacrificing quality). If after compression it's still taking quite a while to load then consider adding a loader to your site.

## Conclusion

Parallax scrolling is one of the most loved effects of recent times and people are constantly pushing the boundaries with it. Here today I've shown you how to setup a basic website that utilizes parallax scrolling. The demo that I've shown today is a relatively simple site for learning purposes. For those of you who are about to create a site using this effect then I urge you to spend time on the concept and story as this is what makes them unique, shareable and indeed a pleasure to browse. It's about how you can utilize this effect wisely and not just using it for the sake of using it.

I hope you've enjoyed reading about parallax scrolling and I'd love to see how you guys have utilized it in your own sites, feel free to leave links to them below. Until next time!