首頁  >  文章  >  web前端  >  查看過渡主題動畫

查看過渡主題動畫

Patricia Arquette
Patricia Arquette原創
2024-09-25 17:11:42938瀏覽

View transition theme animations

使用 CSS 和視圖轉換從淺色模式轉換為深色模式時添加酷炫效果

複製自 Twitter 上的@jhey

[!注意]
這假設您已經設定了暗光模式,並具有某種功能來更新您的主題

  1. 添加CSS
  /* Angled */
  [data-style='angled']::view-transition-old(root) {
    animation: none;
    z-index: -1;
  }

  [data-style='angled']::view-transition-new(root) {
    animation: unclip 1s;
    clip-path: polygon(-100vmax 100%, 100% 100%, 100% -100vmax);
  }

  @keyframes unclip {
    0% {
      clip-path: polygon(100% 100%, 100% 100%, 100% 100%);
    }
  }

  1. 確保在根元素上設定 data-style="angled" 屬性 在 SPA React 中我們使用 useEffect 鉤子
  useEffect(() => {
    // set the data-style attribute
    document.documentElement.dataset.style = "angled";
  }, []);

在SSR中可以直接在html標籤中設定

  1. 將主題變更函數包裝在 documnet.startViewTransition 中以啟動視圖轉換
  function transitionColors() {
    if (typeof window !== "undefined") {
      document.startViewTransition(() => {
        const newTheme = theme  === "light" ? "dark" : "light";
        document.documentElement.dataset.theme = newTheme;
        updateTheme(newTheme);
      });
    }
  }

可以透過包含對應的 css 檔案並添加正確的 data-style 屬性來新增更多過渡樣式

      <select
      className="select select-bordered select-sm max-w-xs"
        onChange={(e) =>
          (document.documentElement.dataset.style = e.target.value)
        }
      >
        <option value="default">Default</option>
        <option value="vertical">Vertical</option>
        <option value="wipe">Wipe</option>
        <option value="angled">Angled</option>
        <option value="flip">Flip</option>
        <option value="slides">Slides</option>
      </select>

反應範例

如果你喜歡這種類型的 CSS 技巧,請考慮關注 jhey

以上是查看過渡主題動畫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn