willmcpo / body-scroll-lock

Body scroll locking that just works with everything 😏
MIT License
4.04k stars 337 forks source link

Added optional callback to apply changes when scroll is hidden/shown #131

Open smolnikov opened 5 years ago

smolnikov commented 5 years ago

Callback (optional) is called after body element padding is set when overflow: hidden is applied if reserveScrollBarGap options is set to true. Callback receives scroll bar width as an argument might be usefull to apply the same padding as for body element to elements that have position: fixed.

const options: BodyScrollOptions = {
    reserveScrollBarGap: true
}

disableBodyScroll(targetElement, options, (scrollbarGapSize) => {
  document.getElementById('fixed').style.paddingRight = scrollbarGapSize + 'px';
});

enableBodyScroll(targetElement, () => {
  document.getElementById('fixed').style.paddingRight = 0;
});
smolnikov commented 5 years ago

This also might help with #95 .

smolnikov commented 5 years ago

@willmcpo could you review this PR? Does it makes sense from your perspective?

Tomas2D commented 4 years ago

Please merge