用css實(shí)現(xiàn)轉(zhuǎn)動(dòng)的陰陽(yáng)太極圖

問:如何用css和html畫出旋轉(zhuǎn)的陰陽(yáng)太極圖
需要的知識(shí)
1.div標(biāo)簽的運(yùn)用 2.id選擇器,后代選擇器, 3.簡(jiǎn)單的css樣式長(zhǎng),寬,高,背景顏色,浮動(dòng),絕對(duì)定位,邊框弧度 4.div的布局特點(diǎn)
靜態(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>太極頁(yè)面</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部分說明
通過改變每個(gè)div的大小顏色,浮動(dòng)的left,top值,改變每個(gè)div的border-redius的值來畫這個(gè)八卦圖
<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>太極頁(yè)面</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>
注意事項(xiàng)
1.id可以用中文 ,但是文件名和路徑不能(最好不要) 2.一定要注意清楚瀏覽其的默認(rèn)樣式 3.能不寫死就千萬(wàn)不要寫死,最好用百分比代替像素(防止用戶改變分辨率造成的bug) 4.父元素relative,子元素absolute 5.注意幾個(gè)div的偏移位置,可以加boder: 1px,solid,red;來確定位置后調(diào)試 6.第27行代碼為讓八卦轉(zhuǎn)起來在靜態(tài)八卦中不起作用
讓八卦轉(zhuǎn)起來
/*先定義一個(gè)@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);
}
轉(zhuǎn)動(dòng)的八卦完整代碼
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>太極頁(yè)面</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>
拓展:手機(jī)端的轉(zhuǎn)動(dòng)的八卦
在原代碼不變的情況下使用
@media(max-width:500px){ }來設(shè)置移動(dòng)端的樣式。
(其中設(shè)置的樣式為當(dāng)在移動(dòng)端的時(shí)候所展現(xiàn)的樣式,沒有在其中設(shè)置的樣式默認(rèn)使用客戶端的樣式)
移動(dòng)端完整代碼
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>太極頁(yè)面</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真是神秘~
針對(duì)這個(gè)題目,你的解決方案又是什么呢?
不妨在下面的留言給出,學(xué)習(xí)共勉下~
碼字不易,走過路過來個(gè)贊可否??先謝謝了!
ε=ε=ε=┏(゜ロ゜;)┛
最后
?有疑問的同學(xué) 歡迎 評(píng)論區(qū)討論,也歡迎大家加入我的前端技術(shù)交流群 來討論。搜索《前端陽(yáng)光》公眾號(hào),回復(fù)加群吧!
評(píng)論
圖片
表情


