How to disable scrolling temporarily?

前端 未结 30 3299
萌比男神i
萌比男神i 2020-11-21 05:16

I\'m using the scrollTo jQuery plugin and would like to know if it is somehow possible to temporarily disable scrolling on the window element through Javascript? The reason

30条回答
  •  萌比男神i
    2020-11-21 05:26

    To prevent the jump, this is what I used

    export function toggleBodyScroll(disable) {
      if (!window.tempScrollTop) {
        window.tempScrollTop = window.pageYOffset; 
        // save the current position in a global variable so I can access again later
    
      }
      if (disable) {
        document.body.classList.add('disable-scroll');
        document.body.style.top = `-${window.tempScrollTop}px`;
      } else {
        document.body.classList.remove('disable-scroll');
        document.body.style.top = `0px`;
        window.scrollTo({top: window.tempScrollTop});
        window.tempScrollTop = 0;
      }
    }
    
    

    and in my css

    .disable-scroll {
      height: 100%;
      overflow: hidden;
      width: 100%;
      position: fixed;
    }
    
    

提交回复
热议问题