用css实现转动的阴阳太极图
前端阳光
共 40762字,需浏览 82分钟
· 2021-03-23
问:如何用css和html画出旋转的阴阳太极图
需要的知识
1.div标签的运用 2.id选择器,后代选择器, 3.简单的css样式长,宽,高,背景颜色,浮动,绝对定位,边框弧度 4.div的布局特点
静态的完成效果
开始
HTML部分
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>太极页面</title>
</head>
<body>
<div id="八卦-wrapper">
<div id="八卦">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="八卦-描述">道可道,非常道</div>
</div>
</body>
</html>
HTML部分说明(不要吐槽中文id哈哈哈,纯属为了方便讲解)
1.其中最外面的id为八卦-wrapper为包住八卦的最大的div 2.id 为八卦的div为上图中的八卦 3.八卦里面的div为 下图指示部分
CSS部分
CSS部分说明
通过改变每个div的大小颜色,浮动的left,top值,改变每个div的border-redius的值来画这个八卦图
<style>
* {
box-sizing: border-box;
margin: 0px;
padding: 0px;
}
body {
background-color: #eeeeee;
}
#八卦 {
width: 400px;
height: 400px;
border-radius: 200px;
position: relative;
overflow: hidden;
animation: x 10s linear infinite;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 1);
}
#八卦>div:first-child {
width: 50%;
height: 100%;
position: absolute;
left: 0;
background-color: black;
}
#八卦>div:nth-child(2) {
width: 50%;
height: 100%;
position: absolute;
right: 0;
background-color: white;
}
#八卦>div:nth-child(3) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: black;
}
#八卦>div:nth-child(4) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: white;
}
#八卦>div:nth-child(5) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
top: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: white;
}
#八卦>div:nth-child(6) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
bottom: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: black;
}
#八卦-wrapper {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#八卦-描述 {
margin-top: 1em;
font-size: 3em;
}
</style>
完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>太极页面</title>
<style>
* {
box-sizing: border-box;
margin: 0px;
padding: 0px;
}
body {
background-color: #eeeeee;
}
#八卦 {
width: 400px;
height: 400px;
border-radius: 200px;
position: relative;
overflow: hidden;
animation: x 10s linear infinite;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 1);
}
#八卦>div:first-child {
width: 50%;
height: 100%;
position: absolute;
left: 0;
background-color: black;
}
#八卦>div:nth-child(2) {
width: 50%;
height: 100%;
position: absolute;
right: 0;
background-color: white;
}
#八卦>div:nth-child(3) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: black;
}
#八卦>div:nth-child(4) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: white;
}
#八卦>div:nth-child(5) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
top: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: white;
}
#八卦>div:nth-child(6) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
bottom: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: black;
}
#八卦-wrapper {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#八卦-描述 {
margin-top: 1em;
font-size: 3em;
}
</style>
</head>
<body>
<div id="八卦-wrapper">
<div id="八卦">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="八卦-描述">道可道,非常道</div>
</div>
</body>
</html>
注意事项
1.id可以用中文 ,但是文件名和路径不能(最好不要) 2.一定要注意清楚浏览其的默认样式 3.能不写死就千万不要写死,最好用百分比代替像素(防止用户改变分辨率造成的bug) 4.父元素relative,子元素absolute 5.注意几个div的偏移位置,可以加boder: 1px,solid,red;来确定位置后调试 6.第27行代码为让八卦转起来在静态八卦中不起作用
让八卦转起来
/*先定义一个@keyframes 命名为x*/
@keyframes x {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
/*在id="八卦"中添加样式 animation: x 10s linear infinite;*/
#八卦 {
width: 400px;
height: 400px;
border-radius: 200px;
position: relative;
overflow: hidden;
animation: x 10s linear infinite;/*添加此行*/
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 1);
}
转动的八卦完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>太极页面</title>
<style>
* {
box-sizing: border-box;
margin: 0px;
padding: 0px;
}
body {
background-color: #eeeeee;
}
@keyframes x {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
#八卦 {
width: 400px;
height: 400px;
border-radius: 200px;
position: relative;
overflow: hidden;
animation: x 10s linear infinite;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 1);
}
#八卦>div:first-child {
width: 50%;
height: 100%;
position: absolute;
left: 0;
background-color: black;
}
#八卦>div:nth-child(2) {
width: 50%;
height: 100%;
position: absolute;
right: 0;
background-color: white;
}
#八卦>div:nth-child(3) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: black;
}
#八卦>div:nth-child(4) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: white;
}
#八卦>div:nth-child(5) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
top: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: white;
}
#八卦>div:nth-child(6) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
bottom: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: black;
}
#八卦-wrapper {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#八卦-描述 {
margin-top: 1em;
font-size: 3em;
}
</style>
</head>
<body>
<div id="八卦-wrapper">
<div id="八卦">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="八卦-描述">道可道,非常道</div>
</div>
</body>
</html>
拓展:手机端的转动的八卦
在原代码不变的情况下使用
@media(max-width:500px){ }
来设置移动端的样式。
(其中设置的样式为当在移动端的时候所展现的样式,没有在其中设置的样式默认使用客户端的样式)
移动端完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>太极页面</title>
<style>
* {
box-sizing: border-box;
margin: 0px;
padding: 0px;
}
body {
background-color: #eeeeee;
}
@keyframes x {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
#八卦 {
width: 400px;
height: 400px;
border-radius: 200px;
position: relative;
overflow: hidden;
animation: x 10s linear infinite;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 1);
}
@media (max-width:500px) {
#八卦 {
width: 200px;
height: 200px;
}
}
#八卦>div:first-child {
width: 50%;
height: 100%;
position: absolute;
left: 0;
background-color: black;
}
#八卦>div:nth-child(2) {
width: 50%;
height: 100%;
position: absolute;
right: 0;
background-color: white;
}
#八卦>div:nth-child(3) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: black;
}
@media (max-width:500px) {
#八卦>div:nth-child(3) {
width: 100px;
height: 100px;
margin-left: -50px;
}
}
#八卦>div:nth-child(4) {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
border-radius: 50%;
background-color: white;
}
@media (max-width:500px) {
#八卦>div:nth-child(4) {
width: 100px;
height: 100px;
margin-left: -50px;
}
}
#八卦>div:nth-child(5) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
top: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: white;
}
@media (max-width:500px) {
#八卦>div:nth-child(5) {
width: 25px;
height: 25px;
top: 37.5px;
margin-left: -25px;
}
}
#八卦>div:nth-child(6) {
width: 50px;
height: 50px;
position: absolute;
left: 50%;
bottom: 75px;
margin-left: -50px;
border-radius: 50%;
background-color: black;
}
@media (max-width:500px) {
#八卦>div:nth-child(6) {
width: 25px;
height: 25px;
bottom: 37.5px;
margin-left: -25px;
}
}
#八卦-wrapper {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#八卦-描述 {
margin-top: 1em;
font-size: 3em;
}
@media (max-width:500px) {
#八卦-描述 {
margin-top: 0.5em;
font-size: 1.5em;
}
}
</style>
</head>
<body>
<div id="八卦-wrapper">
<div id="八卦">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="八卦-描述">道可道,非常道</div>
</div>
</body>
</html>
CSS真是神秘~
针对这个题目,你的解决方案又是什么呢?
不妨在下面的留言给出,学习共勉下~
码字不易,走过路过来个赞可否👍先谢谢了!
ε=ε=ε=┏(゜ロ゜;)┛
最后
❝有疑问的同学 欢迎 评论区讨论,也欢迎大家加入我的前端技术交流群 来讨论。搜索《前端阳光》公众号,回复加群吧!
评论
如何画出漂亮的神经网络图?神经网络可视化工具集锦搜集
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达1. draw_convnet一个用于画卷积神经网络的Python脚本https://github.com/gwding/draw_convnet2. NNSVGhttp://alexlenail.me/NN-SVG/LeNet.
小白学视觉
0
三个优秀的PyTorch实现语义分割框架
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达转自 | 机器学习AI算法工程使用的VOC数据集链接开放在文章中,预训练模型已上传Github,环境我使用Colab pro,大家下载模型做预测即可。代码链接: https://github.com/lixiang007
小白学视觉
0
如何看待稚晖君的时间管理水平?
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达计算机视觉life整理原文链接:https://www.zhihu.com/question/491456524/answer/2183081310稚晖君究竟是如何安排业余时间去做这么多高水平的项目?而且每个项目的用时也很少,普通
小白学视觉
2
如何确定神经网络的层数和隐藏层神经元数量?
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达公众号:尤而小屋作者:呦呦鹿鸣编辑:Peter大家好,我是Peter~关于神经网络中隐藏层的层数和神经元个数充满了疑惑。刚好看到一篇文章很好地答疑了,分享给大家~https://zhuanlan.zhihu.com/p/10041
小白学视觉
0
“海优最初弄出来可并不是为了给大佬弟子开后门的, 学术界现在几乎不存在任何套利空间,不要相信有什么办法可以投机取巧”
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达知乎问题:为什么部分同学倾向土博+国外博后而不是直接国外读博?感觉身边土博越来越多,基本都是土博➕海博后,申国外phd的明显减少(相比较疫情之前几年)知乎Tianteman: 这个问题下,可以看得出一部分国内硕博信息检索能力差的离
小白学视觉
0
神经网络之CNN与RNN的关系
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达1、CNN介绍CNN是一种利用卷积计算的神经网络。它可以通过卷积计算将原像素很大的图片保留主要特征变成很小的像素图片。本文以李宏毅老师ppt内容展开具体介绍。1.1 Why CNN for Image①为什么引入CNN ?图片示意
小白学视觉
0
真高!比亚迪员工爆料比亚迪在越南的薪资水平:基本工资480万,全勤奖35万,交通补助20万,餐补110万,每周6天,每天10小时
上一篇:某大公司为逼迫员工离职,竟然把他的工位安排到厕所旁,没想到他直接开始记录领导的如厕时间,还发到公司大群...对此,你怎么看?--完--PS:欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,欢迎转发分享给更多人。全文完,感谢你的耐心阅读。如果你还想看到我的文章,请一定给本
开发者全社区
0
太敢穿了!透视纱裙!性感火辣的身材
绝了呀今天的厂花:吴宣仪1995年1月26日,吴宣仪出生于海南省海口市,中国内地流行乐女歌手、影视演员。2016年2月,吴宣仪随宇宙少女发行首张迷你专辑正式出道。2018年4月,她参加《创造101》综艺选秀,获得第二名,成功加入火箭少女101组合。吴宣仪的颜值一直备受称赞,她的五官立体精致,皮肤白皙
逆锋起笔
0