单击按钮,html的溢出变为隐藏,如果再次单击,它将变回可见

Click on button, overflow-y of html turns hidden, if click again, it turns back visible

本文关键字:单击 隐藏 html 按钮 溢出 如果      更新时间:2023-09-26

我正在尝试做这件事:我希望,当单击按钮时,我的html的溢出变得隐藏。然后,当再次单击时,它将变为可见,依此类推。到目前为止,我已经这样做了:(按钮的 id #plus(

$('#plus').click(function () {
   $("html").css({
      'overflow-y': 'hidden',
   });
});

当然,它只工作一次。我怎样才能把它调回来?

$('#plus').click(function () {
   var overflowY = 'hidden'; 
   if($("html").css('overflow-y') == 'hidden') overflowY = 'visible';
   $("html").css({ 'overflow-y': overflowY });
});
html, body {
  height:200%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="plus">Change Overflow Y</button>