JavaScript fancyBox v2: How do I prevent background scrolling in Chrome?

I'm using fancyBox v2.1.4. In Chrome it's allowing scrolling of the main page when the fancyBox is open.

I'm utilizing the locked: true but that doesn't seem to solve the issue. I have also considered using e.preventDefault to disable certain scrolling abilities as another option:

$(document).ready(function() {
    $('.fancybox').fancybox({
        'closeClick': false,
        'scrolling': 'no',

        helpers: {
            overlay: {
                closeClick: false,
                locked: true
            }
        },

        beforeShow: function() {
            // considering some type of functionality here to prevent default
            // of mousewheel
        },
        afterClose: function() {
            // restore default action of mousewheel, although my initial attempts
            // at this did not work
        }
    });
});
Answer:1

this code worked for me:

<script type="text/javascript">
  $(document).ready(function() {
    $(".lightbox").fancybox({
      scrolling: "no",
      openEffect: "elastic",
      padding: 0,
      closeEffect: "elastic",
        afterShow: function(){
            document.ontouchstart = function(e){
                //prevent scrolling
                e.preventDefault();
            }
        },
        afterClose: function(){
            document.ontouchstart = function(e){
                //default scroll behaviour
            }
        },
      helpers: {
          overlay: {
              locked: true
          }
      }
    });
  });
</script>
Answer:2

I've got a possible solution to your (and also my) problem. Before this, I was able to scroll the page behind the fancyBox on Android devices in the default browser. With this little 'hack' the user can't scroll the background page anymore, but it's a little bit glitchy though because of the speed the browser catches 'scroll' events.

UPDATE: On computer browsers this fix works like a charm. It's only the Android browser that glitches.

jQuery(document).ready(function($) {    
    var scrolltop, htmlOrBody;

    var antiscrollevent = function(e) {
        e.preventDefault();
        $(htmlOrBody).scrollTop(scrolltop);
    };

    // links met een popover
    $("a.popup").fancybox({
        type: 'ajax',

        beforeShow: function() {
            // considering some type of functionality here to prevent default of
            // mousewheel
            htmlOrBody = $('body').scrollTop() != 0 ? 'body' : 'html';
            scrolltop = $(htmlOrBody).scrollTop();
            $(window).on('scroll', antiscrollevent);
        },
        afterClose: function() {
            // restore default action of mousewheel, although my initial attempts
            // at this did not work
            $(window).off('scroll', antiscrollevent);
        }
    });
});
Answer:3

Many of the things I tried ended up disabling all interaction on mobile, making the FancyBox impossible to close.

I ended up overriding jQuery's scroll event on afterShow. This preserved interaction with FancyBox content while disabling scrolling of the body. Tested on Android Browser and Mobile Safari:

afterShow: function(){
            $(document).on('scroll','body',function(e){
                e.preventDefault();
            });
        }

Reset this on afterClose by repeating the scroll function without e.preventDefault();

Answer:4

You can use the scrollOutside setting:

.fancybox({
  scrolling: "no",
  scrollOutside: "false"
});

the scrolling setting prevents the vertical scrollbar to show.

Answer:5

I'm using this code to prevent people from puting spaces between alphabets and numbers. I would lilke to modify the code so that only alphabets, letters and numbers can be typed. This is what i'm ...

I'm using this code to prevent people from puting spaces between alphabets and numbers. I would lilke to modify the code so that only alphabets, letters and numbers can be typed. This is what i'm ...

  1. regex javascript allow spaces
  2. regex javascript allow special characters
  3. regex javascript allow only numbers
  4. javascript regex allow only alphanumeric and spaces
  5. javascript regex allow only numbers and dot
  6. javascript regex allow only one dot
  7. javascript regex allow empty string
  8. javascript regex allow only alphanumeric
  9. javascript regex allow dash
  10. javascript regex allow only letters and numbers
  11. javascript regex allow numbers and decimals only
  12. javascript regex allow numbers and dashes
  13. javascript regex allow whitespace
  14. javascript regex allow negative numbers
  15. javascript regex allow only letters
  16. javascript regex allow hyphen
  17. javascript regex allow some special characters
  18. javascript regex allow only one space
  19. javascript regex allow backslash
  20. javascript regex allow parentheses

I'm trying to submit a form with AJAX, the form being content of jQuery UI Dialog. I use the same code as in jQuery's documentation but I modified the form so there is only one text input, so the ...

I'm trying to submit a form with AJAX, the form being content of jQuery UI Dialog. I use the same code as in jQuery's documentation but I modified the form so there is only one text input, so the ...

In ECMAScript 6, I have two way to define a dictionary: Traditional method (with dictionary = Object.create(null)): set property: dictionary.key = value or dictionary[key] = value get property: ...

In ECMAScript 6, I have two way to define a dictionary: Traditional method (with dictionary = Object.create(null)): set property: dictionary.key = value or dictionary[key] = value get property: ...

I like the off canvas feature that bootstrap 3 has: http://getbootstrap.com/examples/offcanvas/. However, for one of my projects i'd like it to overlap the content rather than move the content to the ...

I like the off canvas feature that bootstrap 3 has: http://getbootstrap.com/examples/offcanvas/. However, for one of my projects i'd like it to overlap the content rather than move the content to the ...