Skip to content

Commit 3b05936

Browse files
committed
Spring clean
1 parent 9827696 commit 3b05936

File tree

3 files changed

+163
-163
lines changed

3 files changed

+163
-163
lines changed

jquery.stellar.js

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* http://markdalgleish.mit-license.org
88
*/
99

10-
;(function($, window, document, undefined){
10+
;(function($, window, document, undefined) {
1111

1212
var pluginName = 'stellar',
1313
defaults = {
@@ -43,12 +43,12 @@
4343
},
4444
transform: {
4545
getLeft: function($elem) {
46-
var computedTransform = getComputedStyle($elem[0])[transform];
47-
return computedTransform !== 'none' ? parseInt(computedTransform.match(/(-?[0-9]+)/g)[4], 10) * -1 : 0;
46+
var computedTransform = getComputedStyle($elem[0])[prefixedTransform];
47+
return (computedTransform !== 'none' ? parseInt(computedTransform.match(/(-?[0-9]+)/g)[4], 10) * -1 : 0);
4848
},
4949
getTop: function($elem) {
50-
var computedTransform = getComputedStyle($elem[0])[transform];
51-
return computedTransform !== 'none' ? parseInt(computedTransform.match(/(-?[0-9]+)/g)[5], 10) * -1 : 0;
50+
var computedTransform = getComputedStyle($elem[0])[prefixedTransform];
51+
return (computedTransform !== 'none' ? parseInt(computedTransform.match(/(-?[0-9]+)/g)[5], 10) * -1 : 0);
5252
}
5353
}
5454
},
@@ -60,14 +60,15 @@
6060
},
6161
transform: {
6262
setPosition: function($elem, left, startingLeft, top, startingTop) {
63-
$elem[0].style[transform] = 'translate3d(' + (left - startingLeft) + 'px, ' + (top - startingTop) + 'px, 0)';
63+
$elem[0].style[prefixedTransform] = 'translate3d(' + (left - startingLeft) + 'px, ' + (top - startingTop) + 'px, 0)';
6464
}
6565
}
6666
},
6767

68+
// Returns a function which adds a vendor prefix to any CSS property name
6869
vendorPrefix = (function() {
6970
var prefixes = /^(Moz|Webkit|Khtml|O|ms|Icab)(?=[A-Z])/,
70-
style = document.getElementsByTagName('script')[0].style,
71+
style = $('script')[0].style,
7172
prefix = '',
7273
prop;
7374

@@ -86,46 +87,44 @@
8687
};
8788
}()),
8889

89-
transform = vendorPrefix('transform'),
90+
prefixedTransform = vendorPrefix('transform'),
9091

9192
supportsBackgroundPositionXY = $('<div />').css('background-position-x') !== undefined,
9293

93-
setBackgroundPosition = (function() {
94-
return supportsBackgroundPositionXY ?
95-
function($elem, x, y) {
96-
$elem.css({
97-
'background-position-x': x,
98-
'background-position-y': y
99-
});
100-
} :
101-
function($elem, x, y) {
102-
$elem.css('background-position', x + ' ' + y);
103-
};
104-
}()),
105-
106-
getBackgroundPosition = (function() {
107-
return supportsBackgroundPositionXY ?
108-
function($elem) {
109-
return [
110-
$elem.css('background-position-x'),
111-
$elem.css('background-position-y')
112-
];
113-
} :
114-
function($elem) {
115-
return $elem.css('background-position').split(' ');
116-
};
117-
}()),
118-
119-
requestAnimFrame = (function(){
120-
return window.requestAnimationFrame ||
121-
window.webkitRequestAnimationFrame ||
122-
window.mozRequestAnimationFrame ||
123-
window.oRequestAnimationFrame ||
124-
window.msRequestAnimationFrame ||
125-
function(callback, element){
126-
window.setTimeout(callback, 1000 / 60);
127-
};
128-
}());
94+
setBackgroundPosition = (supportsBackgroundPositionXY ?
95+
function($elem, x, y) {
96+
$elem.css({
97+
'background-position-x': x,
98+
'background-position-y': y
99+
});
100+
} :
101+
function($elem, x, y) {
102+
$elem.css('background-position', x + ' ' + y);
103+
}
104+
),
105+
106+
getBackgroundPosition = (supportsBackgroundPositionXY ?
107+
function($elem) {
108+
return [
109+
$elem.css('background-position-x'),
110+
$elem.css('background-position-y')
111+
];
112+
} :
113+
function($elem) {
114+
return $elem.css('background-position').split(' ');
115+
}
116+
),
117+
118+
requestAnimFrame = (
119+
window.requestAnimationFrame ||
120+
window.webkitRequestAnimationFrame ||
121+
window.mozRequestAnimationFrame ||
122+
window.oRequestAnimationFrame ||
123+
window.msRequestAnimationFrame ||
124+
function(callback) {
125+
setTimeout(callback, 1000 / 60);
126+
}
127+
);
129128

130129
function Plugin(element, options) {
131130
this.element = element;
@@ -139,14 +138,14 @@
139138

140139
Plugin.prototype = {
141140
init: function() {
142-
this.options.name = pluginName + '_' + Math.floor(Math.random()*10000);
141+
this.options.name = pluginName + '_' + Math.floor(Math.random() * 1e9);
143142

144143
this._defineElements();
145144
this._defineGetters();
146145
this._defineSetters();
147146
this._handleWindowLoadAndResize();
148-
149147
this._detectViewport();
148+
150149
this.refresh({ firstLoad: true });
151150

152151
if (this.options.scrollProperty === 'scroll') {
@@ -158,18 +157,19 @@
158157
_defineElements: function() {
159158
if (this.element === document.body) this.element = window;
160159
this.$scrollElement = $(this.element);
161-
this.$element = this.element === window ? $('body') : this.$scrollElement;
162-
this.$viewportElement = (this.options.viewportElement !== undefined ? $(this.options.viewportElement) : (this.$scrollElement[0] === window || this.options.scrollProperty.indexOf('scroll') === 0 ? this.$scrollElement : this.$scrollElement.parent()) );
160+
this.$element = (this.element === window ? $('body') : this.$scrollElement);
161+
this.$viewportElement = (this.options.viewportElement !== undefined ? $(this.options.viewportElement) : (this.$scrollElement[0] === window || this.options.scrollProperty === 'scroll' ? this.$scrollElement : this.$scrollElement.parent()) );
163162
},
164163
_defineGetters: function() {
165-
var self = this;
164+
var self = this,
165+
scrollPropertyAdapter = scrollProperty[self.options.scrollProperty];
166166

167167
this._getScrollLeft = function() {
168-
return scrollProperty[self.options.scrollProperty].getLeft(self.$scrollElement);
168+
return scrollPropertyAdapter.getLeft(self.$scrollElement);
169169
};
170170

171171
this._getScrollTop = function() {
172-
return scrollProperty[self.options.scrollProperty].getTop(self.$scrollElement);
172+
return scrollPropertyAdapter.getTop(self.$scrollElement);
173173
};
174174
},
175175
_defineSetters: function() {
@@ -179,13 +179,13 @@
179179
setScrollLeft = scrollPropertyAdapter.setLeft,
180180
setScrollTop = scrollPropertyAdapter.setTop;
181181

182-
this._setScrollLeft = typeof setScrollLeft === 'function' ? function(val) {
182+
this._setScrollLeft = (typeof setScrollLeft === 'function' ? function(val) {
183183
setScrollLeft(self.$scrollElement, val);
184-
} : $.noop;
184+
} : $.noop);
185185

186-
this._setScrollTop = typeof setScrollTop === 'function' ? function(val) {
186+
this._setScrollTop = (typeof setScrollTop === 'function' ? function(val) {
187187
setScrollTop(self.$scrollElement, val);
188-
} : $.noop;
188+
} : $.noop);
189189

190190
this._setPosition = positionPropertyAdapter.setPosition ||
191191
function($elem, left, startingLeft, top, startingTop) {
@@ -230,7 +230,7 @@
230230

231231
// Fix for WebKit background rendering bug
232232
if (options && options.firstLoad && /WebKit/.test(navigator.userAgent)) {
233-
$(window).load(function(){
233+
$(window).load(function() {
234234
var oldLeft = self._getScrollLeft(),
235235
oldTop = self._getScrollTop();
236236

@@ -252,10 +252,10 @@
252252
this.viewportWidth = this.$viewportElement.width();
253253
this.viewportHeight = this.$viewportElement.height();
254254

255-
this.viewportOffsetTop = hasOffsets ? viewportOffsets.top : 0;
256-
this.viewportOffsetLeft = hasOffsets ? viewportOffsets.left : 0;
255+
this.viewportOffsetTop = (hasOffsets ? viewportOffsets.top : 0);
256+
this.viewportOffsetLeft = (hasOffsets ? viewportOffsets.left : 0);
257257
},
258-
_findParticles: function(){
258+
_findParticles: function() {
259259
var self = this,
260260
scrollLeft = this._getScrollLeft(),
261261
scrollTop = this._getScrollTop();
@@ -270,7 +270,7 @@
270270

271271
if (!this.options.parallaxElements) return;
272272

273-
this.$element.find('[data-stellar-ratio]').each(function(i){
273+
this.$element.find('[data-stellar-ratio]').each(function(i) {
274274
var $this = $(this),
275275
horizontalOffset,
276276
verticalOffset,
@@ -315,7 +315,7 @@
315315
offsetTop = $this.offset().top - marginTop;
316316

317317
// Calculate the offset parent
318-
$this.parents().each(function(){
318+
$this.parents().each(function() {
319319
var $this = $(this);
320320

321321
if ($this.data('stellar-offset-parent') === true) {
@@ -334,7 +334,7 @@
334334
horizontalOffset = ($this.data('stellar-horizontal-offset') !== undefined ? $this.data('stellar-horizontal-offset') : ($offsetParent !== undefined && $offsetParent.data('stellar-horizontal-offset') !== undefined ? $offsetParent.data('stellar-horizontal-offset') : self.horizontalOffset));
335335
verticalOffset = ($this.data('stellar-vertical-offset') !== undefined ? $this.data('stellar-vertical-offset') : ($offsetParent !== undefined && $offsetParent.data('stellar-vertical-offset') !== undefined ? $offsetParent.data('stellar-vertical-offset') : self.verticalOffset));
336336

337-
//Add our object to the particles collection
337+
// Add our object to the particles collection
338338
self.particles.push({
339339
$element: $this,
340340
$offsetParent: $offsetParent,
@@ -347,7 +347,7 @@
347347
startingOffsetTop: offsetTop,
348348
parentOffsetLeft: parentOffsetLeft,
349349
parentOffsetTop: parentOffsetTop,
350-
stellarRatio: $this.data('stellar-ratio') !== undefined ? $this.data('stellar-ratio') : 1,
350+
stellarRatio: ($this.data('stellar-ratio') !== undefined ? $this.data('stellar-ratio') : 1),
351351
width: $this.outerWidth(true),
352352
height: $this.outerHeight(true),
353353
isHidden: false
@@ -370,7 +370,7 @@
370370
$backgroundElements.add(this.$element);
371371
}
372372

373-
$backgroundElements.each(function(){
373+
$backgroundElements.each(function() {
374374
var $this = $(this),
375375
backgroundPosition = getBackgroundPosition($this),
376376
horizontalOffset,
@@ -410,7 +410,7 @@
410410
offsetTop = $this.offset().top - marginTop - scrollTop;
411411

412412
// Calculate the offset parent
413-
$this.parents().each(function(){
413+
$this.parents().each(function() {
414414
var $this = $(this);
415415

416416
if ($this.data('stellar-offset-parent') === true) {
@@ -437,15 +437,15 @@
437437
verticalOffset: verticalOffset,
438438
startingValueLeft: backgroundPosition[0],
439439
startingValueTop: backgroundPosition[1],
440-
startingBackgroundPositionLeft: isNaN(parseInt(backgroundPosition[0], 10)) ? 0 : parseInt(backgroundPosition[0], 10),
441-
startingBackgroundPositionTop: isNaN(parseInt(backgroundPosition[1], 10)) ? 0 : parseInt(backgroundPosition[1], 10),
440+
startingBackgroundPositionLeft: (isNaN(parseInt(backgroundPosition[0], 10)) ? 0 : parseInt(backgroundPosition[0], 10)),
441+
startingBackgroundPositionTop: (isNaN(parseInt(backgroundPosition[1], 10)) ? 0 : parseInt(backgroundPosition[1], 10)),
442442
startingPositionLeft: $this.position().left,
443443
startingPositionTop: $this.position().top,
444444
startingOffsetLeft: offsetLeft,
445445
startingOffsetTop: offsetTop,
446446
parentOffsetLeft: parentOffsetLeft,
447447
parentOffsetTop: parentOffsetTop,
448-
stellarRatio: $this.data('stellar-background-ratio') === undefined ? 1 : $this.data('stellar-background-ratio')
448+
stellarRatio: ($this.data('stellar-background-ratio') === undefined ? 1 : $this.data('stellar-background-ratio'))
449449
});
450450
});
451451
},
@@ -520,7 +520,7 @@
520520
newOffsetTop,
521521
i;
522522

523-
//First check that the scroll position or container size has changed
523+
// First check that the scroll position or container size has changed
524524
if (this.currentScrollLeft === scrollLeft && this.currentScrollTop === scrollTop && this.currentWidth === this.viewportWidth && this.currentHeight === this.viewportHeight) {
525525
return;
526526
} else {
@@ -530,13 +530,13 @@
530530
this.currentHeight = this.viewportHeight;
531531
}
532532

533-
//Reposition elements
533+
// Reposition elements
534534
for (i = this.particles.length - 1; i >= 0; i--) {
535535
particle = this.particles[i];
536536

537-
fixedRatioOffset = particle.isFixed ? 1 : 0;
537+
fixedRatioOffset = (particle.isFixed ? 1 : 0);
538538

539-
//Calculate position, then calculate what the particle's new offset will be (for visibility check)
539+
// Calculate position, then calculate what the particle's new offset will be (for visibility check)
540540
if (this.options.horizontalScrolling) {
541541
newPositionLeft = (scrollLeft + particle.horizontalOffset + this.viewportOffsetLeft + particle.startingPositionLeft - particle.startingOffsetLeft + particle.parentOffsetLeft) * -(particle.stellarRatio + fixedRatioOffset - 1) + particle.startingPositionLeft;
542542
newOffsetLeft = newPositionLeft - particle.startingPositionLeft + particle.startingOffsetLeft;
@@ -553,7 +553,7 @@
553553
newOffsetTop = particle.startingOffsetTop;
554554
}
555555

556-
//Check visibility
556+
// Check visibility
557557
if (this.options.hideDistantElements) {
558558
isVisibleHorizontal = !this.options.horizontalScrolling || newOffsetLeft + particle.width > (particle.isFixed ? 0 : scrollLeft) && newOffsetLeft < (particle.isFixed ? 0 : scrollLeft) + this.viewportWidth + this.viewportOffsetLeft;
559559
isVisibleVertical = !this.options.verticalScrolling || newOffsetTop + particle.height > (particle.isFixed ? 0 : scrollTop) && newOffsetTop < (particle.isFixed ? 0 : scrollTop) + this.viewportHeight + this.viewportOffsetTop;
@@ -574,13 +574,13 @@
574574
}
575575
}
576576

577-
//Reposition backgrounds
577+
// Reposition backgrounds
578578
for (i = this.backgrounds.length - 1; i >= 0; i--) {
579579
background = this.backgrounds[i];
580580

581-
fixedRatioOffset = background.isFixed ? 0 : 1;
582-
bgLeft = this.options.horizontalScrolling ? (scrollLeft + background.horizontalOffset - this.viewportOffsetLeft - background.startingOffsetLeft + background.parentOffsetLeft - background.startingBackgroundPositionLeft) * (fixedRatioOffset - background.stellarRatio) + 'px' : background.startingValueLeft;
583-
bgTop = this.options.verticalScrolling ? (scrollTop + background.verticalOffset - this.viewportOffsetTop - background.startingOffsetTop + background.parentOffsetTop - background.startingBackgroundPositionTop) * (fixedRatioOffset - background.stellarRatio) + 'px' : background.startingValueTop;
581+
fixedRatioOffset = (background.isFixed ? 0 : 1);
582+
bgLeft = (this.options.horizontalScrolling ? (scrollLeft + background.horizontalOffset - this.viewportOffsetLeft - background.startingOffsetLeft + background.parentOffsetLeft - background.startingBackgroundPositionLeft) * (fixedRatioOffset - background.stellarRatio) + 'px' : background.startingValueLeft);
583+
bgTop = (this.options.verticalScrolling ? (scrollTop + background.verticalOffset - this.viewportOffsetTop - background.startingOffsetTop + background.parentOffsetTop - background.startingBackgroundPositionTop) * (fixedRatioOffset - background.stellarRatio) + 'px' : background.startingValueTop);
584584

585585
setBackgroundPosition(background.$element, bgLeft, bgTop);
586586
}
@@ -607,7 +607,7 @@
607607
_startAnimationLoop: function() {
608608
var self = this;
609609

610-
this._animationLoop = function(){
610+
this._animationLoop = function() {
611611
requestAnimFrame(self._animationLoop);
612612
self._repositionElements();
613613
};
@@ -641,10 +641,10 @@
641641
return $window.stellar.apply($window, Array.prototype.slice.call(arguments, 0));
642642
};
643643

644-
//Expose the scroll and position property function hashes so they can be extended
644+
// Expose the scroll and position property function hashes so they can be extended
645645
$[pluginName].scrollProperty = scrollProperty;
646646
$[pluginName].positionProperty = positionProperty;
647647

648-
//Expose the plugin class so it can be modified
648+
// Expose the plugin class so it can be modified
649649
window.Stellar = Plugin;
650-
}(jQuery, window, document));
650+
}(jQuery, this, document));

0 commit comments

Comments
 (0)