-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Stickyjs improvements #7139
Stickyjs improvements #7139
Changes from 5 commits
8bfa048
996d213
a7254cf
798469a
9f926ef
c6d309b
766292c
0e479a3
ae7f764
dde585c
5071e54
e6795cb
7ae6be0
db648a6
fc0ae57
febbd88
d715b42
62b822c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,10 @@ define([ | |
|
||
$.widget('mage.sticky', { | ||
options: { | ||
container: '' | ||
container: '', | ||
spacingTop: 0, | ||
offsetTop: 0, | ||
stickyClass: '_sticky' | ||
}, | ||
|
||
/** | ||
|
@@ -41,8 +44,34 @@ define([ | |
|
||
if (!isStatic && this.element.is(':visible')) { | ||
offset = $(document).scrollTop() - this.parentOffset; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this line of code? Variable offset will be redefined in any case in line 49 or 51. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I'm missing the point, but it's not redefined at those lines, it just modified with the possible There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, can you comment on this? I don't understand how you'd like to modify the source. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good. Thank you |
||
|
||
if (typeof this.options.spacingTop === 'function') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should check to see whether var spacingTop = this.options.spacingTop || 0;
if (typeof this.options.spacingTop === 'function') {
spacingTop = this.options.spacingTop();
}
spacingTop = Number(spacingTop);
if (isNaN(spacingTop)) {
throw new Error('sticky: spacingTop must be a Number');
}
offset += this.options.spacingTop; See below for more notes. |
||
offset += this.options.spacingTop(); | ||
} else { | ||
offset += this.options.spacingTop; | ||
} | ||
|
||
offset = Math.max(0, Math.min(offset, this.maxOffset)); | ||
this.element.css('top', offset); | ||
|
||
if (offset && | ||
this.options.offsetTop && | ||
!this.element.hasClass(this.options.stickyClass)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain the desired logic here? It's hard to tell from the code what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic inside of this In other words, I check if an element should be stuck (offset > 0), |
||
|
||
var offsetTop = 0; | ||
if (typeof this.options.offsetTop === 'function') { | ||
offsetTop = this.options.offsetTop(); | ||
} else { | ||
offsetTop = this.options.offsetTop; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern happens twice, and there may be further configurable numbers, so it probably should be separated into a private function. function optionToNumber(self, option) {
var value = self.options[option] || 0;
if (typeof value === 'function') {
value = self.options[option]();
}
var converted = Number(value);
if (isNaN(converted)) {
throw Error('sticky: Could not convert supplied option "' + option + '" to Number from "' + value + '"');
}
return converted;
} And then here, you can just have: var offsetTop = optionToNumber(this, 'offsetTop');
if (offset < offsetTop) {
offset = 0;
} And above, you can just have: offset += optionToNumber(this, 'spacingTop'); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice optimization, but is it ok to send There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's okay to send |
||
|
||
if (offset < this.options.offsetTop) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is a bug; |
||
offset = 0; | ||
} | ||
} | ||
|
||
this.element | ||
.toggleClass(this.options.stickyClass, (offset > 0)) | ||
.css('top', offset); | ||
} | ||
}, | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have been provide new settings properties that by default is Number, no reason to check it on another types. User can't change this property, only developer, when developer will be configuring sticky widget he can look at type of parameter. Also,
offsetTop
it's native property of HTMLElement would be nice to change it to another, e.g.blockOffsetTop
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still thinking about a new name for it 😫
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you say to rename it to
stickAfter
ortriggerOffset
orstickOffset
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've picked a
stickAfter
. It seems that it better and self-explanatory name.