Saibbyweb Saibbyweb - 3 months ago 20
CSS Question

Why does my off-canvas menu push my fixed div out of frame when opened?

I use Pushy off-canvas menu for my website, it works great but it troubles my fixed div header. When I scroll down through the page, the header sticks perfectly to top but once I open the off-canvas menu, my header disappears? And when I close my off-canvas menu, it comes back abruptly. It looks weird.

You can check the (code pen) live demo of my problem, here.
Or directly run the code snippet below.

I want it to stay visible when the off-canvas menu is opened.
Help would be appreciated.

Thanks in advance.




/*! Pushy - v1.0.0 - 2016-3-1
* Pushy is a responsive off-canvas navigation menu using CSS transforms & transitions.
* https://github.com/christophery/pushy/
* by Christopher Yee */

(function($) {
var pushy = $('.pushy'), //menu css class
body = $('body'),
container = $('#container'), //container css class
push = $('.push'), //css class to add pushy capability
pushyLeft = 'pushy-left', //css class for left menu position
pushyOpenLeft = 'pushy-open-left', //css class when menu is open (left position)
pushyOpenRight = 'pushy-open-right', //css class when menu is open (right position)
siteOverlay = $('.site-overlay'), //site overlay
menuBtn = $('.menu-btn, .pushy-link'), //css classes to toggle the menu
menuSpeed = 200, //jQuery fallback menu speed
menuWidth = pushy.width() + 'px', //jQuery fallback menu width
submenuClass = '.pushy-submenu',
submenuOpenClass = 'pushy-submenu-open',
submenuClosedClass = 'pushy-submenu-closed',
submenu = $(submenuClass);

function togglePushy() {
//add class to body based on menu position
if (pushy.hasClass(pushyLeft)) {
body.toggleClass(pushyOpenLeft);
} else {
body.toggleClass(pushyOpenRight);
}
}

function openPushyFallback() {

//animate menu position based on CSS class
if (pushy.hasClass(pushyLeft)) {
body.addClass(pushyOpenLeft);
pushy.animate({
left: "0px"
}, menuSpeed);
container.animate({
left: menuWidth
}, menuSpeed);
//css class to add pushy capability
push.animate({
left: menuWidth
}, menuSpeed);
} else {
body.addClass(pushyOpenRight);
pushy.animate({
right: '0px'
}, menuSpeed);
container.animate({
right: menuWidth
}, menuSpeed);
push.animate({
right: menuWidth
}, menuSpeed);
}

}

function closePushyFallback() {

//animate menu position based on CSS class
if (pushy.hasClass(pushyLeft)) {
body.removeClass(pushyOpenLeft);
pushy.animate({
left: "-" + menuWidth
}, menuSpeed);
container.animate({
left: "0px"
}, menuSpeed);
//css class to add pushy capability
push.animate({
left: "0px"
}, menuSpeed);
} else {
body.removeClass(pushyOpenRight);
pushy.animate({
right: "-" + menuWidth
}, menuSpeed);
container.animate({
right: "0px"
}, menuSpeed);
push.animate({
right: "0px"
}, menuSpeed);
}

}

function toggleSubmenu() {
//hide submenu by default
$(submenuClass).addClass(submenuClosedClass);

$(submenuClass).on('click', function() {
var selected = $(this);

if (selected.hasClass(submenuClosedClass)) {
//hide opened submenus
$(submenuClass).addClass(submenuClosedClass).removeClass(submenuOpenClass);
//show submenu
selected.removeClass(submenuClosedClass).addClass(submenuOpenClass);
} else {
//hide submenu
selected.addClass(submenuClosedClass).removeClass(submenuOpenClass);
}
});
}

function toggleSubmenuFallback() {
//hide submenu by default
$(submenuClass).addClass(submenuClosedClass);

submenu.children('a').on('click', function(event) {
event.preventDefault();
$(this).toggleClass(submenuOpenClass)
.next('.pushy-submenu ul').slideToggle(200)
.end().parent(submenuClass)
.siblings(submenuClass).children('a')
.removeClass(submenuOpenClass)
.next('.pushy-submenu ul').slideUp(200);
});
}

//checks if 3d transforms are supported removing the modernizr dependency
var cssTransforms3d = (function csstransforms3d() {
var el = document.createElement('p'),
supported = false,
transforms = {
'webkitTransform': '-webkit-transform',
'OTransform': '-o-transform',
'msTransform': '-ms-transform',
'MozTransform': '-moz-transform',
'transform': 'transform'
};

// Add it to the body to get the computed style
document.body.insertBefore(el, null);

for (var t in transforms) {
if (el.style[t] !== undefined) {
el.style[t] = 'translate3d(1px,1px,1px)';
supported = window.getComputedStyle(el).getPropertyValue(transforms[t]);
}
}

document.body.removeChild(el);

return (supported !== undefined && supported.length > 0 && supported !== "none");
})();

if (cssTransforms3d) {
//make menu visible
pushy.css({
'visibility': 'visible'
});

//toggle submenu
toggleSubmenu();

//toggle menu
menuBtn.on('click', function() {
togglePushy();
});
//close menu when clicking site overlay
siteOverlay.on('click', function() {
togglePushy();
});
} else {
//add css class to body
body.addClass('no-csstransforms3d');

//hide menu by default
if (pushy.hasClass(pushyLeft)) {
pushy.css({
left: "-" + menuWidth
});
} else {
pushy.css({
right: "-" + menuWidth
});
}

//make menu visible
pushy.css({
'visibility': 'visible'
});
//fixes IE scrollbar issue
container.css({
"overflow-x": "hidden"
});

//keep track of menu state (open/close)
var opened = false;

//toggle submenu
toggleSubmenuFallback();

//toggle menu
menuBtn.on('click', function() {
if (opened) {
closePushyFallback();
opened = false;
} else {
openPushyFallback();
opened = true;
}
});

//close menu when clicking site overlay
siteOverlay.on('click', function() {
if (opened) {
closePushyFallback();
opened = false;
} else {
openPushyFallback();
opened = true;
}
});
}
}(jQuery));

body {
margin: 0px;
}
div.header {
padding: 10px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
position: fixed;
top: 0;
width: 100%;
height: 40px;
background: white;
z-index: 100;
}
div.content {
padding-top: 70px;
padding-left: 300px;
padding-right: 300px;
}
div.menu-btn {
float: left;
font-size: 20px;
}
/*! Pushy - v1.0.0 - 2016-3-1
* Pushy is a responsive off-canvas navigation menu using CSS transforms & transitions.
* https://github.com/christophery/pushy/
* by Christopher Yee */

/* Menu Appearance */

.pushy {
position: fixed;
width: 200px;
height: 100%;
top: 0;
z-index: 9999;
background: #191918;
overflow: auto;
visibility: hidden;
-webkit-overflow-scrolling: touch;
/* enables momentum scrolling in iOS overflow elements */
}
.pushy a {
display: block;
color: #b3b3b1;
padding: 15px 30px;
text-decoration: none;
outline: 0;
}
.pushy a:hover {
color: #FFF;
}
.pushy ul:first-child {
margin-top: 10px;
}
.pushy.pushy-left {
left: 0;
}
.pushy.pushy-right {
right: 0;
}
/* Menu Movement */

.pushy-left {
-webkit-transform: translate3d(-200px, 0, 0);
-ms-transform: translate3d(-200px, 0, 0);
transform: translate3d(-200px, 0, 0);
}
.pushy-open-left #container,
.pushy-open-left .push {
-webkit-transform: translate3d(200px, 0, 0);
-ms-transform: translate3d(200px, 0, 0);
transform: translate3d(200px, 0, 0);
}
.pushy-right {
-webkit-transform: translate3d(200px, 0, 0);
-ms-transform: translate3d(200px, 0, 0);
transform: translate3d(200px, 0, 0);
}
.pushy-open-right #container,
.pushy-open-right .push {
-webkit-transform: translate3d(-200px, 0, 0);
-ms-transform: translate3d(-200px, 0, 0);
transform: translate3d(-200px, 0, 0);
}
.pushy-open-left .pushy,
.pushy-open-right .pushy {
-webkit-transform: translate3d(0, 0, 0);
-ms-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
/* Menu Transitions */

#container,
.pushy,
.push {
transition: transform 0.2s cubic-bezier(0.16, 0.68, 0.43, 0.99);
}
/* Site Overlay */

.site-overlay {
display: none;
}
.pushy-open-left .site-overlay,
.pushy-open-right .site-overlay {
display: block;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 9998;
background-color: rgba(0, 0, 0, 0.5);
-webkit-animation: fade 500ms;
animation: fade 500ms;
}
@keyframes fade {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-webkit-keyframes fade {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
/* Submenu Appearance */

.pushy-submenu {
/* Submenu Icon */
}
.pushy-submenu ul {
padding-left: 15px;
transition: max-height 0.2s ease-in-out;
}
.pushy-submenu ul .pushy-link {
transition: opacity 0.2s ease-in-out;
}
.pushy-submenu > a {
position: relative;
}
.pushy-submenu > a::after {
content: '';
display: block;
height: 11px;
width: 8px;
position: absolute;
top: 50%;
right: 15px;
background: url("../img/arrow.svg") no-repeat;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
transition: transform 0.2s;
}
/* Submenu Movement */

.pushy-submenu-closed ul {
max-height: 0;
overflow: hidden;
}
.pushy-submenu-closed .pushy-link {
opacity: 0;
}
.pushy-submenu-open {
/* Submenu Icon */
}
.pushy-submenu-open ul {
max-height: 1000px;
}
.pushy-submenu-open .pushy-link {
opacity: 1;
}
.pushy-submenu-open a::after {
-webkit-transform: translateY(-50%) rotate(90deg);
-ms-transform: translateY(-50%) rotate(90deg);
transform: translateY(-50%) rotate(90deg);
}
.no-csstransforms3d .pushy-submenu-closed ul {
max-height: none;
display: none;
}
/*# sourceMappingURL=pushy.css.map */

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>


<body>
<!-- Pushy Menu -->
<nav class="pushy pushy-left">

</nav>

<!-- Site Overlay -->
<div class="site-overlay"></div>

<!-- Your Content -->
<div id="container">
<div class="header">
<div class="menu-btn">&#9776; Sroll down a bit and Click me, I vanish (get stuck at top)</div>

</div>


<div align="center" class="content">
<h1> Lorem Ipsum </h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras
dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius
laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque
sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis
leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras
dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius
laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque
sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis
leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras
dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius
laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque
sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis
leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras
dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius
laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque
sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis
leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras
dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius
laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque
sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis
leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>
</div>
</div>
</body>

</html>




Answer

There is a closed issue about that in pushy:

The trick is to have any fixed position (like a fixed header) elements outside of the container div. See the demo.html for examples.

Moving .header outside #container in your case works.

If you're interested in what is causing that, see this answer.

Comments