阻止 window 滚动然后在单击按钮后解除绑定 - Jquery

Prevent window scrolling and then unbind after button click - Jquery

我试图阻止滚动我的网站,直到单击按钮然后允许滚动。我可以成功地阻止滚动,但我无法取消绑定此功能。这是我的代码:

提前致谢!

 $('body').addClass('noscroll');

 $('.fold-trigger').click(function(event) {
   $('body').removeClass('noscroll')
   console.log('removed');
 });


 if ($('body').hasClass('noscroll')){
  $(window).bind('scroll', function(){
    $('body').on({
    'mousewheel': function(e) {
        if (e.target.id == 'el') return;
        e.preventDefault();
        e.stopPropagation();
      }
    })
  }); 
 } else {
  $('body').on({
  'mousewheel': function(e) {
      if (e.target.id == 'el') return;
      e.preventDefault(false);
      e.stopPropagation(false);
    }
  })
 }
}

如果你想在 body 上切换这个 class,你应该改变你的逻辑。

$('body').on('mousewheel', function(e) {
  if ($('body').hasClass('noscroll')) {
    e.preventDefault();
    e.stopPropagation();
  }
});

然后随时添加和删除 noscroll class。