@@ -11,7 +11,48 @@ define([
11
11
12
12
$ . widget ( 'mage.sticky' , {
13
13
options : {
14
- container : ''
14
+ /**
15
+ * Element selector, who's height will be used to restrict the
16
+ * maximum offsetTop position of the stuck element.
17
+ * Default uses document body.
18
+ * @type {String }
19
+ */
20
+ container : '' ,
21
+
22
+ /**
23
+ * Spacing in pixels above the stuck element
24
+ * @type {Number|Function } Number or Function that will return a Number
25
+ */
26
+ spacingTop : 0 ,
27
+
28
+ /**
29
+ * Allows postponing sticking, until element will go out of the
30
+ * screen for the number of pixels.
31
+ * @type {Number|Function } Number or Function that will return a Number
32
+ */
33
+ stickAfter : 0 ,
34
+
35
+ /**
36
+ * CSS class for active sticky state
37
+ * @type {String }
38
+ */
39
+ stickyClass : '_sticky'
40
+ } ,
41
+
42
+ /**
43
+ * Retrieve option value
44
+ * @param {String } option
45
+ * @return {* }
46
+ * @private
47
+ */
48
+ _getOptionValue : function ( option ) {
49
+ var value = this . options [ option ] || 0 ;
50
+
51
+ if ( typeof value === 'function' ) {
52
+ value = this . options [ option ] ( ) ;
53
+ }
54
+
55
+ return value ;
15
56
} ,
16
57
17
58
/**
@@ -35,14 +76,29 @@ define([
35
76
*/
36
77
_stick : function ( ) {
37
78
var offset ,
38
- isStatic ;
79
+ isStatic ,
80
+ stuck ,
81
+ stickAfter ;
39
82
40
83
isStatic = this . element . css ( 'position' ) === 'static' ;
41
84
42
85
if ( ! isStatic && this . element . is ( ':visible' ) ) {
43
- offset = $ ( document ) . scrollTop ( ) - this . parentOffset ;
86
+ offset = $ ( document ) . scrollTop ( ) -
87
+ this . parentOffset +
88
+ this . _getOptionValue ( 'spacingTop' ) ;
89
+
44
90
offset = Math . max ( 0 , Math . min ( offset , this . maxOffset ) ) ;
45
- this . element . css ( 'top' , offset ) ;
91
+
92
+ stuck = this . element . hasClass ( this . options . stickyClass ) ;
93
+ stickAfter = this . _getOptionValue ( 'stickAfter' ) ;
94
+
95
+ if ( offset && ! stuck && offset < stickAfter ) {
96
+ offset = 0 ;
97
+ }
98
+
99
+ this . element
100
+ . toggleClass ( this . options . stickyClass , offset > 0 )
101
+ . css ( 'top' , offset ) ;
46
102
}
47
103
} ,
48
104
0 commit comments