js/css : disable scrollbar but keep scroll position

后端 未结 6 487
情深已故
情深已故 2020-12-31 01:40

I\'m using the Jquery dialog to open a popup box window on top of a page. When I open the dialog box, I want the general page scrolling to be disabled. To do so, I am doing

6条回答
  •  灰色年华
    2020-12-31 02:14

    Remembering the offset should keep your popup at bay;

    JSfiddle

    HTML

    asd
    asd
    asd
    asd
    asd

    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd

    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd

    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd

    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd
    asd

    CSS

    html, body {
        margin: 0px;
    }
    
    #popupholder {
        width: 100%;
        height: 100%;
        position: absolute;
        display: box;
        display: -webkit-box;
        display: -moz-box;
        background-color: rgba(0,0,0,0.75);
        display: none;
    }
    
    #close {
        display: block;
        height: 20px;
        margin: 75px auto 0px auto;
    }
    

    JavaScript

    $(document).ready(function() {
        $('.open').click(function() {
            // Block scrolling
            $('body').css('overflow', 'hidden');
    
            // Show popup
            var offset = window.pageYOffset;
            //console.log(offset);
            $('#popupholder').css({
                'display': 'block',
                'top': offset + 'px'
            });
        });
    
        $('#close').click(function() {
            // Enable scrolling
            $('body').css('overflow', 'auto');
    
            // Hide popup
            $('#popupholder').css('display', 'none');
        });
    });
    

    For safety-reasons you could add a very high z-index to your #popupholder, but that's not really relevant to the question.

提交回复
热议问题