All files / src/helpers scrollLock.ts

55.56% Statements 10/18
37.5% Branches 3/8
50% Functions 1/2
55.56% Lines 10/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 245x                         5x 11x 11x 11x 11x 11x 11x 11x 11x    
export const scrollLock = () => {
  const body = document.querySelector('body');
  if (body) {
    const hasScrollbar = body.scrollHeight > window.innerHeight;
    body.style.top = `-${window.scrollY}px`;
    body.style.position = `fixed`;
    if (hasScrollbar) {
      body.style.overflowY = `scroll`;
    }
    body.style.width = `100%`;
  }
};
 
export const scrollUnlock = () => {
  const body = document.querySelector('body');
  Eif (body) {
    const scrollY = parseInt(body.style.top || '0');
    body.style.position = ``;
    body.style.overflowY = ``;
    body.style.width = ``;
    body.style.top = ``;
    window.scrollTo(0, scrollY * -1);
  }
};