How to disable scrolling temporarily?

前端 未结 30 2541
萌比男神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条回答
  •  醉酒成梦
    2020-11-21 05:24

    Another solution:

    body {
        overflow-y: scroll;
        width: 100%;
        margin: 0 auto;
    }
    

    This way you always have a vertical scrollbar, but as most of my content is longer than the viewport, this is ok for me. Content is centered with a separate div, but without setting margin again in body my content would stay at the left.

    These are the two function I use to show my popup/modal:

    var popup_bodyTop = 0;
    var popup_bodyLeft = 0;
    
    function popupShow(id)
    {
        $('#'+ id).effect('fade');
        $('#popup-overlay').effect('fade');
    
        // remember current scroll-position
        // because when setting/unsetting position: fixed to body
        // the body would scroll to 0,0
        popup_bodyLeft = $(document).scrollLeft();
        popup_bodyTop  = $(document).scrollTop();
    
        // invert position
        var x = - popup_bodyLeft;
        var y = - popup_bodyTop;
    
        $('body').css('position', 'fixed');
        $('body').css('top', y.toString() +'px');
        $('body').css('left', x.toString() +'px');
    }
    
    function popupHide(id)
    {
        $('#'+ id).effect('fade');
        $('#popup-overlay').effect('fade');
        $('body').css('position', '');
        $('html, body').scrollTop(popup_bodyTop);
        $('html, body').scrollLeft(popup_bodyLeft);
    }
    

    Result: non scrollable background and no re-positioning of the content because of the left scrollbar. Tested with current FF, Chrome and IE 10.

提交回复
热议问题