My website is contained in a wrapper with a max width, and I have a fixed side menu that can be toggled with a button.
The problem I am facing is keeping the fixed side menu within the page wrapper. Fixed elements are typically positioned relative to the window rather than the parent element.
Here is an example using position: fixed
: https://jsfiddle.net/okavp4p1/
As shown in the link above, the menu extends beyond the viewport instead of staying within the wrapper (grey area).
https://i.sstatic.net/mioNI.gif
To work around this issue, I utilized position: absolute
: https://jsfiddle.net/5q3hn1fq/
In this updated version, the menu remains inside the wrapper as intended.
https://i.sstatic.net/SYFFl.gif
However, I had to add extra jQuery code to mimic fixed positioning during scrolling.
// Fix menu
$(window).on('load scroll resize', function() {
navigation.find('ul').css('top', $(window).scrollTop());
});
Unfortunately, this method causes glitches and lag on most browsers. While the scrolling animation in the example may not show much issues, it becomes very apparent when implemented on a site with more elements.
Check out how it looks like when scrolling down the page:
https://i.sstatic.net/jOaC4.gif
I considered disabling scrolling when the menu is open but I am seeking advice or suggestions from others. Can anyone offer assistance?