简体   繁体   English

如何通过单击与jQuery相同的元素来启用和禁用滚动?

[英]how can I enable and disable scroll by clicking the same element with jquery?

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 但我需要一些帮助,因为我是这种语言的新手

you should put toggle() instead of show 你应该把toggle()代替show

toggle : Display or hide the matched elements. toggle :显示或隐藏匹配的元素。

$(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'});      
      }
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM