0

I'm trying to toggle the scrollbar of the <html> tag using jquery.

I want the scrollbar to disappear when i click on button #foo, and i want it to reappear when i click on #bar1.

$('#foo').click(function() {
  $('html').toggleClass("hidescroll");
});
$('#bar1').click(function() {
  $('html').toggleClass("hidescroll");
});
.hidescroll{
  overflow-y: hidden !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo">foo</div>
<div id="bar1">bar1</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

it disappears when i click on #foo, but it doesnt reappear on #bar1, however it reappears when #foo is clicked again.

5
  • What is CSS for .hidescroll Commented Nov 15, 2018 at 10:32
  • 1
    can you make a js fiddle. according to your code it will work no issues Commented Nov 15, 2018 at 10:33
  • It does work -> jsfiddle.net/fyedavht. Do you definitely have same ids of foo and bar1 in your html? Commented Nov 15, 2018 at 10:37
  • its working bro Commented Nov 15, 2018 at 10:42
  • your example clearly works here Commented Nov 15, 2018 at 10:44

3 Answers 3

2

You can do it like this...use add and removeClass instead of toggleClass...

$('#foo').click(function() {
  $('html').addClass("hidescroll");
});

$('#bar1').click(function() {
  $('html').removeClass("hidescroll");
});
.hidescroll {
  overflow:hidden;
}
#main {
  width:100%;
  height:2000px;
  float:left;
}
#foo {
  float:left;
  width:100px;
  height:50px;
  background-color:red;
  margin-right:15px;
}
#bar1 {
  float:left;
  width:100px;
  height:50px;
  background-color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
<div id="foo"></div>
<div id="bar1"></div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

it worked using overflow: hidden instead of overflow-y: hidden. thank you.
2
$("#foo, #bar1")

is the selector you're looking for.

Snippet :

$('#foo, #bar1').click(function() {
  $('html').toggleClass("hidescroll");
});
.hidescroll {
  overflow:hidden;
}
#main {
  width:100%;
  height:2000px;
  float:left;
}
#foo {
  float:left;
  width:100px;
  height:50px;
  background-color:red;
  margin-right:15px;
}
#bar1 {
  float:left;
  width:100px;
  height:50px;
  background-color:blue;
}
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<div id="main">
<div id="foo"></div>
<div id="bar1"></div>
</div>

Comments

0

Just an alternative

bar =v=> $('html').css('overflow',v)
$('#foo').click(()=> bar('hidden'))
$('#bar1').click(()=> bar('scroll'))

Comments

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.