|
14 | 14 | var vendors = ['ms', 'moz', 'webkit', 'o'];
|
15 | 15 | for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
|
16 | 16 | 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']; |
19 | 18 | }
|
20 | 19 |
|
21 | 20 | if (!window.requestAnimationFrame)
|
|
65 | 64 | positions = [positions[1], positions[0]];
|
66 | 65 | }
|
67 | 66 |
|
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(); |
70 | 69 |
|
71 | 70 | self.positionX = positions[0];
|
72 | 71 | self.positionY = positions[1];
|
|
154 | 153 | this.$slider.trigger('load');
|
155 | 154 | }
|
156 | 155 |
|
157 |
| - }; |
| 156 | + } |
158 | 157 |
|
159 | 158 |
|
160 | 159 | // Parallax Instance Methods
|
|
182 | 181 | var minOffset = Math.max(this.boxOffsetTop + this.boxHeight - winHeight, 0);
|
183 | 182 | var imageHeightMin = this.boxHeight + (maxOffset - minOffset) * (1 - this.speed) | 0;
|
184 | 183 | var imageOffsetMin = (this.boxOffsetTop - maxOffset) * (1 - this.speed) | 0;
|
| 184 | + var margin; |
185 | 185 |
|
186 | 186 | if (imageHeightMin * this.aspectRatio >= this.boxWidth) {
|
187 | 187 | this.imageWidth = imageHeightMin * this.aspectRatio | 0;
|
188 | 188 | this.imageHeight = imageHeightMin;
|
189 | 189 | this.offsetBaseTop = imageOffsetMin;
|
190 | 190 |
|
191 |
| - var margin = this.imageWidth - this.boxWidth; |
| 191 | + margin = this.imageWidth - this.boxWidth; |
192 | 192 |
|
193 | 193 | if (this.positionX == 'left') {
|
194 | 194 | this.offsetLeft = 0;
|
|
204 | 204 | this.imageHeight = this.boxWidth / this.aspectRatio | 0;
|
205 | 205 | this.offsetLeft = 0;
|
206 | 206 |
|
207 |
| - var margin = this.imageHeight - imageHeightMin; |
| 207 | + margin = this.imageHeight - imageHeightMin; |
208 | 208 |
|
209 | 209 | if (this.positionY == 'top') {
|
210 | 210 | this.offsetBaseTop = imageOffsetMin;
|
|
234 | 234 | }
|
235 | 235 |
|
236 | 236 | this.$mirror.css({
|
237 |
| - transform: 'translate3d(0px, 0px, 0px)', |
| 237 | + transform: 'translate3d('+this.mirrorLeft+'px, '+(this.mirrorTop - overScroll)+'px, 0px)', |
238 | 238 | visibility: this.visibility,
|
239 |
| - top: this.mirrorTop - overScroll, |
240 |
| - left: this.mirrorLeft, |
241 | 239 | height: this.boxHeight,
|
242 | 240 | width: this.boxWidth
|
243 | 241 | });
|
244 | 242 |
|
245 | 243 | this.$slider.css({
|
246 |
| - transform: 'translate3d(0px, 0px, 0px)', |
| 244 | + transform: 'translate3d('+this.offsetLeft+'px, '+this.offsetTop+'px, 0px)', |
247 | 245 | position: 'absolute',
|
248 |
| - top: this.offsetTop, |
249 |
| - left: this.offsetLeft, |
250 | 246 | height: this.imageHeight,
|
251 | 247 | width: this.imageWidth,
|
252 | 248 | maxWidth: 'none'
|
|
272 | 268 | setup: function() {
|
273 | 269 | if (this.isReady) return;
|
274 | 270 |
|
| 271 | + var self = this; |
| 272 | + |
275 | 273 | var $doc = $(document), $win = $(window);
|
276 | 274 |
|
277 | 275 | var loadDimensions = function() {
|
|
292 | 290 |
|
293 | 291 | $win.on('resize.px.parallax load.px.parallax', function() {
|
294 | 292 | loadDimensions();
|
| 293 | + self.refresh(); |
295 | 294 | Parallax.isFresh = false;
|
296 | 295 | Parallax.requestRender();
|
297 | 296 | })
|
|
304 | 303 | loadScrollPosition();
|
305 | 304 |
|
306 | 305 | 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(); |
307 | 320 | },
|
308 | 321 |
|
309 | 322 | configure: function(options) {
|
|
315 | 328 | },
|
316 | 329 |
|
317 | 330 | refresh: function() {
|
318 |
| - $.each(this.sliders, function(){ this.refresh() }); |
| 331 | + $.each(this.sliders, function(){ this.refresh(); }); |
319 | 332 | this.isFresh = true;
|
320 | 333 | },
|
321 | 334 |
|
322 | 335 | render: function() {
|
323 | 336 | this.isFresh || this.refresh();
|
324 |
| - $.each(this.sliders, function(){ this.render() }); |
| 337 | + $.each(this.sliders, function(){ this.render(); }); |
325 | 338 | },
|
326 | 339 |
|
327 | 340 | requestRender: function() {
|
328 | 341 | 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; |
337 | 344 | },
|
338 | 345 | destroy: function(el){
|
339 | 346 | var i,
|
|
374 | 381 | }
|
375 | 382 | if (typeof option == 'string') {
|
376 | 383 | if(option == 'destroy'){
|
377 |
| - Parallax['destroy'](this); |
| 384 | + Parallax.destroy(this); |
378 | 385 | }else{
|
379 | 386 | Parallax[option]();
|
380 | 387 | }
|
381 | 388 | }
|
382 |
| - }) |
383 |
| - }; |
| 389 | + }); |
| 390 | + } |
384 | 391 |
|
385 | 392 | var old = $.fn.parallax;
|
386 | 393 |
|
|
0 commit comments