cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <header>
    <h1>Title</h1>
    <nav id="global-nav">
        <a href="">hogehoge</a>
    </nav>
</header>
<section>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
    <p>piyopiyo</p>
</section>
            
          
!
            
              header {
    height: 100px;
}

#global-nav {
    width: 100%;
    background: white;
}

.fixed {
    position: fixed;
    top: 0;
    z-index: 1;
}
            
          
!
            
              
// http://qiita.com/ANTON072/items/ea5b95ae5b7f0f6186f7
var getOffset = function(elem, relativeToViewport) {
  var offset = {
    top : 0,
    left: 0
  };
  var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

  if (!elem) return offset;
  if (elem.getBoundingClientRect) {
    var rect = elem.getBoundingClientRect();
    offset.top  = rect.top;
    offset.left = rect.left;
    if (!elem.relativeToViewport) {
      offset.top  += scrollTop;
      offset.left += scrollLeft;
    }
  }
  else {
    if (relativeToViewport) {
      offset.top  -= scrollTop;
      offset.left -= scrollLeft;
    }
  }
  return offset;
};

// http://qiita.com/zaru/items/82d61ac90394885fb486

var nav = document.getElementById('global-nav');
// メニューのtop座標を取得する
var offsetTop = this.getOffset(nav).top;

var floatMenu = function() {
var nav = document.getElementById('global-nav');
    // スクロール位置がメニューのtop座標を超えたら固定にする
  var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  if (scrollTop > offsetTop) {
        nav.classList.add('fixed');
  } else {
        nav.classList.remove('fixed');
  }
}

document.addEventListener("scroll", _.throttle(floatMenu, 100), false);

            
          
!
999px
Loading ..................

Console