somewebmedia / hc-offcanvas-nav

JavaScript library for creating toggled off-canvas multi-level navigations, allowing endless nesting of submenu elements, supporting swipe gestures, keyboard interactions and ARIA attributes.
https://somewebmedia.github.io/hc-offcanvas-nav/
MIT License
336 stars 82 forks source link

Issue with fixed header #48

Open minemindmedia opened 3 years ago

minemindmedia commented 3 years ago

First of all, thank you for this awesome menu!!!

I am having trouble getting it to work with a fixed header. When opening the menu, I have it set to push the content to the right. It works fine on first page load but if I scroll down and then open the menu, the fixed header disappears. I can't figure out what is causing this to happen. Hope you have a solution, thank you.

somewebmedia commented 3 years ago

Can you provide me with some demo so I can check what's happening?

Phoenixy commented 3 years ago

Hi, I also saw this happening to me as well, but I think this is the expected behavior, so I have no problems with it. If you open the menu on the top of the page (without any scrolling), the fixed header won't disappear, but if you scroll down, then open the menu there, the fixed header disappears. example1 example2

Exacore commented 3 years ago

You can try to fix this bug with thoses lines of code (jQuery) :

// Resize function resize() { // Sticky Fix var headerHeight = $('.header').outerHeight(); $('.header').css('position', 'fixed'); $('.header').next().css('margin-top', headerHeight + 'px'); } window.addEventListener('resize', resize); resize();

somewebmedia commented 3 years ago

Ah I see, when the menu opens, if you have pushContent option enabled, the container element gets transform property which displaces any fixed child element. Here is the explanation of why that's happening: https://stackoverflow.com/questions/15194313/transform3d-not-working-with-position-fixed-children/15256339#15256339

So a workaround would be something like this:

var $fixed_header = $('#fixed-header'); // your fixed header element
var $container = $('#container'); // element that you've set to pushContent

Nav.on('toggle', function(e, settings) {
  if (e.data.action == 'open') {
    $fixed_header.css('top', -$container.offset().top);
  }
});

Nav.on('close', function(e, settings) {
  $fixed_header.css('top', 0);
});