Skip to content

Stickyjs improvements #7139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions lib/web/mage/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,48 @@ define([

$.widget('mage.sticky', {
options: {
container: ''
/**
* Element selector, who's height will be used to restrict the
* maximum offsetTop position of the stuck element.
* Default uses document body.
* @type {String}
*/
container: '',

/**
* Spacing in pixels above the stuck element
* @type {Number|Function} Number or Function that will return a Number
*/
spacingTop: 0,

/**
* Allows postponing sticking, until element will go out of the
* screen for the number of pixels.
* @type {Number|Function} Number or Function that will return a Number
*/
stickAfter: 0,

/**
* CSS class for active sticky state
* @type {String}
*/
stickyClass: '_sticky'
},

/**
* Retrieve option value
* @param {String} option
* @return {*}
* @private
*/
_getOptionValue: function (option) {
var value = this.options[option] || 0;

if (typeof value === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you check value on function type? In what case it can be function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be useful to set a function for stickAfter parameter that will calculate sticky element height when you wish to stick element after it goes out of the viewport and the element could change its height dynamically (shopping cart for example)
You can use different spacing top value for different screen size/orientation also.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but can you leave docblock with description. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

value = this.options[option]();
}

return value;
},

/**
Expand All @@ -35,14 +76,29 @@ define([
*/
_stick: function () {
var offset,
isStatic;
isStatic,
stuck,
stickAfter;

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

if (!isStatic && this.element.is(':visible')) {
offset = $(document).scrollTop() - this.parentOffset;
offset = $(document).scrollTop() -
this.parentOffset +
this._getOptionValue('spacingTop');

offset = Math.max(0, Math.min(offset, this.maxOffset));
this.element.css('top', offset);

stuck = this.element.hasClass(this.options.stickyClass);
stickAfter = this._getOptionValue('stickAfter');

if (offset && !stuck && offset < stickAfter) {
offset = 0;
}

this.element
.toggleClass(this.options.stickyClass, offset > 0)
.css('top', offset);
}
},

Expand Down