|
1 |
| -/* Stellar.js v0.1 |
| 1 | +/* Stellar.js v0.2 |
2 | 2 | * Copyright 2012, Mark Dalgleish
|
3 | 3 | *
|
4 | 4 | * This content is released under the MIT License
|
|
171 | 171 | };
|
172 | 172 | },
|
173 | 173 | refresh: function() {
|
174 |
| - var self = this; |
| 174 | + var self = this, |
| 175 | + oldLeft = self._getScrollLeft(), |
| 176 | + oldTop = self._getScrollTop(); |
175 | 177 |
|
176 | 178 | this._setScrollLeft(0);
|
177 | 179 | this._setScrollTop(0);
|
|
180 | 182 | this._findParticles();
|
181 | 183 | this._findBackgrounds();
|
182 | 184 |
|
183 |
| - // Fix for Webkit background rendering bug |
184 |
| - if (navigator.userAgent.indexOf('Webkit') > 0) { |
| 185 | + // Fix for WebKit background rendering bug |
| 186 | + if (navigator.userAgent.indexOf('WebKit') > 0) { |
185 | 187 | $(window).load(function(){
|
186 | 188 | var oldLeft = self._getScrollLeft(),
|
187 | 189 | oldTop = self._getScrollTop();
|
188 |
| - |
| 190 | + |
189 | 191 | self._setScrollLeft(oldLeft + 1);
|
190 | 192 | self._setScrollTop(oldTop + 1);
|
191 |
| - |
| 193 | + |
192 | 194 | self._setScrollLeft(oldLeft);
|
193 | 195 | self._setScrollTop(oldTop);
|
194 | 196 | });
|
195 | 197 | }
|
| 198 | + |
| 199 | + self._setScrollLeft(oldLeft); |
| 200 | + self._setScrollTop(oldTop); |
196 | 201 | },
|
197 | 202 | _findParticles: function(){
|
198 | 203 | var self = this,
|
|
224 | 229 | tempParentOffsetTop = 0;
|
225 | 230 |
|
226 | 231 | // Ensure this element isn't already part of another scrolling element
|
227 |
| - if ($this.data('stellar-elementIsActive') === undefined) { |
228 |
| - $this.data('stellar-elementIsActive', true); |
229 |
| - } else { |
| 232 | + if (!$this.data('stellar-elementIsActive')) { |
| 233 | + $this.data('stellar-elementIsActive', this); |
| 234 | + } else if ($this.data('stellar-elementIsActive') !== this) { |
230 | 235 | return;
|
231 | 236 | }
|
232 | 237 |
|
233 | 238 | self.options.showElement($this);
|
234 | 239 |
|
235 |
| - // Save/restore the original top and left CSS values in case we refresh the particles |
236 |
| - if ($this.data('stellar-startingLeft') === undefined) { |
| 240 | + // Save/restore the original top and left CSS values in case we refresh the particles or destroy the instance |
| 241 | + if (!$this.data('stellar-startingLeft')) { |
237 | 242 | $this.data('stellar-startingLeft', $this.css('left'));
|
238 | 243 | $this.data('stellar-startingTop', $this.css('top'));
|
239 | 244 | } else {
|
|
293 | 298 | scrollTop = this._getScrollTop(),
|
294 | 299 | $backgroundElements;
|
295 | 300 |
|
296 |
| - if (this.background !== undefined) { |
297 |
| - for (var i = this.backgrounds.length - 1; i >= 0; i--) { |
298 |
| - this.backgrounds[i].$element.data('stellar-backgroundIsActive', undefined); |
299 |
| - } |
300 |
| - } |
301 |
| - |
302 | 301 | this.backgrounds = [];
|
303 | 302 |
|
304 | 303 | if (!this.options.parallaxBackgrounds) return;
|
|
320 | 319 | offsetTop;
|
321 | 320 |
|
322 | 321 | // Ensure this element isn't already part of another scrolling element
|
323 |
| - if ($this.data('stellar-backgroundIsActive') === undefined) { |
324 |
| - $this.data('stellar-backgroundIsActive', true); |
325 |
| - } else { |
| 322 | + if (!$this.data('stellar-backgroundIsActive')) { |
| 323 | + $this.data('stellar-backgroundIsActive', this); |
| 324 | + } else if ($this.data('stellar-backgroundIsActive') !== this) { |
326 | 325 | return;
|
327 | 326 | }
|
328 | 327 |
|
| 328 | + // Save/restore the original top and left CSS values in case we destroy the instance |
| 329 | + if (!$this.data('stellar-backgroundStartingLeft')) { |
| 330 | + $this.data('stellar-backgroundStartingLeft', backgroundPosition[0]); |
| 331 | + $this.data('stellar-backgroundStartingTop', backgroundPosition[1]); |
| 332 | + } else { |
| 333 | + $this.css('background-position', $this.data('stellar-backgroundStartingLeft') + ' ' + $this.data('stellar-backgroundStartingTop')); |
| 334 | + } |
| 335 | + |
329 | 336 | offsetLeft = $this.offset().left - parseInt($this.css('margin-left'), 10) - scrollLeft;
|
330 | 337 | offsetTop = $this.offset().top - parseInt($this.css('margin-top'), 10) - scrollTop;
|
331 | 338 |
|
|
350 | 357 | });
|
351 | 358 | });
|
352 | 359 | },
|
| 360 | + destroy: function() { |
| 361 | + var particle, |
| 362 | + startingPositionLeft, |
| 363 | + startingPositionTop, |
| 364 | + background; |
| 365 | + |
| 366 | + for (var i = this.particles.length - 1; i >= 0; i--) { |
| 367 | + particle = this.particles[i]; |
| 368 | + startingPositionLeft = particle.$element.data('stellar-startingLeft'); |
| 369 | + startingPositionTop = particle.$element.data('stellar-startingTop'); |
| 370 | + |
| 371 | + this._setLeft(particle.$element, startingPositionLeft, startingPositionLeft); |
| 372 | + this._setTop(particle.$element, startingPositionTop, startingPositionTop); |
| 373 | + |
| 374 | + this.options.showElement(particle.$element); |
| 375 | + |
| 376 | + particle.$element.data('stellar-startingLeft', null).data('stellar-elementIsActive', null).data('stellar-backgroundIsActive', null); |
| 377 | + } |
| 378 | + |
| 379 | + for (var i = this.backgrounds.length - 1; i >= 0; i--) { |
| 380 | + background = this.backgrounds[i]; |
| 381 | + background.$element.css('background-position', background.startingValueLeft + ' ' + background.startingValueTop); |
| 382 | + } |
| 383 | + |
| 384 | + this._animationLoop = $.noop; |
| 385 | + clearInterval(this._viewportDetectionInterval); |
| 386 | + }, |
353 | 387 | _setOffsets: function() {
|
354 | 388 | var self = this;
|
355 | 389 |
|
|
467 | 501 | };
|
468 | 502 |
|
469 | 503 | detect();
|
470 |
| - setInterval(detect, this.options.viewportDetectionInterval); |
| 504 | + this._viewportDetectionInterval = setInterval(detect, this.options.viewportDetectionInterval); |
471 | 505 | },
|
472 | 506 | _startAnimationLoop: function() {
|
473 | 507 | var self = this,
|
474 | 508 | requestAnimFrame = (function(){
|
475 |
| - return window.requestAnimationFrame || |
| 509 | + return window.requestAnimationFrame || |
476 | 510 | window.webkitRequestAnimationFrame ||
|
477 | 511 | window.mozRequestAnimationFrame ||
|
478 | 512 | window.oRequestAnimationFrame ||
|
479 | 513 | window.msRequestAnimationFrame ||
|
480 | 514 | function(callback, element){
|
481 | 515 | window.setTimeout(callback, 1000 / 60);
|
482 | 516 | };
|
483 |
| - })(); |
484 |
| - |
485 |
| - (function animloop(){ |
486 |
| - requestAnimFrame(animloop); |
487 |
| - self._repositionElements(); |
488 |
| - })(); |
| 517 | + })(); |
| 518 | + |
| 519 | + this._animationLoop = function(){ |
| 520 | + requestAnimFrame(self._animationLoop); |
| 521 | + self._repositionElements(); |
| 522 | + }; |
| 523 | + this._animationLoop(); |
489 | 524 | }
|
490 | 525 | };
|
491 | 526 |
|
|
503 | 538 | if (instance instanceof Plugin && typeof instance[options] === 'function') {
|
504 | 539 | instance[options].apply(instance, Array.prototype.slice.call(args, 1));
|
505 | 540 | }
|
| 541 | + if (options === 'destroy') { |
| 542 | + $.data(this, 'plugin_' + pluginName, null); |
| 543 | + } |
506 | 544 | });
|
507 | 545 | }
|
508 | 546 | };
|
|
0 commit comments