Skip to content

Commit 597a799

Browse files
committed
fix(*): set will update only if component is near to or within the viewport
Issue: #20
1 parent ee6cb0f commit 597a799

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/Sticky.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class Sticky extends React.PureComponent<IProps, IState> {
6262
return Boolean(this.props.container);
6363
};
6464

65+
isNearToViewport = (rect: IRect): boolean => {
66+
const padding = 300;
67+
return rect.top - padding < 0 && rect.bottom + padding > 0;
68+
};
69+
6570
isSticky = (rect: IRect, containerRect: IRect) => {
6671
if (!this.hasContainer()) {
6772
return containerRect.top <= this.offsetTop;
@@ -139,7 +144,7 @@ class Sticky extends React.PureComponent<IProps, IState> {
139144

140145
if (!this.props.disableHardwareAcceleration) {
141146
styles.transform = `translateZ(0)`;
142-
styles.willChange = 'position, top';
147+
styles.willChange = this.isNearToViewport(rect) ? 'position, top' : null;
143148
}
144149

145150
return styles;

lib/StickyScrollUp.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,21 @@ class StickyScrollUp extends React.PureComponent<IProps, IState> {
114114
}
115115
}
116116

117+
isNearToViewport = (rect: IRect): boolean => {
118+
const padding = 300;
119+
return rect.top - padding < 0;
120+
};
121+
117122
getStickyStyles(stickyRect: IRect, scroll: IScroll) {
118123
const styles = calcPositionStyles(stickyRect, scroll, {
119124
offsetTop: this.props.defaultOffsetTop,
120125
});
121126

122127
if (!this.props.disableHardwareAcceleration) {
123128
styles.transform = `translateZ(0)`;
124-
styles.willChange = 'position, top';
129+
styles.willChange = this.isNearToViewport(stickyRect)
130+
? 'position, top'
131+
: null;
125132
}
126133

127134
return styles;

0 commit comments

Comments
 (0)