ScrollFreezer
Frost scroll event on window. A no-jumping javascript alternative to overflow:hidden.
Why
What's the probelem with overflow:hidden
? Well if people have their browsers
defaulting to show scrollbars (not overlayed fancy ghost ones like mac/safari)
content will jump to right when overflow is applied. It's kind of ugly/janky.
Public Methods
// assuming an instanceconst myScrollFreezer = 'body';
Method | Description |
---|---|
myScrollFreezer.freeze() |
Locks scroll. Sets isFrozen=true |
myScrollFreezer.defrost() |
Unlocks scroll. Sets isFrozen=false |
myScrollFreezer.toggleFreeze() |
toggle state between frozeen and defrosted |
myScrollFreezer.getState() |
returns an state Object . {isFrozen: bool, curScrollLockPos: number} |
Usage
// Instanciatevar bodyScrollFreezer = 'body'; // starts at falsedocument; /* * Manual verfication Example */var isFrozen = bodyScrollFreezerisFrozen; if isFrozen bodyScrollFreezer else bodyScrollFreezer
Credits
Technique originally seen at facebook, at least that's what @fhilM said.
Based on philm SO answer 🙌