|
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];
|
|
113 | 112 | return this;
|
114 | 113 | }
|
115 | 114 |
|
116 |
| - this.$mirror = $('<div />').prependTo('body'); |
| 115 | + this.$mirror = $('<div />').prependTo(this.mirrorContainer); |
117 | 116 |
|
118 | 117 | var slider = this.$element.find('>.parallax-slider');
|
119 | 118 | var sliderExisted = false;
|
|
154 | 153 | this.$slider.trigger('load');
|
155 | 154 | }
|
156 | 155 |
|
157 |
| - }; |
| 156 | + } |
158 | 157 |
|
159 | 158 |
|
160 | 159 | // Parallax Instance Methods
|
|
167 | 166 | androidFix: true,
|
168 | 167 | position: 'center',
|
169 | 168 | overScrollFix: false,
|
| 169 | + mirrorContainer: 'body', |
170 | 170 |
|
171 | 171 | refresh: function() {
|
172 | 172 | this.boxWidth = this.$element.outerWidth();
|
|
181 | 181 | var minOffset = Math.max(this.boxOffsetTop + this.boxHeight - winHeight, 0);
|
182 | 182 | var imageHeightMin = this.boxHeight + (maxOffset - minOffset) * (1 - this.speed) | 0;
|
183 | 183 | var imageOffsetMin = (this.boxOffsetTop - maxOffset) * (1 - this.speed) | 0;
|
| 184 | + var margin; |
184 | 185 |
|
185 | 186 | if (imageHeightMin * this.aspectRatio >= this.boxWidth) {
|
186 | 187 | this.imageWidth = imageHeightMin * this.aspectRatio | 0;
|
187 | 188 | this.imageHeight = imageHeightMin;
|
188 | 189 | this.offsetBaseTop = imageOffsetMin;
|
189 | 190 |
|
190 |
| - var margin = this.imageWidth - this.boxWidth; |
| 191 | + margin = this.imageWidth - this.boxWidth; |
191 | 192 |
|
192 | 193 | if (this.positionX == 'left') {
|
193 | 194 | this.offsetLeft = 0;
|
|
203 | 204 | this.imageHeight = this.boxWidth / this.aspectRatio | 0;
|
204 | 205 | this.offsetLeft = 0;
|
205 | 206 |
|
206 |
| - var margin = this.imageHeight - imageHeightMin; |
| 207 | + margin = this.imageHeight - imageHeightMin; |
207 | 208 |
|
208 | 209 | if (this.positionY == 'top') {
|
209 | 210 | this.offsetBaseTop = imageOffsetMin;
|
|
233 | 234 | }
|
234 | 235 |
|
235 | 236 | this.$mirror.css({
|
236 |
| - transform: 'translate3d(0px, 0px, 0px)', |
| 237 | + transform: 'translate3d('+this.mirrorLeft+'px, '+(this.mirrorTop - overScroll)+'px, 0px)', |
237 | 238 | visibility: this.visibility,
|
238 |
| - top: this.mirrorTop - overScroll, |
239 |
| - left: this.mirrorLeft, |
240 | 239 | height: this.boxHeight,
|
241 | 240 | width: this.boxWidth
|
242 | 241 | });
|
243 | 242 |
|
244 | 243 | this.$slider.css({
|
245 |
| - transform: 'translate3d(0px, 0px, 0px)', |
| 244 | + transform: 'translate3d('+this.offsetLeft+'px, '+this.offsetTop+'px, 0px)', |
246 | 245 | position: 'absolute',
|
247 |
| - top: this.offsetTop, |
248 |
| - left: this.offsetLeft, |
249 | 246 | height: this.imageHeight,
|
250 | 247 | width: this.imageWidth,
|
251 | 248 | maxWidth: 'none'
|
|
271 | 268 | setup: function() {
|
272 | 269 | if (this.isReady) return;
|
273 | 270 |
|
| 271 | + var self = this; |
| 272 | + |
274 | 273 | var $doc = $(document), $win = $(window);
|
275 | 274 |
|
276 | 275 | var loadDimensions = function() {
|
|
291 | 290 |
|
292 | 291 | $win.on('resize.px.parallax load.px.parallax', function() {
|
293 | 292 | loadDimensions();
|
| 293 | + self.refresh(); |
294 | 294 | Parallax.isFresh = false;
|
295 | 295 | Parallax.requestRender();
|
296 | 296 | })
|
|
303 | 303 | loadScrollPosition();
|
304 | 304 |
|
305 | 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(); |
306 | 320 | },
|
307 | 321 |
|
308 | 322 | configure: function(options) {
|
|
314 | 328 | },
|
315 | 329 |
|
316 | 330 | refresh: function() {
|
317 |
| - $.each(this.sliders, function(){ this.refresh() }); |
| 331 | + $.each(this.sliders, function(){ this.refresh(); }); |
318 | 332 | this.isFresh = true;
|
319 | 333 | },
|
320 | 334 |
|
321 | 335 | render: function() {
|
322 | 336 | this.isFresh || this.refresh();
|
323 |
| - $.each(this.sliders, function(){ this.render() }); |
| 337 | + $.each(this.sliders, function(){ this.render(); }); |
324 | 338 | },
|
325 | 339 |
|
326 | 340 | requestRender: function() {
|
327 | 341 | var self = this;
|
328 |
| - |
329 |
| - if (!this.isBusy) { |
330 |
| - this.isBusy = true; |
331 |
| - window.requestAnimationFrame(function() { |
332 |
| - self.render(); |
333 |
| - self.isBusy = false; |
334 |
| - }); |
335 |
| - } |
| 342 | + self.render(); |
| 343 | + self.isBusy = false; |
336 | 344 | },
|
337 | 345 | destroy: function(el){
|
338 | 346 | var i,
|
|
373 | 381 | }
|
374 | 382 | if (typeof option == 'string') {
|
375 | 383 | if(option == 'destroy'){
|
376 |
| - Parallax['destroy'](this); |
| 384 | + Parallax.destroy(this); |
377 | 385 | }else{
|
378 | 386 | Parallax[option]();
|
379 | 387 | }
|
380 | 388 | }
|
381 |
| - }) |
382 |
| - }; |
| 389 | + }); |
| 390 | + } |
383 | 391 |
|
384 | 392 | var old = $.fn.parallax;
|
385 | 393 |
|
|
397 | 405 |
|
398 | 406 | // Parallax Data-API
|
399 | 407 |
|
400 |
| - $(document).on('ready.px.parallax.data-api', function () { |
401 |
| - $('[data-parallax="scroll"]').parallax(); |
| 408 | + $( function () { |
| 409 | + $('[data-parallax="scroll"]').parallax(); |
402 | 410 | });
|
403 | 411 |
|
404 | 412 | }(jQuery, window, document));
|
0 commit comments