how to prevent scrolling of page behind fancybox-2

前端 未结 9 1932
北恋
北恋 2020-12-16 14:44

We are using fancybox2 for displaying images. Everything works well, but when the larger image is displayed in the fancybox, the page behind scrolls to the top. After closin

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 14:47

    This is working fine for me:

    Add follow functions in facnybox initialization

    beforeShow: function(){
        $("body").css({'overflow-y':'hidden'});
    },
    afterClose: function(){
        $("body").css({'overflow-y':'visible'});
    }
    

    Example:

    $(".fancyBoxTrigger").fancybox({
        maxWidth    : 820,
        maxHeight   : 670,
        fitToView   : false,
        width       : 760,
        height      : 580,
        autoSize    : false,
        closeClick  : false,
        openEffect  : 'none',
        closeEffect : 'none',
        padding     : 10,
        closeBtn    : false,
        beforeShow: function(){
            $("body").css({'overflow-y':'hidden'});
        },
        afterClose: function(){
            $("body").css({'overflow-y':'visible'});
        },
        helpers : {
            overlay : {
                opacity: 0.4,
                locked: false
            }
        }
    });
    

    I hope it will work for you.

提交回复
热议问题