メニューがスクロールに応じてにゅっと出て、初期位置以外でシャドウがかかるよ

ChatGPTちゃんと一緒に、初期位置以外でシャドウがかかるようにしました。隠れてる時や初期位置ではシャドウが消えます。白背景の時とかに使えるかなと。

こちらの応用です。Tomockyさん作のやつ。
https://community-ja.studio.design/c/customcode_library/fa512e

実際の動きはこちら。https://btcon.jp/2024
ChatGPT任せなので、自己責任で!
サイト全体に適用しないと、ページの遷移時に挙動がちょとおかしかったりします。

bodyの末尾がいいかもです〜。

<style>
  #任意のメニューのID {
    transition: top 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
  }

  .scrolled {
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1);
  }
</style>

<script>
  window.addEventListener("scroll", function() {
    var header = document.getElementById("任意のメニューのID");
    var currentScroll = window.pageYOffset;
    var headerHeight = header.offsetHeight;

    if (typeof this.lastScroll === 'undefined') {
      this.lastScroll = 0;
    }

    if (currentScroll <= 0) {
      header.style.top = "0";
      header.classList.remove("scrolled");
    } else if (currentScroll > this.lastScroll) {
      header.style.top = -headerHeight + "px";
      header.classList.remove("scrolled");
    } else {
      header.style.top = "0";
      header.classList.add("scrolled");
    }
    this.lastScroll = currentScroll;
  }, false);
</script>