Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ca73cee

Browse filesBrowse files
committed
increase compatibility, update readme
1 parent 5c9219e commit ca73cee
Copy full SHA for ca73cee

File tree

Expand file treeCollapse file tree

2 files changed

+86
-49
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+86
-49
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+79-46Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ Follow these steps to get started:
1212

1313
1. [Overview](#overview)
1414
2. [Install](#install)
15-
3. [Import](#import)
16-
4. [Call](#call)
17-
5. [Review Options](#options)
15+
3. [Call](#call)
16+
4. [Review Options](#options)
17+
5. [Review API](#api)
1818

1919
### Overview
2020

21-
Rallax.js creates a parallax effect given a target HTML element and an image location. It will create an `<img>` element, and position it `absolute` behind your target element.
21+
**The problem**: You want to create a dynamic parallax scrolling effect on your web page, but you don't want to use jQuery OR dramatically impact your website's performance.
2222

23-
As the user scrolls, the position of the `<img>` element will change relative to the target's location, creating the desired effect.
23+
**The solution**: Rallax.js makes it easy to create a parallax effect on a target HTML element, with great performance and no dependencies.
2424

2525
### Install
2626

@@ -30,28 +30,29 @@ Using NPM, install Rallax, and save it to your `package.json` dependencies.
3030
$ npm install rallax.js --save
3131
```
3232

33-
### Import
34-
35-
Import Rallax, naming it according to your preference.
33+
Then, import Rallax, naming it according to your preference.
3634

3735
```es6
3836
import rallax from 'rallax.js'
3937
```
4038

4139
### Call
4240

43-
To generate the desired effect, call Rallax, passing in your target-selector, image location, and your desired options.
41+
To generate the desired effect, call Rallax, passing in your element/target-selector and your desired options.
4442

4543
```es6
4644
// target: <div class="parallax"></div>
47-
// image location: "./test-image.jpg"
4845

4946
const options = { speed: 0.4 }
47+
const parallax = rallax(".parallax", options)
48+
49+
// or:
5050

51-
rallax(".parallax", "./test-image.jpg", options)
51+
const target = document.querySelector('.parallax')
52+
const parallax = rallax(target, { speed: 0.4 })
5253
```
5354

54-
**Note**: Rallax.js does not make any assumptions regarding the styling of the target element. **In order to see the effect, you must set either a transparent or slighty-transparent background on the target.**
55+
**Note**: Rallax.js does not make any assumptions regarding the styling of the target element.
5556

5657
## Options
5758

@@ -60,71 +61,103 @@ You are not required to pass any options. All options come with sensible defaul
6061
```es6
6162
{
6263
speed: 0.3,
63-
offset: {},
64-
zIndex: -1000
64+
mobilePx: false
6565
}
6666
```
6767

6868
Explanation of each option follows:
6969

7070
* [speed](#speed)
71-
* [offset](#offset)
72-
* [zIndex](#zIndex)
73-
* [minPixels](#minPixels)
74-
* [maxPixels](#maxPixels)
71+
* [mobilePx](#mobilePx)
7572

7673
### speed
7774

7875
Accepts an `integer` between `0` and `1`.
7976

80-
At a speed of `0`, the image will scroll with the target.
81-
At a speed of `1`, the image will appear fixed in place.
77+
At a speed of `0`, the target will scroll like a static element.
78+
At a speed of `1`, the target will appear fixed (will move at the speed of scroll).
8279

83-
### offset
80+
### mobilePx
8481

85-
Accepts an `object`.
82+
Accepts an `integer`, as number of pixels.
8683

87-
The `offset` option works similar to the [background-position](https://developer.mozilla.org/en-US/docs/Web/CSS/background-position) CSS property. `offset` will specify the **center** of the image relative to the target element.
84+
Pass this option if you want parallax for this target to automatically be disabled at a certain screen width.
8885

89-
`offset` is declared with two of the following keys: `top`, `bot`, `left`, `right`, followed by a an `integer` representing the number of pixels.
86+
## API
9087

91-
```es6
92-
// position the center of the image 10px from the top of the
93-
// target, and 40 px from the left.
88+
Calling Rallax will return an object with a set of methods defined. Those methods are:
89+
90+
* [stop](#stop)
91+
* [start](#start)
92+
* [changeSpeed](#changeSpeed)
93+
* [when](#when)
9494

95-
rallax(".parallax", "./test-image.jpg", {
96-
offset: {
97-
top: 10,
98-
left: 40
99-
}
100-
})
95+
### stop
10196

102-
// by default, the image will be centered relative to the target
97+
Calling `stop()` will pause the parallax effect on the target until the next time you call `start()`.
10398

104-
rallax(".parallax", "./test-image.jpg")
99+
```es6
100+
const parallax = rallax('.parallax')
101+
102+
if (condition) {
103+
parallax.stop()
104+
}
105105
```
106106

107-
### zIndex
107+
### start
108+
109+
Calling `start()` will re-enable the parallax effect if you had previously disabled it. **Note:** calling `start()` will not re-enable the effect if you have disabled it with the [mobilePx](#mobilePx) option.
108110

109-
Accepts an `integer`.
111+
```es6
112+
const parallax = rallax('.parallax')
113+
parallax.stop()
114+
115+
// some time later
116+
117+
parallax.start()
118+
```
110119

111-
`zIndex` specifies the [z-index](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index) property of the parallax image.
120+
### changeSpeed (speed)
112121

113-
### maxPixels
122+
Accepts a `float` between `0` and `1`.
114123

115-
Accepts an `integer`, specifying the maximum pixel width of the screen before 'turning off' the parallax effect.
124+
Calling `changeSpeed` will change the speed of the target parallax effect.
116125

117-
Set `maxPixels` if you would like to turn off the parallax effect for very large screens.
126+
```es6
127+
// initialize the target at a speed of 0.6
128+
const parallax = rallax('.parallax', {speed: 0.6})
118129

119-
Once turned off, the parallax image will appear as a background image for the target.
130+
// change speed to 1, making the target appear fixed
131+
parallax.changeSpeed(1)
132+
```
120133

121-
### minPixels
134+
### when (condition, action)
122135

123-
Accepts an `integer`, specifying the minimum pixel width of the screen before 'turning off' the parallax effect.
136+
Accepts a condition `function` and an action `function`.
124137

125-
Set `minPixels` if you would like to turn off the parallax effect for very small screens (such as mobile devices).
138+
Calling `when` will queue a condition and action onto the target, which will be evaluated before the target is scrolled. The `when` method is useful for setting up all kinds of special effects.
126139

127-
Once turned off, the parallax image will appear as a background image for the target.
140+
```es6
141+
const parallax = rallax('.parallax')
142+
143+
// after reaching a certain position in the document,
144+
// increase the target's speed
145+
parallax.when(
146+
() => window.scrollY > 400,
147+
() => parallax.changeSpeed(1)
148+
)
149+
150+
// stop the parallax after a certain period of time
151+
const startTime = new Date().getTime()
152+
153+
parallax.when(
154+
() => {
155+
const newTime = new Date().getTime()
156+
return newTime - startTime > 4000
157+
},
158+
() => parallax.stop()
159+
)
160+
```
128161

129162
## Browser Support
130163

‎src/rallax.js

Copy file name to clipboardExpand all lines: src/rallax.js
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RallaxObj {
4040
changeSpeed(newSpeed) {
4141
if (this.inWindow()) {
4242
this.accumulated = this.getTranslation()
43-
this.startScroll = window.scrollY
43+
this.startScroll = this.scrollY()
4444
}
4545
this.speed = newSpeed
4646
}
@@ -50,9 +50,13 @@ class RallaxObj {
5050
return this
5151
}
5252

53-
// HELPERS
53+
// HELPERS
54+
scrollY() {
55+
return window.scrollY || window.pageYOffset
56+
}
57+
5458
getTranslation() {
55-
const dist = window.scrollY - this.startScroll
59+
const dist = this.scrollY() - this.startScroll
5660
const translation = (dist * this.speed) + this.accumulated
5761
return translation >= 0 ? translation : 0
5862
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.