0
点赞
收藏
分享

微信扫一扫

有趣的CSS - 旋转的太极图

大家好,我是 Just,这里是「设计师工作日常」,今天分享的是用css画一个旋转的太极图。

《有趣的css》系列最新实例通过公众号「设计师工作日常」发布。

(目录)

整体效果

使用 :before:after 伪元素以及 animation 属性画一个顺时针旋转的太极图。

这个实例可以帮助你更好的了解和使用 css 中的伪元素。

核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

核心代码

html 代码

<div class="taiji">
  <div class="yu"></div>
</div>

两个 div 标签,分别用作绘制整体大圆以及阴阳鱼。

css 部分代码

.taiji{
  width: 200px;
  height: 200px;
  position: relative;
  border-radius: 50%;
  box-shadow: 0px -10px 20px 0px #2AF598, 0px 10px 20px 0px #08AEEA;
  animation: zhuan 5s linear infinite;  /*设置旋转动画*/
}
.taiji:before,.taiji:after{
  content: '';
  width: 200px;
  height: 100px;
  position: absolute;
  background-color: #fff;
  border-radius: 100px 100px 0 0;
}
.taiji:after{
  top: 100px;
  background-color: #000;
  border-radius: 0 0 100px 100px;
}
.yu:before,.yu:after{
  content: '';
  width: 24px;
  height: 24px;
  position: absolute;
  top: 50px;
  left: 100px;
  border-radius: 50%;
  background-color: #000;
  border: 38px solid #fff;
  z-index: 1;
}
.yu:after{
  left: 0;
  background-color: #fff;
  border: 38px solid #000;
}
@keyframes zhuan{
  to {
    transform: rotate(360deg);
  }
}

.taiji:before.taiji:after 伪元素分别绘制黑白阴阳鱼的主体,.yu:before.yu:after 伪元素分别绘制黑白阴阳鱼的小鱼眼,然后设置旋转动画,顺时针旋转。

完整代码如下

html 页面

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>旋转的太极图</title>
  </head>
  <body>
    <div class="app">
      <div class="taiji">
        <div class="yu"></div>
      </div>
    </div>
  </body>
</html>

css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #e8e8e8;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.taiji{
  width: 200px;
  height: 200px;
  position: relative;
  border-radius: 50%;
  box-shadow: 0px -10px 20px 0px #2AF598, 0px 10px 20px 0px #08AEEA;
  animation: zhuan 5s linear infinite;
}
.taiji:before,.taiji:after{
  content: '';
  width: 200px;
  height: 100px;
  position: absolute;
  background-color: #fff;
  border-radius: 100px 100px 0 0;
}
.taiji:after{
  top: 100px;
  background-color: #000;
  border-radius: 0 0 100px 100px;
}
.yu:before,.yu:after{
  content: '';
  width: 24px;
  height: 24px;
  position: absolute;
  top: 50px;
  left: 100px;
  border-radius: 50%;
  background-color: #000;
  border: 38px solid #fff;
  z-index: 1;
}
.yu:after{
  left: 0;
  background-color: #fff;
  border: 38px solid #000;
}
@keyframes zhuan{
  to {
    transform: rotate(360deg);
  }
}

页面渲染效果

以上就是所有代码,以及简单的思路,希望对你有一些帮助或者启发。

我是 Just,这里是「设计师工作日常」,求点赞求关注!

举报

相关推荐

0 条评论