v3.3.4: Affix plugin has problems handling items with offsetTop = 0
Created by: rafalp
Background
I have custom navbar in my app. Depending on configuration this navbar is either located at top of page, or its below user set header.
Issue
If there's element taking height above affixed one, everything works, however if affix element is at top of page, click events will switch it between affix/affix-top.
This is because if affix isn't affixed and its offsetTop
is 0, following line in getState will change it to 'top':
if (offsetTop != null && scrollTop <= offsetTop) return 'top'
Next click event will trigger getState again, this time changing state back off:
if (offsetTop != null && this.affixed == 'top') return scrollTop < offsetTop ? 'top' : false
Temporary fix
I have managed to fix this issue, by changing offsetTop to 1 if navbar has no element obove it (and making existing logic work).
Proposed fix
because offsetTop == 0 is edge case, getState
may check for it via running following check before all other checks:
if (offsetTop === 0) return scrollTop === 0 ? 'top' : false