一个dark mode的动画

简介: 一个dark mode的动画

前言


还记得去年更新了几个 Dark Mode 的动画,比如这个按钮动画:



image.png

还有这个拉绳动画:

image.png


今年掘金出了一个码上掘金的playground,就顺势再更新几个吧~


本次灵感来源于第一个按钮动画,当时看着挺满意的,现在就觉得差点意思,完全可以不用加 span 做遮挡,svg好像也是多余的,直接用CSS画出来可能会更好... 反正做一个小小的改进吧,成果如下


image.png


有手就行的代码,但还是简单说一下思路,毕竟有字数限制🤷‍♂️


HTML+CSS


因为涉及到状态的切换,所以 HTML 部分我们依然使用 checkbox 的选中和未选中两种状态来代表明暗两种模式。


<h1>Mancuoj</h1>
<input type="checkbox" id="dark-checkbox" />
<label for="dark-checkbox"></label>
复制代码


然后我们需要在CSS部分将 checkbox 隐藏,用 label 替代并画出按钮。


/* 一些预设跳过了 */
label {
  display: block;
  position: relative;
  width: 90px;
  height: 50px;
  border-radius: 50px;
  background-color: $bg-color;
  cursor: pointer;
  &:before,
  &:after {
    display: block;
    position: absolute;
    content: "";
    width: 36px;
    height: 36px;
    border-radius: 50%;
    top: 7px;
    left: 7px;
    transition: all 1s;
  }
  &:before {
    background: linear-gradient(#fcd670, #f2784b);
  }
}
复制代码


这里我们用 :beforeafter 画出两个圆,将其中一个⚪加上颜色代表☀,然后两个⚪取交集部分作为🌙,如图所示:


image.png


隐藏复选框,然后移动⚪位置即可,也可以在这一步再把 :after 的内容画出来,不用移动位置:


#dark-checkbox {
  display: none;
  &:checked + label {
    &:before {
      background: linear-gradient(#f8f4ed, #e9ddb6);
      transform: translateX(40px);
    }
    &:after {
      background-color: $bg-color;
      transform: translateX(24px);
    }
  }
}
复制代码

然后是与 JS 联动的部分:


.dark {
  background-color: $dark-color;
  color: $light-color;
  transition: all 1s;
}
复制代码


JS


因为想要用 localStorage 记录用户的设置,所以就显得比较麻烦:


const changeTheme = () => {
  const darkCheck = document.getElementById("dark-checkbox");
  darkCheck.addEventListener("click", () => {
    if (darkCheck.checked) {
      document.body.classList.add("dark");
      localStorage.setItem("css", "dark");
    } else {
      document.body.classList.remove("dark");
      localStorage.removeItem("css");
    }
  });
  if (localStorage.getItem("css")) {
    document.body.className = "dark";
    darkCheck.checked = true;
  }
};
changeTheme();
复制代码


如果不用 localStorage 直接用一个 toggle() 一行就够了,你甚至可以不用JS,都随你~

目录
相关文章
|
4月前
mix-blend-mode 利用混合模式让文字智能适配背景颜色
这篇文章介绍了一种CSS3属性:mix-blend-mode,它可以实现文字智能适配背景颜色和文字镂空效果。通过设置mix-blend-mode为difference,可以在黑色背景中显示白色文字,在白色背景中显示黑色文字。同时,文章还提到了mix-blend-mode的其他属性,如multiply、screen等等,感兴趣的读者可以进一步研究。
mix-blend-mode 利用混合模式让文字智能适配背景颜色
|
2月前
|
前端开发
css 平滑滚动 scroll-behavior: smooth
css 平滑滚动 scroll-behavior: smooth
38 1
|
前端开发
transition和animation的区别
transition和animation的区别
Core Animation - 渐变色CAGradientLayer
Core Animation - 渐变色CAGradientLayer
90 0
|
前端开发 JavaScript
两个dark mode的动画
两个dark mode的动画
104 0
两个dark mode的动画
Mysterious Light——AT
题目描述 Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of Mysterious Light. Three mirrors of length N are set so that they form an equilateral triangle. Let the vertices of the triangle be a,b and c.
119 0
Mysterious Light——AT
|
前端开发 JavaScript API
Qt-QML-给我的导航条写一个动画-State-Transition
上篇中,我已经写出一个导航条的,虽然太丑了,不过功能是有了,这次我将要给我的导航条加一个动画,先看下演示效果
159 0
Qt-QML-给我的导航条写一个动画-State-Transition
|
容器 JavaScript Web App开发
如何不用 transition 和 animation 也能做网页动画
效果预览 在线演示 按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。 https://codepen.io/comehope/pen/BxbQJj 可交互视频教程 此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。
1124 0
MPAndroidChart 教程:设置颜色 Setting Colors
从v1.4.0版本开始,不再需要(不建议使用)在先前版本中负责设置颜色的ColorTemplate对象。然而,它仍然保留所有预定义的颜色数组(例如,ColorTemplate.VORDIPLOM_COLORS并提供方便的方法,用于将颜色从资源(资源整数)转换为“真实”颜色。
1536 0