Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll lock for body while mobile menu is open is not working

I have found several other solutions that demonstrate how you can lock scroll behaviour for a website by using CSS overflow property. As such I have implemented this solution and added the overflow: hidden; to the body tag when the menu is open. However when using iOS Safari or Chrome the body is still scrollable.

CSS:

    body.opened-drawer {
      overflow: hidden !important;
      height: 100% !important;
      width: 100% !important;
      position: fixed !important;
      z-index: 0 !important;
    }

JS:

timber.openDrawerMenu = function () {
  var $mobileMenu = $('.nav-bar'),
      $mobileMenuButton = $('#menu-opener'),
      $body = $('body');
      $mobileMenuButton.addClass('opened');
      $mobileMenu.addClass('opened');
      $body.addClass('opened-drawer');

  // Make drawer a11y accessible
  timber.cache.$navBar.attr('aria-hidden', 'false');

  // Set focus on drawer
  timber.trapFocus({
     $container: timber.cache.$navBar,
     namespace: 'drawer_focus'
  });

  // Escape key closes menu
  timber.cache.$html.on('keyup.drawerMenu', function(evt) {
    if (evt.keyCode == 27) {
      timber.closeDrawerMenu();
    }
  });
}

timber.closeDrawerMenu = function () {

  var $mobileMenu = $('.nav-bar'),
      $mobileMenuButton = $('#menu-opener'),
      $body = $('body');

  $mobileMenuButton.removeClass('opened');
  $mobileMenu.removeClass('opened');
  $body.removeClass('opened-drawer');

  // Make drawer a11y unaccessible
  timber.cache.$navBar.attr('aria-hidden', 'true');

  // Remove focus on drawer
  timber.removeTrapFocus({
    $container: timber.cache.$navBar,
    namespace: 'drawer_focus'
  });

  timber.cache.$html.off('keyup.drawerMenu');
}
like image 624
JSON_Derulo Avatar asked Jun 30 '17 15:06

JSON_Derulo


People also ask

How do you stop your body from scrolling when the menu is open?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.

How do I lock my body scroll?

ontouchmove = (e) => { e. preventDefault(); return false; }; locks the body scroll, but ALSO locks the scroll of a target element (eg. modal).

How do I stop my screen from scrolling CSS?

To hide the scrollbar and disable scrolling, we can use the CSS overflow property. This property determines what to do with content that extends beyond the boundaries of its container. To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element.


2 Answers

Here you go for the quick fix

body.opened-drawer {
    overflow: hidden;
    height: 100%;
    width: 100%;
    position: fixed;
    z-index: 0;
}

Please find the modified script here

function menuDrawerButtons (){

    cache.$mobileMenuButton.on('click', function(e) {
         e.preventDefault();
        if ($(this).hasClass('opened')) {
            timber.closeDrawerMenu()
        } else {
            timber.openDrawerMenu();
        }           
    });

    setTimeout(function() {
        cache.$mobileMenu.addClass('animate');
    }, 500);
}
like image 183
Karthik Avatar answered Sep 22 '22 19:09

Karthik


The solution I came to is more of a hack and less a solution but gets the job done either way. What I did was fix the position on the body when the menu was opened and calculate and set the scrollTop position as the menu was opened or closed.

jQuery:

    var tempScrollTop = null;

    tempScrollTop = $(window).scrollTop();

    $(window).scrollTop(tempScrollTop);

    var fixed = document.getElementById('fixed--nav');
       fixed.addEventListener('touchmove', function(e) {
       e.preventDefault();
    }, false);
like image 43
JSON_Derulo Avatar answered Sep 23 '22 19:09

JSON_Derulo