Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <main class="js-scroll-overlap">
    <h1>
      Overlap areas with scrolling 
      <br>Vanilla JS (position: sticky;)
    </h1>
  
    <section class="js-scroll-overlap">
      <p><img src="https://picsum.photos/id/1003/500/600" alt="" width="500" height="600"></p>
      <p class=""><a href="">リンク</a></p>
    </section>
  
    <section class="js-scroll-overlap">
      <p><img src="https://picsum.photos/id/1010/1600/500" alt="" width="1600" height="500"></p>
    </section>
  
    <section class="relative">
      <p><img src="https://picsum.photos/id/1060/1600/500" alt="" width="1600" height="500"></p>
      <p><img src="https://picsum.photos/id/1001/700/600" alt="" width="700" height="600"></p>
      <p class=""><a href="">リンク</a></p>
    </section>
  </main>

  <footer class="relative">
    <p><img src="https://picsum.photos/id/270/400/500" alt="" width="400" height="500"></p>
    <p><img src="https://picsum.photos/id/1051/1200/500" alt="" width="1200" height="500"></p>
    <p class=""><a href="">リンク</a></p>
  </footer>
              
            
!

CSS

              
                .js-scroll-overlap:not(.is-disabled) {
  --sticky-offset: -1px;
  position: sticky;
  top: var(--sticky-offset);
}
.relative {
  position: relative;
}


/* テスト表示用style */
body {
  margin: 0;
  padding: 100px 0 0;
  text-align: center;
}
h1 {
  text-align: center;
  font-size: 40px;
  margin-bottom: 100px;
}
p + p {
  margin-top: 100px;
}
img {
  height: auto;
  max-width: 100%;
  vertical-align: top;
}
section {
  padding: 100px 50px;
}
section:nth-child(even) {
  background-color: #fff;
}
section:nth-child(odd) {
  background-color: #ccc;
}
footer {
  background: #000;
  color: #fff;
  padding: 100px 50px;
}
footer a {
  color: inherit;
}
              
            
!

JS

              
                //スクロールしてエリアが切り替わる時に次のコンテンツと重ねる
scrollOverlap();
function scrollOverlap() {
  const targets = document.querySelectorAll('.js-scroll-overlap');
  if (targets.length === 0) {
    return;
  }

  let lastWinHight = window.innerHeight;

  //position: sticky;のオフセット値をCSS変数で設定
  const setStickyOffset = () => {
    targets.forEach((target) => {
      const targetHeight = target.offsetHeight;
			//ウィンドウの高さが要素の高さより大きい場合はオフセット値を-1pxに設定
      const offsetValue = lastWinHight > targetHeight ? '-1px' : `-${targetHeight - lastWinHight}px`;
      target.style.setProperty('--sticky-offset', offsetValue);
    });
  };
  setStickyOffset();

  addEventListener('resize', () => {
	  //ウィンドウの高さが変わった時のみウィンドウの高さを更新
    const winHight = window.innerHeight;
    if (lastWinHight !== winHight) {
      lastWinHight = winHight;
    }
  });
  
  // bodyのサイズ変更を監視して再取得
  const body = document.body;
  // ResizeObserverを作成
  const resizeObserver = new ResizeObserver((entries) => {
    for (let entry of entries) {
      setStickyOffset();
    }
  });
  // body要素を監視対象に追加
  resizeObserver.observe(body);

  /*
    キーボード操作時に無効化するクラスを付与する
     フォーカス移動時にフォーカスした要素と、
		 次のエリアが重なって要素が見えなくなってしまう可能性への対応
  */
  const toggleDisabledClass = (boolean) => {
    targets.forEach((target) => {
      target.classList.toggle('is-disabled', boolean);
    });
  };

  //キーボードフォーカス状態を管理するフラグ
  let isKeyboardFocus = false;

  //キーボードのフォーカス移動時には無効化する
  document.addEventListener('keydown', (event) => {
    if (event.key === 'Tab') {
      //Tabキーが押されたのでキーボード操作中
      isKeyboardFocus = true; 
      toggleDisabledClass(isKeyboardFocus);
    }
  });

  //マウス操作の検知で無効化を解除する
  document.addEventListener('mousedown', () => {
    if (isKeyboardFocus) {
      //マウス操作があったのでキーボードフォーカスを解除
      isKeyboardFocus = false;
      toggleDisabledClass(isKeyboardFocus);
    }
  });
}
              
            
!
999px

Console