Skip to content

Commit 2067f84

Browse files
committed
relying on jQuery 1.9.0+ easing functions, added ease option
1 parent 1a91906 commit 2067f84

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

README.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ $("#selector").parallax({
3838
});
3939
```
4040

41-
#### Supported properties:
41+
### Supported properties:
42+
43+
Property value can be specified as number or percentage (string).
44+
To specify a **from** value as well **to**, use object syntax:
45+
```javascript
46+
{"to":1,"from":0}
47+
```
4248

4349
translateX
4450

@@ -52,14 +58,26 @@ rotate
5258

5359
opacity
5460

55-
#### Available options:
61+
### Available options:
5662

5763
Options can be specified for all properties, or each individually
5864

59-
**start** - number | selector
65+
#### start
66+
**Type:** number or selector
67+
**Default:** the elements top offset
68+
69+
#### duration
70+
**Type:** number or percentage (string) or callback function
71+
**Default:** element top + height - start
6072

61-
**duration** - number | percentage string | callback function
73+
#### trigger
74+
**Type:** number or percentage (string)
75+
**Default:**: "100%"
6276

63-
**trigger** - number | percentage string
77+
#### ease
78+
**Type:** function or string
79+
**Default:** "linear"
6480

65-
**axis** - "x" | "y"
81+
#### axis
82+
**Type:** string ("x" or "y")
83+
**Default:** "y"

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"description": "jQuery plugin for easy parallax effect",
99
"main": "jquery.parallax.js",
1010
"dependencies": {
11-
"jquery": "*",
11+
"jquery": ">=1.9.0",
1212
"requestAnimationFrame.js": "latest"
1313
},
1414
"keywords": [

jquery.parallax.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,20 @@
221221
return value;
222222
}
223223

224-
function easeInOutQuad(t, b, c, d) {
225-
return c*t/d + b;
226-
//return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
227-
}
228-
229224
function Scene($el, options, maxValue) {
230225
this.$el = $el;
231226
this.axis = options.axis;
232227
this.from = covertOption(options.from, maxValue);
233228
this.to = covertOption(options.to, maxValue);
234229
this.trigger = convertToPx(options.trigger, options.axis);
235230
this.start = convertToObj(options.start) || options.start;
231+
if (typeof options.ease == "function") {
232+
this.ease = options.ease;
233+
}
234+
else {
235+
typeof options.ease == "undefined" || (this.ease = $.easing[options.ease]);
236+
typeof this.ease == "function" || (this.ease = $.easing.linear);
237+
}
236238
this.started = false;
237239

238240
if (typeof options.duration != "undefined") {
@@ -248,6 +250,9 @@
248250
};
249251
}
250252
}
253+
Scene.STATE_BEFORE = 'before';
254+
Scene.STATE_DURING = 'during';
255+
Scene.STATE_AFTER = 'after';
251256
Scene.prototype = {
252257
updateDuration: function() {
253258
this.startPx = Math.max(getOffset(this.start, this.axis) - this.trigger, 0);
@@ -260,26 +265,29 @@
260265
if (!this.started) {
261266
this.updateDuration();
262267
}
263-
var needsUpdate = (this.state == 'during');
268+
var needsUpdate = (this.state == Scene.STATE_DURING);
264269
if (scrollTop < this.startPx) {
265-
this.state = 'before';
270+
this.state = Scene.STATE_BEFORE;
266271
}
267272
else if (scrollTop <= (this.startPx + this.durationPx)) {
268-
this.state = 'during';
273+
this.state = Scene.STATE_DURING;
269274
this.started = true;
270275
needsUpdate = true;
271276
}
272277
else {
273-
this.state = 'after';
278+
this.state = Scene.STATE_AFTER;
274279
}
275280
return needsUpdate;
276281
},
277282
value: function() {
278-
if (this.state == 'before') {
283+
if (this.state == Scene.STATE_BEFORE) {
279284
return this.from;
280285
}
281-
else if (this.state == 'during') {
282-
return easeInOutQuad(scrollTop-this.startPx, this.from, (this.to - this.from), this.durationPx);
286+
else if (this.state == Scene.STATE_DURING) {
287+
var posPx = scrollTop - this.startPx,
288+
percent = posPx / this.durationPx,
289+
aniPos = this.ease.call(this, percent);
290+
return (this.to - this.from) * aniPos + this.from;
283291
}
284292
else {
285293
return this.to;

0 commit comments

Comments
 (0)