1

I want to avoid, that a user scroll the background when my mobile navbar is opened. That is no problem so far, just set overflow: hidden on body. The problem is, if the navbar has so many items, that it's higher than the users viewport, it won't scroll too. So I want to be able to scroll within the navbar but not on the body at the same time.

The question is thus: Is it possible to change the overflow behavior of an nested element?

Simplified example

// simple helper to add many list items
var menu = document.getElementById('menu');
for(var i=0; i<100; i++) menu.innerHTML += '<li>foo</li>';
.scroll-disabled { overflow: hidden; }
.scroll-enabled { overflow: visible; }
<body class="scroll-disabled">
  <ul class="scroll-enabled" id="menu">
    <!-- lots of li's // more than the display can show at once-->
  </ul>
</body>

0

3 Answers 3

3

You can give the element fixed height and set auto scroll on overflow:

.scroll-enabled {height: 100px; overflow:auto;}
Sign up to request clarification or add additional context in comments.

Comments

2

Just use max-height: 100vh with overflow: auto. That way you don't have to set a fixed height and scrolling will only appear when the menu height is greater than the view port.

// simple helper to add many list items
var menu = document.getElementById('menu');
for(var i=0; i<100; i++) menu.innerHTML += '<li>foo</li>';
.scroll-disabled { overflow: hidden; }
.scroll-enabled {
  overflow: auto;
  max-height: 100vh;
}
<body class="scroll-disabled">
  <ul class="scroll-enabled" id="menu">
    <!-- lots of li's // more than the display can show at once-->
  </ul>
</body>

Here an example with the menu not exceeding the view port height.

// simple helper to add many list items
var menu = document.getElementById('menu');
for(var i=0; i<8; i++) menu.innerHTML += '<li>foo</li>';
.scroll-disabled { overflow: hidden; }
.scroll-enabled {
  overflow: auto;
  max-height: 100vh;
}
<body class="scroll-disabled">
  <ul class="scroll-enabled" id="menu">
    <!-- lots of li's // more than the display can show at once-->
  </ul>
</body>

6 Comments

That is perfect! Thank you
if the element is not the entire screen but a sub component, giving it a fixed height like this will lead to it having incorrect height. And if you want the scrollbar to show up on that element, you get another scrollbar on the main page.
@PuiHoLam I am not really sure what your trying to achieve, but I posted this answer a long time ago and the OP accepted it as the correct answer. Maybe you should post your own question so that someone can help you with your problem?
Not seeking an answer, just putting it here so random people don't believe everything they see on the internet.
@Pui Ho Lam You're obviously describing a complete different situation. The OPs question did not mention a "a sub component", there are plenty of answers on SO to what you are describing though, oh and not to mention that the expression "component" in this context is somewhat misleading. If you are truly concerned about other people consuming the wrong information, you should post an alternative answer which addresses the issue? That way others could upvote it and it would show a solution to a similar problem for those who come here with the exact same issue. It could be helpful.
|
1

For this you have to define a fix height of ul.scroll-enabled and then use overflow:auto also so it can scroll within the ul.

Here is a fiddle example for the same :)

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.