两个dark mode的动画

简介: 两个dark mode的动画

前言


前文:一个dark mode的动画

一二三四,二二三四,继续来更新 Dark Mode 的动画,这次我们更新两个,都是来源于我记忆中网站的动画,就当作复刻吧。


第一个


第一个改自我前面写的按钮动画,加了一个简单的旋转动画,效果如下:

image.png


有手就行,定义一个简单的 @keyframes 关键帧动画:


@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
复制代码


然后使用 animation 属性调用即可:


.moon-svg,
.sun-svg {
  width: 30px;
  height: 30px;
  top: 30%;
  right: 30%;
  position: absolute;
  animation: rotate 2s linear infinite;
}
复制代码


在切换明亮和黑暗模式时,调用 visibility 属性调整 svg 的可见性:


.dark {
  transition: 1s;
  background-color: $dark-color;
  color: $light-color;
  .sun-svg {
    fill: $light-color;
    visibility: visible;
  }
  .moon-svg {
    visibility: hidden;
  }
}
复制代码


第二个


第二个也是一个动画,但是我们不用 svg,直接用CSS来画太阳和月亮,效果如下:


image.png


我们把它分成两个部分,首先是两个方块叠加的齿轮背景,也可以看作☀的边角,用 div 画两个不同方向的正方形就行了:


.gear {
  position: absolute;
  background-color: $light-color;
  height: 100px;
  width: 100px;
  border-radius: 5px;
  transition: 2s;
}
#gear1 {
  transform: rotate(-45deg);
}
#gear2 {
  transform: rotate(-90deg);
}
复制代码


在点击时切换 active 的 class,让它们旋转起来:


#gear1.active {
  background-color: $bg-color;
  transform: rotate(225deg);
  transition: 2s;
}
#gear2.active {
  background-color: $bg-color;
  transform: rotate(180deg);
  transition: 2s;
}
复制代码


另一部分是中心的太阳和月亮,依旧采用上篇文章两个圆叠加的方法,画出一大一小两个圆,在切换模式的时候移动位置,如图:


image.png


#big {
  position: absolute;
  background-color: $bg-color;
  height: 70px;
  width: 70px;
  border-radius: 50%;
  transition: 2s;
}
#small {
  position: absolute;
  background-color: $light-color;
  height: 50px;
  width: 50px;
  border-radius: 50%;
  transition: 2s;
}
#small.active {
  height: 60px;
  width: 60px;
  transform: translate(15px, -5px);
  transition: 2s;
  background-color: $bg-color;
}
#big.active {
  background-color: $light-color;
  transition: 2s;
}
复制代码


同样是加上 active 类,在点击时随着齿轮的旋转移动圆的位置和颜色,叠加出月亮的形状,非常简单🤷‍♂️


JS 偷个懒,只使用 toggle() 来做切换,就不多说了。

目录
相关文章
|
1月前
mix-blend-mode 利用混合模式让文字智能适配背景颜色
这篇文章介绍了一种CSS3属性:mix-blend-mode,它可以实现文字智能适配背景颜色和文字镂空效果。通过设置mix-blend-mode为difference,可以在黑色背景中显示白色文字,在白色背景中显示黑色文字。同时,文章还提到了mix-blend-mode的其他属性,如multiply、screen等等,感兴趣的读者可以进一步研究。
mix-blend-mode 利用混合模式让文字智能适配背景颜色
|
10月前
|
前端开发
transition和animation的区别
transition和animation的区别
你不知道的css3 transition animation transform
你不知道的css3 transition animation transform
Core Animation - 渐变色CAGradientLayer
Core Animation - 渐变色CAGradientLayer
77 0
|
JavaScript 前端开发
一个dark mode的动画
一个dark mode的动画
107 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.
108 0
Mysterious Light——AT
|
前端开发 JavaScript API
Qt-QML-给我的导航条写一个动画-State-Transition
上篇中,我已经写出一个导航条的,虽然太丑了,不过功能是有了,这次我将要给我的导航条加一个动画,先看下演示效果
138 0
Qt-QML-给我的导航条写一个动画-State-Transition
MPAndroidChart 教程:设置颜色 Setting Colors
从v1.4.0版本开始,不再需要(不建议使用)在先前版本中负责设置颜色的ColorTemplate对象。然而,它仍然保留所有预定义的颜色数组(例如,ColorTemplate.VORDIPLOM_COLORS并提供方便的方法,用于将颜色从资源(资源整数)转换为“真实”颜色。
1508 0