Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>sticky要素の上部固定を検知するサンプル</title>
  </head>
  <body>
    <p class="content">前書き前書き前書き</p>
    <header>
      <div id="header-before"></div>
      固定部分
    </header>
    <p class="content">すごく縦に長いページ内容。もしPC画面のブラウザなどでこれでも長さが足りない場合はウィンドウを小さくしたり開発者ツールを出したりしてみてください</p>
  </body>
</html>
              
            
!

CSS

              
                p.content {
  writing-mode: vertical-lr;
  margin: 1em 0;
  white-space: nowrap;
}

/* 固定部分のスタイル */
header {
  position: sticky;
  left: 0;
  top: 0;
  background-color: #444444;
  color: #ffffff;
  padding: 6px;
  font-weight: bold;
}
/* 固定部分がページ上部に固定されているときのスタイル
(JavaScriptからクラスを付与)
*/
.fixed {
  background-color: #440088;
}

/* ヘッダーの前後のダミー要素 */
#header-before {
  position: absolute;
  left: 0;
  top: -33px;
  width: 100%;
  height: 32px;
  background-color: rgba(255, 64, 64, 0.3);
  pointer-events: none;
}
              
            
!

JS

              
                // 後で操作する用に固定部分の要素を取得
const header = document.querySelector('header');
const observer = new IntersectionObserver(entries => {
  for (const entry of entries) {
    // entry.isIntersectingは
    // 赤い半透明の部分がわずかでも画面に入っていればtrue
    if (entry.isIntersecting) {
      // 固定されていない場合の処理
      header.classList.remove('fixed');
    } else {
      // 固定されている場合の処理
      header.classList.add('fixed');
    }
  }
});

observer.observe(document.getElementById('header-before'));
              
            
!
999px

Console