• Stars
    star
    4,642
  • Rank 8,654 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Stellar.js - Parallax scrolling made easy

Build Status Gitter

Stellar.js

PLEASE NOTE: This project is no longer maintained. If parallax scrolling is something you care about, please apply to become a contributor to this project.

Parallax scrolling made easy

Full guide and demonstrations available at the official Stellar.js project page.

Download

Get the development or production version, or use a package manager.

Getting Started

Stellar.js is a jQuery plugin that provides parallax scrolling effects to any scrolling element. The first step is to run .stellar() against the element:

// For example:
$(window).stellar();
// or:
$('#main').stellar();

If you're running Stellar.js on 'window', you can use the shorthand:

$.stellar();

This will look for any parallax backgrounds or elements within the specified element and reposition them when the element scrolls.

Mobile Support

Support in Mobile WebKit browsers requires a touch scrolling library, and a slightly tweaked configuration. For a full walkthrough on how to implement this correctly, read my blog post "Mobile Parallax with Stellar.js".

Please note that parallax backgrounds are not recommended in Mobile WebKit due to performance constraints. Instead, use parallax elements with static backgrounds.

Parallax Elements

If you want elements to scroll at a different speed, add the following attribute to any element with a CSS position of absolute, relative or fixed:

<div data-stellar-ratio="2">

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. If a ratio lower than 1 is causing the element to appear jittery, try setting its CSS position to fixed.

In order for Stellar.js to perform its calculations correctly, all parallax elements must have their dimensions specified in pixels for the axis/axes being used for parallax effects. For example, all parallax elements for a vertical site must have a pixel height specified. If your design prohibits the use of pixels, try using the 'responsive' option.

Parallax Backgrounds

If you want an element's background image to reposition on scroll, simply add the following attribute:

<div data-stellar-background-ratio="0.5">

As with parallax elements, the ratio is relative to the natural scroll speed. For ratios lower than 1, to avoid jittery scroll performance, set the element's CSS 'background-attachment' to fixed.

Configuring Offsets

Stellar.js' most powerful feature is the way it aligns elements.

All elements will return to their original positioning when their offset parent meets the edge of the screenβ€”plus or minus your own optional offset. This allows you to create intricate parallax patterns very easily.

Confused? See how offsets are used on the Stellar.js home page.

To modify the offsets for all elements at once, pass in the options:

$.stellar({
  horizontalOffset: 40,
  verticalOffset: 150
});

You can also modify the offsets on a per-element basis using the following data attributes:

<div data-stellar-ratio="2"
     data-stellar-horizontal-offset="40"
     data-stellar-vertical-offset="150">

Configuring Offset Parents

By default, offsets are relative to the element's offset parent. This mirrors the way an absolutely positioned element behaves when nested inside an element with a relative position.

As with regular CSS, the closest parent element with a position of relative or absolute is the offset parent.

To override this and force the offset parent to be another element higher up the DOM, use the following data attribute:

<div data-stellar-offset-parent="true">

The offset parent can also have its own offsets:

<div data-stellar-offset-parent="true"
     data-stellar-horizontal-offset="40"
     data-stellar-vertical-offset="150">

Similar to CSS, the rules take precedence from element, to offset parent, to JavaScript options.

Confused? See how offset parents are used on the Stellar.js home page.

Still confused? See what it looks like with its default offset parents. Notice how the alignment happens on a per-letter basis? That's because each letter's containing div is its default offset parent.

By specifying the h2 element as the offset parent, we can ensure that the alignment of all the stars in a heading is based on the h2 and not the div further down the DOM tree.

Configuring Scroll Positioning

You can define what it means for an element to 'scroll'. Whether it's the element's scroll position that's changing, its margins or its CSS3 'transform' position, you can define it using the 'scrollProperty' option:

$('#gallery').stellar({
  scrollProperty: 'transform'
});

This option is what allows you to run Stellar.js on iOS.

You can even define how the elements are repositioned, whether it's through standard top and left properties or using CSS3 transforms:

$('#gallery').stellar({
  positionProperty: 'transform'
});

Don't have the level of control you need? Write a plugin!

Otherwise, you're ready to get started!

Configuring Everything

Below you will find a complete list of options and matching default values:

$.stellar({
  // Set scrolling to be in either one or both directions
  horizontalScrolling: true,
  verticalScrolling: true,

  // Set the global alignment offsets
  horizontalOffset: 0,
  verticalOffset: 0,

  // Refreshes parallax content on window load and resize
  responsive: false,

  // Select which property is used to calculate scroll.
  // Choose 'scroll', 'position', 'margin' or 'transform',
  // or write your own 'scrollProperty' plugin.
  scrollProperty: 'scroll',

  // Select which property is used to position elements.
  // Choose between 'position' or 'transform',
  // or write your own 'positionProperty' plugin.
  positionProperty: 'position',

  // Enable or disable the two types of parallax
  parallaxBackgrounds: true,
  parallaxElements: true,

  // Hide parallax elements that move outside the viewport
  hideDistantElements: true,

  // Customise how elements are shown and hidden
  hideElement: function($elem) { $elem.hide(); },
  showElement: function($elem) { $elem.show(); }
});

Writing a Scroll Property Plugin

Out of the box, Stellar.js supports the following scroll properties: 'scroll', 'position', 'margin' and 'transform'.

If your method for creating a scrolling interface isn't covered by one of these, you can write your own. For example, if 'margin' didn't exist yet you could write it like so:

$.stellar.scrollProperty.margin = {
  getLeft: function($element) {
    return parseInt($element.css('margin-left'), 10) * -1;
  },
  getTop: function($element) {
    return parseInt($element.css('margin-top'), 10) * -1;
  }
}

Now, you can specify this scroll property in Stellar.js' configuration.

$.stellar({
  scrollProperty: 'margin'
});

Writing a Position Property Plugin

Stellar.js has two methods for positioning elements built in: 'position' for modifying its top and left properties, and 'transform' for using CSS3 transforms.

If you need more control over how elements are positioned, you can write your own setter functions. For example, if 'position' didn't exist yet, it could be written as a plugin like this:

$.stellar.positionProperty.position = {
  setTop: function($element, newTop, originalTop) {
    $element.css('top', newTop);
  },
  setLeft: function($element, newLeft, originalLeft) {
    $element.css('left', newLeft);
  }
}

Now, you can specify this position property in Stellar.js' configuration.

$.stellar({
  positionProperty: 'position'
});

If, for technical reasons, you need to set both properties at once, you can define a single 'setPosition' function:

$.stellar.positionProperty.foobar = {
  setPosition: function($element, newLeft, originalLeft, newTop, originalTop) {
    $element.css('transform', 'translate3d(' +
      (newLeft - originalLeft) + 'px, ' +
      (newTop - originalTop) + 'px, ' +
      '0)');
  }
}

$.stellar({
  positionProperty: 'foobar'
});

Package Managers

Stellar.js can be installed with Bower:

$ bower install jquery.stellar

Sites Using Stellar.js

I'm sure there are heaps more. Let me know if you'd like me to feature your site here.

How to Build

Stellar.js uses Node.js, Grunt and PhantomJS.

Once you've got Node and PhantomJS set up, install the dependencies:

$ npm install

To lint, test and minify the project, simply run the following command:

$ grunt

Each of the build steps are also available individually.

$ grunt test to test the code using QUnit and PhantomJS:

$ grunt lint to validate the code using JSHint.

$ grunt watch to continuously lint and test the code while developing.

Need help?

As this project is now unmaintained, your best bet is to try the Gitter chat room or Stack Overflow.

License

Copyright 2013, Mark Dalgleish
This content is released under the MIT license
http://markdalgleish.mit-license.org

More Repositories

1

static-site-generator-webpack-plugin

Minimal, unopinionated static site generator powered by webpack
JavaScript
1,608
star
2

redial

Universal data fetching and route lifecycle management for React etc.
JavaScript
1,104
star
3

react-themeable

Utility for making React components easily themeable
JavaScript
550
star
4

fathom

Fathom.js - Present JavaScript in its native environment.
JavaScript
527
star
5

redux-analytics

Analytics middleware for Redux
JavaScript
489
star
6

react-to-html-webpack-plugin

Webpack plugin that renders React components to HTML files
JavaScript
166
star
7

postcss-local-scope-example

Example usage of postcss-local-scope
JavaScript
136
star
8

presentation-build-wars-gulp-vs-grunt

CSS
77
star
9

tmpload

Asynchronous Template Loading and Caching for jQuery Templates
JavaScript
62
star
10

redux-tap

Simple side-effect middleware for Redux
JavaScript
58
star
11

remix-vanilla-extract-prototype

TypeScript
53
star
12

serviceworker-loader

ServiceWorker loader for Webpack
JavaScript
52
star
13

gulp-coveralls

Gulp plugin to send code coverage to Coveralls
JavaScript
45
star
14

web-app-manifest-loader

Web app manifest loader for webpack
JavaScript
41
star
15

isolated-scroll

Prevent scoll events from bubbling up to parent elements
HTML
40
star
16

nextjs-vanilla-extract-example

TypeScript
36
star
17

grunt-micro

Ensure your micro-framework stays micro
JavaScript
35
star
18

react-isolated-scroll

React component for isolated-scroll
JavaScript
34
star
19

figma-leading-trim

Figma plugin for trimming space above and below text elements
TypeScript
28
star
20

react-static-site-playground

WIP
JavaScript
26
star
21

jquery-html5data

$.html5data
JavaScript
22
star
22

presentation-a-state-of-change-object-observe

JavaScript
22
star
23

css-modules-in-js-demo

Working demo of using JavaScript as a CSS Modules preprocessor
JavaScript
21
star
24

astro-vanilla-extract-demo

Basic demo of Astro and vanilla-extract
Astro
18
star
25

Eventralize

jQuery Events for Object Oriented JavaScript
JavaScript
18
star
26

presentation-the-case-for-css-modules

HTML
15
star
27

node-lanyrd-scraper

Lanyrd event scraper for Node.js
JavaScript
14
star
28

allthethings.js

Let your array iterations read like actual sentences
JavaScript
11
star
29

fifteen-kilos-webpack-plugin

Bring the magic of fifteen-kilos to your entire project
HTML
11
star
30

instrument-methods

Simple object method instrumentation
JavaScript
11
star
31

react-fifteen-kilos

Bring the magic of fifteen-kilos to your entire React application
JavaScript
10
star
32

react-progressive-component-starter

WIP
JavaScript
9
star
33

presentation-a-unified-styling-language

HTML
8
star
34

reconomise

Crowd sourced local business support network and priority ranking system. 'Best Use of SAPI' at RHoK Melbourne June '12.
JavaScript
8
star
35

react-themeable-experiment

WIP
JavaScript
7
star
36

bespoke-remote-prototype

Prototype presentation for bespoke-remote
JavaScript
7
star
37

presentation-the-end-of-global-css

HTML
6
star
38

angular-delegator

Write smaller, cleaner AngularJS services
JavaScript
6
star
39

presentation-bespoke.js

DIY Presentations with Bespoke.js
JavaScript
5
star
40

batsignal

Help radiator for remote agile teams. Built using Meteor.
JavaScript
5
star
41

presentation-return-of-the-progressive-web

The Return of the Progressive Web
HTML
5
star
42

redux-hotjar

Declarative Hotjar tagging for Redux
JavaScript
4
star
43

stellar.js-site

Stellar.js Project Page
JavaScript
4
star
44

webpack-serviceworker-demo

Webpack Service Worker demo
JavaScript
4
star
45

presentation-first-class-styles

HTML
4
star
46

indiana

Experimental isomorphic JavaScript demo
JavaScript
3
star
47

bespoke-webpack-boilerplate

Bespoke.js boilerplate powered by Webpack
JavaScript
3
star
48

markdalgleish.com

My personal blog - built with Octopress
JavaScript
3
star
49

remix-vite-css-url-demo

Demo of Remix + Vite + .css?url
JavaScript
3
star
50

stateless-dtm

Purely stateless interface for Dynamic Tag Manager
JavaScript
3
star
51

twitface

Twitter avatar API client for Node.js
JavaScript
2
star
52

chook-jstestdriver

JsTestDriver adapter for Chook, the headless, framework-agnostic unit test runner for Node.js
JavaScript
2
star
53

chook

Headless, framework-agnostic unit test runner for Node.js
JavaScript
2
star
54

css-hooks-playground

Just playing around with CSS Hooks
TypeScript
2
star
55

remix-vite-cloudflare-workers-playground

Demo app using Remix with Vite on Cloudflare Workers
TypeScript
1
star
56

postcss-local-scope-inheritance-example

Example usage of postcss-local-scope with class inheritance
JavaScript
1
star
57

redux-helloworld

Just playing around with Redux
JavaScript
1
star
58

zenhub

Placeholder repo for my Zenhub mega-board
1
star
59

css-modules-selector-experiment

Exploration of non-class selectors in CSS Modules
JavaScript
1
star
60

cuecard

iPad-controlled presentation framework for Node.js
JavaScript
1
star
61

tmpload-site

tmpload Project Page
JavaScript
1
star
62

Eventralize-site

Eventralize Project Page
JavaScript
1
star
63

presentation-dawn-of-the-progressive-single-page-app

HTML
1
star
64

fathom-site

Fathom.js Project Page
JavaScript
1
star
65

cuecard-example

Example usage of Cuecard
JavaScript
1
star
66

kansas

Experimental stereoscopic rendering for React components
JavaScript
1
star
67

ducks-component-experiment

Experimental React component powered by Ducks
JavaScript
1
star
68

bespoke.js-site

Bespoke.js Project Page
CSS
1
star
69

preconstruct-node-esm-import-issue

JavaScript
1
star
70

presentation-a-bespoke-ecosystem

JavaScript
1
star
71

presentation-tabs

Ryan loses
CSS
1
star
72

baltic

Experimental continuous deployment with Codeship for Bespoke.js
JavaScript
1
star
73

presentation-web-components

Web Components: Why You're Already An Expert
JavaScript
1
star
74

bespoke-theme-prototype

CSS
1
star
75

angular-lisa-example

Sample app wiring AngularJS and Lisa together
JavaScript
1
star
76

balance-parens

Balance a string of parentheses.
JavaScript
1
star
77

chook-zombie

Experimental Zombie support for Chook
JavaScript
1
star
78

jquery-html5data-site

$.html5data Project Page
JavaScript
1
star
79

walletconnnect-deeplink-prompt-issue

TypeScript
1
star
80

melbcss-modules-hello-world

A simple CSS Modules example, extracted from a live coding demo at MelbCSS
JavaScript
1
star