Skip to content

Commit 93b2aa0

Browse files
authored
Merge pull request #185 from DevNIX/master
Improved animation technique, fixes an issue which causes to scroll the parallax divs to a different speed
2 parents 2066bcf + 41f3f31 commit 93b2aa0

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

parallax.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
var vendors = ['ms', 'moz', 'webkit', 'o'];
1515
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
1616
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
17-
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
18-
|| window[vendors[x]+'CancelRequestAnimationFrame'];
17+
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
1918
}
2019

2120
if (!window.requestAnimationFrame)
@@ -65,8 +64,8 @@
6564
positions = [positions[1], positions[0]];
6665
}
6766

68-
if (this.positionX != undefined) positions[0] = this.positionX.toLowerCase();
69-
if (this.positionY != undefined) positions[1] = this.positionY.toLowerCase();
67+
if (this.positionX !== undefined) positions[0] = this.positionX.toLowerCase();
68+
if (this.positionY !== undefined) positions[1] = this.positionY.toLowerCase();
7069

7170
self.positionX = positions[0];
7271
self.positionY = positions[1];
@@ -154,7 +153,7 @@
154153
this.$slider.trigger('load');
155154
}
156155

157-
};
156+
}
158157

159158

160159
// Parallax Instance Methods
@@ -182,13 +181,14 @@
182181
var minOffset = Math.max(this.boxOffsetTop + this.boxHeight - winHeight, 0);
183182
var imageHeightMin = this.boxHeight + (maxOffset - minOffset) * (1 - this.speed) | 0;
184183
var imageOffsetMin = (this.boxOffsetTop - maxOffset) * (1 - this.speed) | 0;
184+
var margin;
185185

186186
if (imageHeightMin * this.aspectRatio >= this.boxWidth) {
187187
this.imageWidth = imageHeightMin * this.aspectRatio | 0;
188188
this.imageHeight = imageHeightMin;
189189
this.offsetBaseTop = imageOffsetMin;
190190

191-
var margin = this.imageWidth - this.boxWidth;
191+
margin = this.imageWidth - this.boxWidth;
192192

193193
if (this.positionX == 'left') {
194194
this.offsetLeft = 0;
@@ -204,7 +204,7 @@
204204
this.imageHeight = this.boxWidth / this.aspectRatio | 0;
205205
this.offsetLeft = 0;
206206

207-
var margin = this.imageHeight - imageHeightMin;
207+
margin = this.imageHeight - imageHeightMin;
208208

209209
if (this.positionY == 'top') {
210210
this.offsetBaseTop = imageOffsetMin;
@@ -234,19 +234,15 @@
234234
}
235235

236236
this.$mirror.css({
237-
transform: 'translate3d(0px, 0px, 0px)',
237+
transform: 'translate3d('+this.mirrorLeft+'px, '+(this.mirrorTop - overScroll)+'px, 0px)',
238238
visibility: this.visibility,
239-
top: this.mirrorTop - overScroll,
240-
left: this.mirrorLeft,
241239
height: this.boxHeight,
242240
width: this.boxWidth
243241
});
244242

245243
this.$slider.css({
246-
transform: 'translate3d(0px, 0px, 0px)',
244+
transform: 'translate3d('+this.offsetLeft+'px, '+this.offsetTop+'px, 0px)',
247245
position: 'absolute',
248-
top: this.offsetTop,
249-
left: this.offsetLeft,
250246
height: this.imageHeight,
251247
width: this.imageWidth,
252248
maxWidth: 'none'
@@ -272,6 +268,8 @@
272268
setup: function() {
273269
if (this.isReady) return;
274270

271+
var self = this;
272+
275273
var $doc = $(document), $win = $(window);
276274

277275
var loadDimensions = function() {
@@ -292,6 +290,7 @@
292290

293291
$win.on('resize.px.parallax load.px.parallax', function() {
294292
loadDimensions();
293+
self.refresh();
295294
Parallax.isFresh = false;
296295
Parallax.requestRender();
297296
})
@@ -304,6 +303,20 @@
304303
loadScrollPosition();
305304

306305
this.isReady = true;
306+
307+
var lastPosition = -1;
308+
309+
function frameLoop() {
310+
if (lastPosition == window.pageYOffset) { // Avoid overcalculations
311+
window.requestAnimationFrame(frameLoop);
312+
return false;
313+
} else lastPosition = window.pageYOffset;
314+
315+
self.render();
316+
window.requestAnimationFrame(frameLoop);
317+
}
318+
319+
frameLoop();
307320
},
308321

309322
configure: function(options) {
@@ -315,25 +328,19 @@
315328
},
316329

317330
refresh: function() {
318-
$.each(this.sliders, function(){ this.refresh() });
331+
$.each(this.sliders, function(){ this.refresh(); });
319332
this.isFresh = true;
320333
},
321334

322335
render: function() {
323336
this.isFresh || this.refresh();
324-
$.each(this.sliders, function(){ this.render() });
337+
$.each(this.sliders, function(){ this.render(); });
325338
},
326339

327340
requestRender: function() {
328341
var self = this;
329-
330-
if (!this.isBusy) {
331-
this.isBusy = true;
332-
window.requestAnimationFrame(function() {
333-
self.render();
334-
self.isBusy = false;
335-
});
336-
}
342+
self.render();
343+
self.isBusy = false;
337344
},
338345
destroy: function(el){
339346
var i,
@@ -374,13 +381,13 @@
374381
}
375382
if (typeof option == 'string') {
376383
if(option == 'destroy'){
377-
Parallax['destroy'](this);
384+
Parallax.destroy(this);
378385
}else{
379386
Parallax[option]();
380387
}
381388
}
382-
})
383-
};
389+
});
390+
}
384391

385392
var old = $.fn.parallax;
386393

0 commit comments

Comments
 (0)