Skip to content

Commit 18bd470

Browse files
author
Oleksii Korshenko
authored
MAGETWO-66300: [GitHub][PR] Stickyjs improvements #7139
2 parents ce3eab6 + ef1088f commit 18bd470

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

lib/web/mage/sticky.js

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,48 @@ define([
1111

1212
$.widget('mage.sticky', {
1313
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;
1556
},
1657

1758
/**
@@ -35,14 +76,29 @@ define([
3576
*/
3677
_stick: function () {
3778
var offset,
38-
isStatic;
79+
isStatic,
80+
stuck,
81+
stickAfter;
3982

4083
isStatic = this.element.css('position') === 'static';
4184

4285
if (!isStatic && this.element.is(':visible')) {
43-
offset = $(document).scrollTop() - this.parentOffset;
86+
offset = $(document).scrollTop() -
87+
this.parentOffset +
88+
this._getOptionValue('spacingTop');
89+
4490
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);
46102
}
47103
},
48104

0 commit comments

Comments
 (0)