jQuery simplemodal disable scrolling

后端 未结 6 2124
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 09:01

I have more than 2000 pixels scrolling content on a page.

If the user clicks a div a scrolling content pops up in a simplemodal window. Now my client wants

相关标签:
6条回答
  • 2021-02-04 09:38

    Turning the scrollbars on and off will cause the content to shift and the overlay will no longer cover the whole window. Here's how to fix it.

    var oldBodyMarginRight = $("body").css("margin-right");
    $.modal(iframe, {
        onShow: function () {
            // Turn off scroll bars to prevent the scroll wheel from affecting the main page.  Make sure turning off the scrollbars doesn't shift the position of the content.
            // This solution works Chrome 12, Firefox 4, IE 7/8/9, and Safari 5.
            // It turns off the scroll bars, but doesn't prevent the scrolling, in Opera 11 and Safari 5.
            var body = $("body");
            var html = $("html");
            var oldBodyOuterWidth = body.outerWidth(true);
            var oldScrollTop = html.scrollTop();
            var newBodyOuterWidth;
            $("html").css("overflow-y", "hidden");
            newBodyOuterWidth = $("body").outerWidth(true);
            body.css("margin-right", (newBodyOuterWidth - oldBodyOuterWidth + parseInt(oldBodyMarginRight)) + "px");
            html.scrollTop(oldScrollTop); // necessary for Firefox
            $("#simplemodal-overlay").css("width", newBodyOuterWidth + "px")
        },
        onClose: function () {
            var html = $("html");
            var oldScrollTop = html.scrollTop(); // necessary for Firefox.
            html.css("overflow-y", "").scrollTop(oldScrollTop);
            $("body").css("margin-right", oldBodyMarginRight);
            $.modal.close();
        }
    });
    
    0 讨论(0)
  • 2021-02-04 09:38

    I found overflow:hidden not so nice since it hides the content behind the semi-transparant overlay if the page is scrolled halfway.

    I came up with the following, rather elaborate solution. It disables scrolling in all possible detectable ways I found. And puts the scrollposition straight back to the old position if the page was still scrolled somehow.

    var popupOpened = false;
    
    function openPopup()
    {
        popupOpened = true;
    
        //catch middle mouse click scrolling
        $(document).bind('mousedown',disableMiddleMouseButtonScrolling);
    
        //catch other kinds of scrolling
        $(document).bind('mousewheel DOMMouseScroll wheel',disableNormalScroll);
    
        //catch any other kind of scroll (though the event wont be canceled, the scrolling will be undone)
        //IE8 needs this to be 'window'!
        $(window).bind('scroll',disableNormalScroll);
    
        $("#layover").css({"opacity" : "0.7"}).fadeIn();
        $("#popup").fadeIn(300,function()
        {
            //use document here for crossbrowser scrolltop!
            oldScrollTop = $(document).scrollTop();
        });
    }
    
    function closePopup()
    {
        popupOpened = false;
        $("#layover").fadeOut();
        $("#popup").fadeOut(300,function(){
            $(document).unbind('mousedown',disableMiddleMouseButtonScrolling);
            $(document).unbind('mousewheel DOMMouseScroll wheel',disableNormalScroll);
            $(window).unbind('scroll',disableNormalScroll);
        });
    }
    
    function disableMiddleMouseButtonScrolling(e)
    {
        if(e.which == 2)
        {
            e.preventDefault();
        }
        return false;
    }
    
    function disableNormalScroll(e)
    {
        e.preventDefault();
        $('html, body').scrollTop(oldScrollTop);
        return false;
    }
    
    0 讨论(0)
  • 2021-02-04 09:40

    Use

    overflow:hidden
    

    Apply it to the page when modal dialog is opened and remove it when the dialog is destroyed. This will hide your scrollbar.

    0 讨论(0)
  • 2021-02-04 09:44

    In your script to open your modal:

    $("html,body").css("overflow","hidden");
    

    And on close:

    $("html,body").css("overflow","auto");
    

    (HTML and body are required as the scroll bars are attached to different parts of the browser depending on which you are using)

    0 讨论(0)
  • 2021-02-04 09:47

    In my case in tag <a> param href = "#". So solution be:

    href="javascript:void(0);"
    
    0 讨论(0)
  • 2021-02-04 10:00

    This option works like a charm:

    document.documentElement.style.overflow = 'hidden';
    
    0 讨论(0)
提交回复
热议问题