1

I would like to disable the scroll when the 'Menu icon' of my page is clicked, and then enable the scroll again when the menu is clicked again to close.

I'm trying something like this:

$(document).ready(function(){

  $(".menu").click(function (e) {
    $(".menucontent").show();
    if ( $(".menucontent").is(":visible")) {
      $('html, body').css({ overflow: 'hidden', height: '100%'});
    }else{
      $('html, body').css({ overflow: 'auto', height: 'auto'});      
    }

  });
  });

but I need some help because I'm kind of newbie on this language

5
  • Check this Commented May 14, 2017 at 20:30
  • How does it fail? Are you getting errors in the console? Commented May 14, 2017 at 20:35
  • it actually disable the scrolll when menu's content is visible, but it doesn't work when I click again the menu button, the scroll would be enable again, but it doesn't result Commented May 14, 2017 at 20:53
  • What you've provided works as expected codepen.io/anon/pen/gWKrYd if it isn't doing what you want, try describing your end goal another way. Commented May 14, 2017 at 20:56
  • maybe I'm not explaining myself well... What I want to do is that after doing anything, the scroll of the page works as usual, but when I click on the menu button, the menu's content is showed and the scroll gets desabled, so I can't move anything untill I close the menu and then the content is hided, and the scroll goes back to the initial status, wich means enable again. Commented May 14, 2017 at 21:06

1 Answer 1

1

you should put toggle() instead of show

toggle: Display or hide the matched elements.

$(document).ready(function(){
  $(".menu").click(function (e) {
    $(".menucontent").toggle();
      if ( $(".menucontent").is(":visible")) {
        $('html, body').css({ overflow: 'hidden', height: '100%'});
      }else{
        $('html, body').css({ overflow: 'auto', height: 'auto'});      
      }
    });
  });
Sign up to request clarification or add additional context in comments.

1 Comment

you guys don't get it yet u.u . The poblem is not about how I show or not the content, the problem is the scroll... I want the scroll of the page to be enable before showing the content and disable while is visible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.