Skip to content

Commit 9d940e2

Browse files
committed
initial commit
0 parents  commit 9d940e2

File tree

4 files changed

+782
-0
lines changed

4 files changed

+782
-0
lines changed

MIT-LICENSE.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2012 Mark Dalgleish
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
[Stellar.js](http://markdalgleish.com/projects/stellar.js/)
2+
===========================================================
3+
4+
Full guide and demonstrations available at the [official Stellar.js project page](http://markdalgleish.com/projects/stellar.js/).
5+
6+
Getting Started
7+
---------------
8+
9+
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:
10+
11+
``` js
12+
// For example:
13+
$(window).stellar();
14+
// or:
15+
$('#main').stellar();
16+
```
17+
18+
If you're running Stellar.js on 'window', you can use the shorthand:
19+
20+
``` js
21+
$.stellar();
22+
```
23+
24+
This will look for any parallax backgrounds or elements within the specified element and reposition them when the element scrolls.
25+
26+
Parallax Elements
27+
-----------------
28+
29+
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:
30+
31+
``` html
32+
<div data-stellar-ratio="2">
33+
```
34+
35+
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.
36+
37+
If a ratio lower than 1 is causing the element to appear jittery, try setting its CSS position to fixed.
38+
39+
Parallax Backgrounds
40+
--------------------
41+
42+
If you want an element's background image to reposition on scroll, simply add the following attribute:
43+
44+
``` html
45+
<div data-stellar-background-ratio="0.5">
46+
```
47+
48+
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.
49+
50+
Configuring Offsets
51+
-------------------
52+
53+
Stellar.js' most powerful feature is the way it aligns elements.
54+
55+
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.
56+
57+
Confused? [See how offsets are used on the Stellar.js home page.](http://markdalgleish.com/projects/stellar.js/#show-offsets)
58+
59+
To modify the offsets for all elements at once, pass in the options:
60+
61+
``` js
62+
$.stellar({
63+
horizontalOffset: 40,
64+
verticalOffset: 150
65+
});
66+
```
67+
68+
You can also modify the offsets on a per-element basis using the following data attributes:
69+
70+
``` html
71+
<div data-stellar-ratio="2"
72+
data-stellar-horizontal-offset="40"
73+
data-stellar-vertical-offset="150">
74+
```
75+
76+
Configuring Offset Parents
77+
--------------------------
78+
79+
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.
80+
81+
As with regular CSS, the closest parent element with a position of relative or absolute is the offset parent.
82+
83+
To override this and force the offset parent to be another element higher up the DOM, use the following data attribute:
84+
85+
``` html
86+
<div data-stellar-offset-parent="true">
87+
```
88+
89+
The offset parent can also have its own offsets:
90+
91+
``` html
92+
<div data-stellar-offset-parent="true"
93+
data-stellar-horizontal-offset="40"
94+
data-stellar-vertical-offset="150">
95+
```
96+
97+
Similar to CSS, the rules take precedence from element, to offset parent, to JavaScript options.
98+
99+
Confused? [See how offset parents are used on the Stellar.js home page.](http://markdalgleish.com/projects/stellar.js/#show-offset-parents)
100+
101+
Still confused? [See what it looks like with its default offset parents.](http://markdalgleish.com/projects/stellar.js/#show-offset-parents-default) Notice how the alignment happens on a per-letter basis? That's because each letter's containing div is its default offset parent.
102+
103+
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.
104+
105+
Configuring Scroll Positioning
106+
------------------------------
107+
108+
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:
109+
110+
``` js
111+
$('#gallery').stellar({
112+
scrollProperty: 'transform'
113+
});
114+
```
115+
116+
This option is what allows you to run [Stellar.js on iOS](http://markdalgleish.com/projects/stellar.js/demos/ios.html).
117+
118+
You can even define how the elements are repositioned, whether it's through standard top and left properties or using CSS3 transforms:
119+
120+
``` js
121+
$('#gallery').stellar({
122+
positionProperty: 'transform'
123+
});
124+
```
125+
126+
Don't have the level of control you need? Write a plugin!
127+
128+
Writing a Scroll Property Plugin
129+
--------------------------------
130+
131+
Out of the box, Stellar.js supports the following scroll properties:
132+
'scroll', 'position', 'margin' and 'transform'.
133+
134+
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:
135+
136+
``` js
137+
$.stellar.scrollProperty.margin = {
138+
getTop: function($element) {
139+
return parseInt($element.css('margin-top'), 10) * -1;
140+
},
141+
setTop: function($element, val) {
142+
$element.css('margin-top', val);
143+
},
144+
145+
getLeft: function($element) {
146+
return parseInt($element.css('margin-left'), 10) * -1;
147+
},
148+
setLeft: function($element, val) {
149+
$element.css('margin-left', val);
150+
}
151+
}
152+
```
153+
154+
Now, you can specify this scroll property in Stellar.js' configuration.
155+
156+
``` js
157+
$.stellar({
158+
scrollProperty: 'margin'
159+
});
160+
```
161+
162+
Writing a Position Property Plugin
163+
----------------------------------
164+
165+
Stellar.js has two methods for positioning elements built in: 'position' for modifying its top and left properties, and 'transform' for using CSS3 transforms.
166+
167+
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:
168+
169+
``` js
170+
$.stellar.positionProperty.position = {
171+
setTop: function($element, newTop, originalTop) {
172+
$element.css('top', newTop);
173+
},
174+
setLeft: function($element, newLeft, originalLeft) {
175+
$element.css('left', newLeft);
176+
},
177+
}
178+
```
179+
180+
Now, you can specify this position property in Stellar.js' configuration.
181+
182+
``` js
183+
$.stellar({
184+
positionProperty: 'position'
185+
});
186+
```
187+
188+
Configuring Everything
189+
----------------------
190+
191+
Below you will find a complete list of options and matching default values:
192+
193+
``` js
194+
$.stellar({
195+
// Set scrolling to be in either one or both directions
196+
horizontalScrolling: true,
197+
verticalScrolling: true,
198+
199+
// Set the global alignment offsets
200+
horizontalOffset: 0,
201+
verticalOffset: 0,
202+
203+
// Select which property is used to calculate scroll.
204+
// Choose 'scroll', 'position', 'margin' or 'transform',
205+
// or write your own 'scrollProperty' plugin.
206+
scrollProperty: 'scroll',
207+
208+
// Select which property is used to position elements.
209+
// Choose between 'position' or 'transform',
210+
// or write your own 'positionProperty' plugin.
211+
positionProperty: 'position',
212+
213+
// Enable or disable the two types of parallax
214+
parallaxBackgrounds: true,
215+
parallaxElements: true,
216+
217+
// Hide parallax elements that move outside the viewport
218+
hideDistantElements: true,
219+
220+
// Set how often the viewport size is detected
221+
viewportDetectionInterval: 1000,
222+
223+
// Customise how elements are shown and hidden
224+
hideElement: function($elem) { $elem.hide(); },
225+
showElement: function($elem) { $elem.show(); }
226+
});
227+
```
228+
229+
Demos
230+
-----
231+
232+
### Parallax Elements
233+
234+
The best demonstration of parallax elements is on the [Stellar.js home page](http://markdalgleish.com/projects/stellar.js/). Other demos are listed below:
235+
236+
### [Parallax Backgrounds](http://markdalgleish.com/projects/stellar.js/demos/backgrounds.html)
237+
238+
While the home page shows off parallax elements, Stellar.js also supports parallax backgrounds.
239+
240+
### [iOS (Using Scrollability)](http://markdalgleish.com/projects/stellar.js/demos/ios.html)
241+
242+
Mobile Safari halts code execution during scroll so you can't use normal scroll events to animate. By using an iOS scrolling library we're able to bypass this limitation. In order to use Scrollability, Stellar.js is run against the scrolling element and the 'scrollProperty' option is set to 'transform'.
243+
244+
Using Stellar.js on your site?
245+
------------------------------
246+
247+
[I'd love to hear about it!](http://twitter.com/markdalgleish) Let me know if you'd like me to feature your site here.
248+
249+
How to Build
250+
------------
251+
252+
The code is minified using UglifyJS using the following command:
253+
254+
`uglifyjs -o stellar.min.js stellar.js`
255+
256+
Contributing to Stellar.js
257+
--------------------------
258+
259+
If you want to contribute in a way that changes the API, please file an issue before submitting a pull request so we can dicuss how to appropriately integrate your ideas.
260+
261+
Questions?
262+
----------
263+
264+
Contact me on GitHub or Twitter: [@markdalgleish](http://twitter.com/markdalgleish)

0 commit comments

Comments
 (0)