cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console