Skip to content

Commit 6cc7913

Browse files
author
Oleg Korsunsky
committed
Fix the behavior of forceSticky when some of the stickies aren’t in their initial position
1 parent 1fee1da commit 6cc7913

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/stickyfill.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class Sticky {
103103
*/
104104
const nodeComputedStyle = getComputedStyle(node);
105105
const nodeComputedProps = {
106+
position: nodeComputedStyle.position,
106107
top: nodeComputedStyle.top,
107108
display: nodeComputedStyle.display,
108109
marginTop: nodeComputedStyle.marginTop,
@@ -124,7 +125,16 @@ class Sticky {
124125
this._active = true;
125126

126127
/*
127-
* 3. Get necessary node parameters
128+
* 3. Check if the current node position is `sticky`. If it is, it means that the browser supports sticky positioning,
129+
* but the polyfill was force-enabled. We set the node’s position to `static` before continuing, so that the node
130+
* is in it’s initial position when we gather its params.
131+
*/
132+
const originalPosition = node.style.position;
133+
if (nodeComputedStyle.position == 'sticky' || nodeComputedStyle.position == '-webkit-sticky')
134+
node.style.position = 'static';
135+
136+
/*
137+
* 4. Get necessary node parameters
128138
*/
129139
const referenceNode = node.parentNode;
130140
const parentNode = shadowRootExists && referenceNode instanceof ShadowRoot? referenceNode.host: referenceNode;
@@ -149,7 +159,7 @@ class Sticky {
149159
right: -nodeWinOffset.right + parentWinOffset.right - parseNumeric(parentComputedStyle.borderRightWidth)
150160
};
151161
this._styles = {
152-
position: node.style.position,
162+
position: originalPosition,
153163
top: node.style.top,
154164
bottom: node.style.bottom,
155165
left: node.style.left,
@@ -169,7 +179,7 @@ class Sticky {
169179
};
170180

171181
/*
172-
* 4. Ensure that the node will be positioned relatively to the parent node
182+
* 5. Ensure that the node will be positioned relatively to the parent node
173183
*/
174184
const parentPosition = parentComputedStyle.position;
175185

@@ -181,13 +191,13 @@ class Sticky {
181191
}
182192

183193
/*
184-
* 5. Recalc node position.
194+
* 6. Recalc node position.
185195
* It’s important to do this before clone injection to avoid scrolling bug in Chrome.
186196
*/
187197
this._recalcPosition();
188198

189199
/*
190-
* 6. Create a clone
200+
* 7. Create a clone
191201
*/
192202
const clone = this._clone = {};
193203
clone.node = document.createElement('div');

0 commit comments

Comments
 (0)