为什么我的非画布菜单在打开时将我的固定div推出框架?

时间:2016-08-06 13:24:39

标签: javascript jquery css off-canvas-menu

我在我的网站上使用Pushy off-canvas menu,它运行良好,但它修复了我的固定 div标题。当我向下滚动页面时,标题完全贴在顶部但是一旦我打开画布上的菜单,我的标题就消失了?当我关闭我的画布菜单时,它会突然回来。看起来很奇怪。

您可以查看(代码笔live demo of my problem, here. 或直接运行下面的代码段。

我想让它在打开画布菜单时保持可见。 帮助将不胜感激。
在此先感谢。



/*! 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

有一个关闭的issue关于那个在pushy:

  

诀窍是拥有任何固定位置(如固定标头)元素   在容器div之外。有关示例,请参阅demo.html。

在您的案例中将.header移到#container之外。

如果您对导致该问题的原因感兴趣,请参阅this answer