7
7
* http://markdalgleish.mit-license.org
8
8
*/
9
9
10
- ; ( function ( $ , window , document , undefined ) {
10
+ ; ( function ( $ , window , document , undefined ) {
11
11
12
12
var pluginName = 'stellar' ,
13
13
defaults = {
43
43
} ,
44
44
transform : {
45
45
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 ) ;
48
48
} ,
49
49
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 ) ;
52
52
}
53
53
}
54
54
} ,
60
60
} ,
61
61
transform : {
62
62
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)' ;
64
64
}
65
65
}
66
66
} ,
67
67
68
+ // Returns a function which adds a vendor prefix to any CSS property name
68
69
vendorPrefix = ( function ( ) {
69
70
var prefixes = / ^ ( M o z | W e b k i t | K h t m l | O | m s | I c a b ) (? = [ A - Z ] ) / ,
70
- style = document . getElementsByTagName ( 'script' ) [ 0 ] . style ,
71
+ style = $ ( 'script' ) [ 0 ] . style ,
71
72
prefix = '' ,
72
73
prop ;
73
74
86
87
} ;
87
88
} ( ) ) ,
88
89
89
- transform = vendorPrefix ( 'transform' ) ,
90
+ prefixedTransform = vendorPrefix ( 'transform' ) ,
90
91
91
92
supportsBackgroundPositionXY = $ ( '<div />' ) . css ( 'background-position-x' ) !== undefined ,
92
93
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
+ ) ;
129
128
130
129
function Plugin ( element , options ) {
131
130
this . element = element ;
139
138
140
139
Plugin . prototype = {
141
140
init : function ( ) {
142
- this . options . name = pluginName + '_' + Math . floor ( Math . random ( ) * 10000 ) ;
141
+ this . options . name = pluginName + '_' + Math . floor ( Math . random ( ) * 1e9 ) ;
143
142
144
143
this . _defineElements ( ) ;
145
144
this . _defineGetters ( ) ;
146
145
this . _defineSetters ( ) ;
147
146
this . _handleWindowLoadAndResize ( ) ;
148
-
149
147
this . _detectViewport ( ) ;
148
+
150
149
this . refresh ( { firstLoad : true } ) ;
151
150
152
151
if ( this . options . scrollProperty === 'scroll' ) {
158
157
_defineElements : function ( ) {
159
158
if ( this . element === document . body ) this . element = window ;
160
159
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 ( ) ) ) ;
163
162
} ,
164
163
_defineGetters : function ( ) {
165
- var self = this ;
164
+ var self = this ,
165
+ scrollPropertyAdapter = scrollProperty [ self . options . scrollProperty ] ;
166
166
167
167
this . _getScrollLeft = function ( ) {
168
- return scrollProperty [ self . options . scrollProperty ] . getLeft ( self . $scrollElement ) ;
168
+ return scrollPropertyAdapter . getLeft ( self . $scrollElement ) ;
169
169
} ;
170
170
171
171
this . _getScrollTop = function ( ) {
172
- return scrollProperty [ self . options . scrollProperty ] . getTop ( self . $scrollElement ) ;
172
+ return scrollPropertyAdapter . getTop ( self . $scrollElement ) ;
173
173
} ;
174
174
} ,
175
175
_defineSetters : function ( ) {
179
179
setScrollLeft = scrollPropertyAdapter . setLeft ,
180
180
setScrollTop = scrollPropertyAdapter . setTop ;
181
181
182
- this . _setScrollLeft = typeof setScrollLeft === 'function' ? function ( val ) {
182
+ this . _setScrollLeft = ( typeof setScrollLeft === 'function' ? function ( val ) {
183
183
setScrollLeft ( self . $scrollElement , val ) ;
184
- } : $ . noop ;
184
+ } : $ . noop ) ;
185
185
186
- this . _setScrollTop = typeof setScrollTop === 'function' ? function ( val ) {
186
+ this . _setScrollTop = ( typeof setScrollTop === 'function' ? function ( val ) {
187
187
setScrollTop ( self . $scrollElement , val ) ;
188
- } : $ . noop ;
188
+ } : $ . noop ) ;
189
189
190
190
this . _setPosition = positionPropertyAdapter . setPosition ||
191
191
function ( $elem , left , startingLeft , top , startingTop ) {
230
230
231
231
// Fix for WebKit background rendering bug
232
232
if ( options && options . firstLoad && / W e b K i t / . test ( navigator . userAgent ) ) {
233
- $ ( window ) . load ( function ( ) {
233
+ $ ( window ) . load ( function ( ) {
234
234
var oldLeft = self . _getScrollLeft ( ) ,
235
235
oldTop = self . _getScrollTop ( ) ;
236
236
252
252
this . viewportWidth = this . $viewportElement . width ( ) ;
253
253
this . viewportHeight = this . $viewportElement . height ( ) ;
254
254
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 ) ;
257
257
} ,
258
- _findParticles : function ( ) {
258
+ _findParticles : function ( ) {
259
259
var self = this ,
260
260
scrollLeft = this . _getScrollLeft ( ) ,
261
261
scrollTop = this . _getScrollTop ( ) ;
270
270
271
271
if ( ! this . options . parallaxElements ) return ;
272
272
273
- this . $element . find ( '[data-stellar-ratio]' ) . each ( function ( i ) {
273
+ this . $element . find ( '[data-stellar-ratio]' ) . each ( function ( i ) {
274
274
var $this = $ ( this ) ,
275
275
horizontalOffset ,
276
276
verticalOffset ,
315
315
offsetTop = $this . offset ( ) . top - marginTop ;
316
316
317
317
// Calculate the offset parent
318
- $this . parents ( ) . each ( function ( ) {
318
+ $this . parents ( ) . each ( function ( ) {
319
319
var $this = $ ( this ) ;
320
320
321
321
if ( $this . data ( 'stellar-offset-parent' ) === true ) {
334
334
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 ) ) ;
335
335
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 ) ) ;
336
336
337
- //Add our object to the particles collection
337
+ // Add our object to the particles collection
338
338
self . particles . push ( {
339
339
$element : $this ,
340
340
$offsetParent : $offsetParent ,
347
347
startingOffsetTop : offsetTop ,
348
348
parentOffsetLeft : parentOffsetLeft ,
349
349
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 ) ,
351
351
width : $this . outerWidth ( true ) ,
352
352
height : $this . outerHeight ( true ) ,
353
353
isHidden : false
370
370
$backgroundElements . add ( this . $element ) ;
371
371
}
372
372
373
- $backgroundElements . each ( function ( ) {
373
+ $backgroundElements . each ( function ( ) {
374
374
var $this = $ ( this ) ,
375
375
backgroundPosition = getBackgroundPosition ( $this ) ,
376
376
horizontalOffset ,
410
410
offsetTop = $this . offset ( ) . top - marginTop - scrollTop ;
411
411
412
412
// Calculate the offset parent
413
- $this . parents ( ) . each ( function ( ) {
413
+ $this . parents ( ) . each ( function ( ) {
414
414
var $this = $ ( this ) ;
415
415
416
416
if ( $this . data ( 'stellar-offset-parent' ) === true ) {
437
437
verticalOffset : verticalOffset ,
438
438
startingValueLeft : backgroundPosition [ 0 ] ,
439
439
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 ) ) ,
442
442
startingPositionLeft : $this . position ( ) . left ,
443
443
startingPositionTop : $this . position ( ) . top ,
444
444
startingOffsetLeft : offsetLeft ,
445
445
startingOffsetTop : offsetTop ,
446
446
parentOffsetLeft : parentOffsetLeft ,
447
447
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' ) )
449
449
} ) ;
450
450
} ) ;
451
451
} ,
520
520
newOffsetTop ,
521
521
i ;
522
522
523
- //First check that the scroll position or container size has changed
523
+ // First check that the scroll position or container size has changed
524
524
if ( this . currentScrollLeft === scrollLeft && this . currentScrollTop === scrollTop && this . currentWidth === this . viewportWidth && this . currentHeight === this . viewportHeight ) {
525
525
return ;
526
526
} else {
530
530
this . currentHeight = this . viewportHeight ;
531
531
}
532
532
533
- //Reposition elements
533
+ // Reposition elements
534
534
for ( i = this . particles . length - 1 ; i >= 0 ; i -- ) {
535
535
particle = this . particles [ i ] ;
536
536
537
- fixedRatioOffset = particle . isFixed ? 1 : 0 ;
537
+ fixedRatioOffset = ( particle . isFixed ? 1 : 0 ) ;
538
538
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)
540
540
if ( this . options . horizontalScrolling ) {
541
541
newPositionLeft = ( scrollLeft + particle . horizontalOffset + this . viewportOffsetLeft + particle . startingPositionLeft - particle . startingOffsetLeft + particle . parentOffsetLeft ) * - ( particle . stellarRatio + fixedRatioOffset - 1 ) + particle . startingPositionLeft ;
542
542
newOffsetLeft = newPositionLeft - particle . startingPositionLeft + particle . startingOffsetLeft ;
553
553
newOffsetTop = particle . startingOffsetTop ;
554
554
}
555
555
556
- //Check visibility
556
+ // Check visibility
557
557
if ( this . options . hideDistantElements ) {
558
558
isVisibleHorizontal = ! this . options . horizontalScrolling || newOffsetLeft + particle . width > ( particle . isFixed ? 0 : scrollLeft ) && newOffsetLeft < ( particle . isFixed ? 0 : scrollLeft ) + this . viewportWidth + this . viewportOffsetLeft ;
559
559
isVisibleVertical = ! this . options . verticalScrolling || newOffsetTop + particle . height > ( particle . isFixed ? 0 : scrollTop ) && newOffsetTop < ( particle . isFixed ? 0 : scrollTop ) + this . viewportHeight + this . viewportOffsetTop ;
574
574
}
575
575
}
576
576
577
- //Reposition backgrounds
577
+ // Reposition backgrounds
578
578
for ( i = this . backgrounds . length - 1 ; i >= 0 ; i -- ) {
579
579
background = this . backgrounds [ i ] ;
580
580
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 ) ;
584
584
585
585
setBackgroundPosition ( background . $element , bgLeft , bgTop ) ;
586
586
}
607
607
_startAnimationLoop : function ( ) {
608
608
var self = this ;
609
609
610
- this . _animationLoop = function ( ) {
610
+ this . _animationLoop = function ( ) {
611
611
requestAnimFrame ( self . _animationLoop ) ;
612
612
self . _repositionElements ( ) ;
613
613
} ;
641
641
return $window . stellar . apply ( $window , Array . prototype . slice . call ( arguments , 0 ) ) ;
642
642
} ;
643
643
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
645
645
$ [ pluginName ] . scrollProperty = scrollProperty ;
646
646
$ [ pluginName ] . positionProperty = positionProperty ;
647
647
648
- //Expose the plugin class so it can be modified
648
+ // Expose the plugin class so it can be modified
649
649
window . Stellar = Plugin ;
650
- } ( jQuery , window , document ) ) ;
650
+ } ( jQuery , this , document ) ) ;
0 commit comments