how to prevent scrolling of page behind fancybox-2

2019-02-08 09:04发布

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 closing the fancybox the user has to scroll back downwards to the position, where he opened the box. The examples on the fancybox2 Site do not show this behaviour. I could not find out, where is the difference to make this happen.

fancyOptions = {
  type: 'image',
  closeBtn: false,
  autoSize: false,
  scrolling: 'no',
  type: 'image',
  padding: [10,10,10,10],  
  beforeLoad: function(){
    this.title = getTitle(this);
    this.href = $(this.element).closest('a').attr('href');
  },
  helpers: {
    overlay: {
      css: {
        'background': 'rgba(0, 0, 0, 0.7)'
      },
    },
    title: {
      type: 'inside'
    }
  }
};

We are using fancybox2 as a module within require.js. The .fancybox() call is in a $(document).ready block.

There where 2 scrollbars and I hid one with css

.fancybox-overlay {
overflow: hidden !important;
}

9条回答
2楼-- · 2019-02-08 09:42

My modification, this help me

/* Fancybox */
$('.fancybox').fancybox({
    beforeShow: function(){
        $("body").css({
            "overflow-y": "hidden",
            "position": "fixed"
            }
        );
    },
    afterClose: function(){
        $("body").removeAttr("style");
    }
});
查看更多
We Are One
3楼-- · 2019-02-08 09:45
$('.fancybox').fancybox({
 'beforeLoad': function(){
   disable_scroll();
    },
 'afterClose': function(){
   enable_scroll();
  }
});

var disabled_scroll = false;

function disable_scroll() {
  disabled_scroll = true;
}

function enable_scroll() {
  disabled_scroll = false;
}

in fullPage.js

function scrollPage(element, callback, isMovementUp)
{
   if(disabled_scroll) return;
   .....
}
查看更多
时光不老,我们不散
4楼-- · 2019-02-08 09:46

In jquery.fancybox.css -> .fancybox-lock
change:
overflow: hidden !important;
to:
overflow: visible !important;
works for me :)

查看更多
登录 后发表回答