CSS

用CSS3绘制旋转八卦

Posted by deft on April 27, 2020

绘制旋转八卦

今天我们来运用CSS里面的animation属性来制作一个会旋转的八卦吧!

首先,创建一个div绘制一个400*400黑白各占一半的圆居中显示,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
div {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
box-sizing: border-box;
width: 400px;
height: 400px;
margin: auto;
border: 1px solid #000;
border-bottom: 200px solid #000;
border-radius: 50%;
transform-origin: 50% 50%;
animation-name: rotate;
animation-duration: 5s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}

然后运用我们的伪类属性:before、:after,给我们的八卦中间添上两个圆环。第一个我们像这样子:

div::before { content: ''; display:block; width: 20px; height: 20px; border: 90px solid #000; background-color: #fff; border-radius: 50%; position: absolute; top:100px; }

同理:

div::after { content: ''; display:block; width: 20px; height: 20px; border: 90px solid #fff; background-color: #000; border-radius: 50%; position: absolute; top:100px; left: 200px; }

然后,我们用@keyframes再让它动起来:

@keyframes rotate { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

咦~?! 我们发现大圆边缘有些被遮住了,可以适当把div里面的宽高属性调整到402px

div { position: absolute; top: 0; right: 0; bottom: 0; left: 0; box-sizing: border-box; width: 402px; height: 402px; ....

然后就完成啦~是不是很简单。