How to disable scrolling temporarily?

前端 未结 30 3292
萌比男神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:40

    I'm sorry to answer an old post but I was looking for a solution and came across this question.

    There are many workarounds for this issue to still display the scrollbar, like giving the container a 100% height and an overflow-y: scroll styling.

    In my case I just created a div with a scrollbar which I display while adding overflow: hidden to the body:

    function disableScroll() {
        document.getElementById('scrollbar').style.display = 'block';
        document.body.style.overflow = 'hidden';
    }
    

    The element scrollbar must have this styles:

    overflow-y: scroll; top: 0; right: 0; display: none; height: 100%; position: fixed;
    

    This shows a grey scrollbar, hope it helps future visitors.

提交回复
热议问题