用css實現(xiàn)轉動的陰陽太極圖

問:如何用css和html畫出旋轉的陰陽太極圖
需要的知識
1.div標簽的運用 2.id選擇器,后代選擇器, 3.簡單的css樣式長,寬,高,背景顏色,浮動,絕對定位,邊框弧度 4.div的布局特點
靜態(tài)的完成效果
開始
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;來確定位置后調(diào)試 6.第27行代碼為讓八卦轉起來在靜態(tài)八卦中不起作用
讓八卦轉起來
/*先定義一個@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){ }來設置移動端的樣式。
(其中設置的樣式為當在移動端的時候所展現(xiàn)的樣式,沒有在其中設置的樣式默認使用客戶端的樣式)
移動端完整代碼
<!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真是神秘~
針對這個題目,你的解決方案又是什么呢?
不妨在下面的留言給出,學習共勉下~
碼字不易,走過路過來個贊可否??先謝謝了!
ε=ε=ε=┏(゜ロ゜;)┛
最后
?有疑問的同學 歡迎 評論區(qū)討論,也歡迎大家加入我的前端技術交流群 來討論。搜索《前端陽光》公眾號,回復加群吧!
評論
圖片
表情


