首页  >  文章  >  web前端  >  查看过渡主题动画

查看过渡主题动画

Patricia Arquette
Patricia Arquette原创
2024-09-25 17:11:42931浏览

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