1.實(shí)現(xiàn)效果
2.實(shí)現(xiàn)原理
2.1 box-shadow
box-shadow屬性可以設(shè)置一個或多個下拉陰影的框给涕。
boxShadow 屬性把一個或多個下拉陰影添加到框上。該屬性是一個用逗號分隔陰影的列表额获,每個陰影由 2-4 個長度值够庙、一個可選的顏色值和一個可選的 inset 關(guān)鍵字來規(guī)定。省略長度的值是 0抄邀。
語法:
box-shadow: h-shadow v-shadow blur spread color inset;
值 | 說明 |
---|---|
h-shadow | 必需的耘眨。水平陰影的位置。允許負(fù)值 |
v-shadow | 必需的境肾。垂直陰影的位置剔难。允許負(fù)值 |
blur | 可選胆屿。模糊距離 |
spread | 可選。陰影的大小 |
color | 可選偶宫。陰影的顏色非迹。在CSS顏色值尋找顏色值的完整列表 |
inset | 可選。從外層的陰影(開始時(shí))改變陰影內(nèi)側(cè)陰影 |
eg:
div{
width: 200px;
height: 100px;
background: #222;
box-shadow:
0 0 0 6px #881515,
0 0 0 10px #ceb0b0,
0 0 0 15px #b0bace,
0 2px 5px 10px #c7e0c7 inset;
}
2.2 CSS radial-gradient() 函數(shù)
radial-gradient() 函數(shù)用徑向漸變創(chuàng)建 "圖像"纯趋。
徑向漸變由中心點(diǎn)定義憎兽。為了創(chuàng)建徑向漸變你必須設(shè)置兩個終止色。
語法:
background:#000;//瀏覽器不支持時(shí)候展示
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
值 | 描述 |
---|---|
shape | 確定圓的類型: ellipse (默認(rèn)): 指定橢圓形的徑向漸變吵冒。 circle :指定圓形的徑向漸變 |
size | 定義漸變的大小纯命,可能值: farthest-corner (默認(rèn)) : 指定徑向漸變的半徑長度為從圓心到離圓心最遠(yuǎn)的角需曾。 closest-side :指定徑向漸變的半徑長度為從圓心到離圓心最近的邊涩禀。 closest-corner : 指定徑向漸變的半徑長度為從圓心到離圓心最近的角。 farthest-side :指定徑向漸變的半徑長度為從圓心到離圓心最遠(yuǎn)的邊 |
position | 定義漸變的位置杆兵【景ⅲ可能值: center(默認(rèn)):設(shè)置中間為徑向漸變圓心的縱坐標(biāo)值疗我。 top:設(shè)置頂部為徑向漸變圓心的縱坐標(biāo)值。 bottom:設(shè)置底部為徑向漸變圓心的縱坐標(biāo)值图甜。 |
start-color, ..., last-color | 用于指定漸變的起止顏色。 |
repeating-radial-gradient() 函數(shù)用于創(chuàng)建重復(fù)的徑向漸變 "圖像"鳖眼。
eg:
div {
width: 200px;
height: 200px;
border-radius: 50%;
border: 1px solid #222;
background: radial-gradient(circle at 50% 50%, rgba(211, 28, 28) 5%, rgba(235, 178, 21) 30%, rgba(61, 132, 199) 50%);
}
2.3 CSS3 transform:rotate
2.3.1 rotate(2D)
rotate(angle) 定義 2D 旋轉(zhuǎn)黑毅,在參數(shù)中規(guī)定角度。在一個給定度數(shù)順時(shí)針旋轉(zhuǎn)的元素钦讳。負(fù)值是允許的矿瘦,這樣是元素逆時(shí)針旋轉(zhuǎn)。
eg:
div{
transform: rotate(30deg);
}
2.3.2 rotateX(3D)
rotateX:CSS3 允許您使用 3D 轉(zhuǎn)換來對元素進(jìn)行格式化愿卒。rotateX()方法缚去,圍繞其在一個給定度數(shù)X軸旋轉(zhuǎn)的元素。
eg:
div{
transform: rotateX(30deg);
}
2.3.3 rotateY(3D)
rotateY:CSS3 允許您使用 3D 轉(zhuǎn)換來對元素進(jìn)行格式化琼开。圍繞其在一個給定度數(shù)Y軸旋轉(zhuǎn)的元素易结。
eg:
div{
transform: rotateY(30deg);
}
2.3.4 rotateZ(3D)
rotateZ:CSS3 允許您使用 3D 轉(zhuǎn)換來對元素進(jìn)行格式化。定義沿 Z 軸的 3D 旋轉(zhuǎn)柜候。rotateZ(a) 相當(dāng)于 rotate(a) or rotate3d(0, 0, 1, a)搞动。
eg:
div{
transform: rotateZ(30deg);
}
2.3.5 rotate3d(3D)
rotate3d(x,y,z,angle) :定義 3D 旋轉(zhuǎn)。
eg:
div{
transform: rotate3d(1,1,1,30deg);
}
2.4 less的使用
Less (Leaner Style Sheets 的縮寫) 是一門向后兼容的 CSS 擴(kuò)展語言渣刷。這里呈現(xiàn)的是 Less 的官方文檔(中文版)鹦肿,包含了 Less 語言以及利用 JavaScript 開發(fā)的用于將 Less 樣式轉(zhuǎn)換成 CSS 樣式的 Less.js 工具。
因?yàn)?Less 和 CSS 非常像辅柴,因此很容易學(xué)習(xí)箩溃。而且 Less 僅對 CSS 語言增加了少許方便的擴(kuò)展瞭吃,這就是 Less 如此易學(xué)的原因之一。
在 Node.js 環(huán)境中使用 Less :
npm install -g less
在瀏覽器環(huán)境中使用 Less :
<link rel="stylesheet/less" type="text/css" href="styles.less" />
<script src="https://cdn.jsdelivr.net/npm/less@4" ></script>
2.4.1變量(Variables)
@width: 10px;
@height: @width + 10px;
#header {
width: @width;
height: @height;
}
編譯為:
#header {
width: 10px;
height: 20px;
}
2.4.2 嵌套(Nesting)
Less 提供了使用嵌套(nesting)代替層疊或與層疊結(jié)合使用的能力涣旨。假設(shè)我們有以下 CSS 代碼:
#header {
color: black;
}
#header .navigation {
font-size: 12px;
}
#header .logo {
width: 300px;
}
用 Less 語言我們可以這樣書寫代碼:
#header {
color: black;
.navigation {
font-size: 12px;
}
.logo {
width: 300px;
}
}
用 Less 書寫的代碼更加簡潔歪架,并且模仿了 HTML 的組織結(jié)構(gòu)。
2.4.3 混合(Mixins)
混合(Mixin)是一種將一組屬性從一個規(guī)則集包含(或混入)到另一個規(guī)則集的方法开泽。假設(shè)我們定義了一個類(class)如下:
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
如果我們希望在其它規(guī)則集中使用這些屬性呢牡拇?沒問題,我們只需像下面這樣輸入所需屬性的類(class)名稱即可穆律,如下所示:
#menu a {
color: #111;
.bordered();
}
.post a {
color: red;
.bordered();
}
.bordered 類所包含的屬性就將同時(shí)出現(xiàn)在 #menu a 和 .post a 中了惠呼。
2.4.4 extend
帶擴(kuò)展的選擇器插值,Extend無法將選擇器與變量匹配峦耘。如果選擇器包含變量剔蹋,擴(kuò)展將忽略它。但是辅髓,extend 可以附加到插值選擇器泣崩。
減小 CSS 大小
Mixins 將所有屬性復(fù)制到選擇器中,這可能導(dǎo)致不必要的重復(fù)洛口。因此矫付,您可以使用 extends 而不是 mixins 將選擇器向上移動到您希望使用的屬性,從而減少生成的 CSS第焰。
示例 - 使用 mixin:
.my-inline-block() {
display: inline-block;
font-size: 0;
}
.thing1 {
.my-inline-block;
}
.thing2 {
.my-inline-block;
}
輸出
.thing1 {
display: inline-block;
font-size: 0;
}
.thing2 {
display: inline-block;
font-size: 0;
}
示例(帶有擴(kuò)展):
.my-inline-block {
display: inline-block;
font-size: 0;
}
.thing1 {
&:extend(.my-inline-block);
}
.thing2 {
&:extend(.my-inline-block);
}
輸出
.my-inline-block,
.thing1,
.thing2 {
display: inline-block;
font-size: 0;
}
3.實(shí)現(xiàn)步驟
-
畫一個圓,為其添加偽元素买优,before設(shè)置box-shadow為圓添加立體感,after利用徑向漸變及rotate3d一些操作實(shí)現(xiàn)圓陰影挺举,整體設(shè)置perspective透視1200px杀赢。
image.png
<div class="ball"></div>
以下為less:
.i-b {
display: inline-block;
}
.p-3d {
transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
}
.ball {
&:extend(.i-b);
// Extend 是一個 Less 偽類,它將它所放置的選擇器與匹配它所引用的選擇器合并湘纵。
width: 200px;
height: 200px;
margin: 0;
border-radius: 50%;
position: relative;
background: #e6be74;
perspective: 1200px;
perspective-origin: 50% 50%;
&:extend(.p-3d);
&:before {
content: "";
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
border-radius: 50%;
box-shadow: -40px 10px 70px 10px rgba(0, 0, 0, 0.5) inset;
z-index: 2;
}
&:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
background: radial-gradient(circle at 50% 50%, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0) 50%);
.trans-all(translateX(64px) rotateX(90deg) translateZ(-113px) rotateY(-30deg));
z-index: -1;
}
}
.trans-all(@content) {
-webkit-transform: @content;
-moz-transform: @content;
-ms-transform: @content;
-o-transform: @content;
transform: @content;
}
-
畫出第一道軌跡線(rotateZ+rotateY設(shè)置圓角度傾斜)脂崔。
image.png
<div class="circle-line1"></div>
.circle-line1 {
.create-circleLine(200px, 200px);
}
.line {
border-radius: 50%;
position: absolute;
border: 3px solid #5c5c6d;
transform: rotateZ(60deg) rotateY(105deg);
-webkit-transform: rotateZ(60deg) rotateY(105deg);
&:extend(.p-3d);
}
.create-circleLine(@a, @b) {
width: @a*2 ;
height: @b*2;
left: 50%;
top: 50%;
margin-left: -@a;
margin-top: -@b;
&:extend(.line);
}
-
為第一道軌跡線設(shè)置前后偽元素,before和after設(shè)置圓點(diǎn)梧喷。
image.png
.circle-line1 {
.create-circleLine(200px, 200px);
&::before {
.create-circle(10px, 10px, #d87093, move);
transform: rotateZ(0deg) translateX(200px) rotateZ(-0deg) rotateY(-105deg);
}
&::after {
.create-circle(10px, 10px, #ad4eda, move4);
transform: rotateZ(180deg) translateX(200px) rotateZ(-180deg) rotateY(-105deg);
}
}
.dot-center {
border-radius: 50%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
.create-circle(@a, @b, @color, @aname) {
content: '';
width: @a;
height: @b;
background: @color;
filter: drop-shadow(10px 10px 10px @color);
animation: @aname 20s linear infinite;
&:extend(.dot-center);
}
-
設(shè)置第一道軌跡線設(shè)置前后偽元素轉(zhuǎn)動動畫砌左。
7.gif
.circle-line1 {
.create-circleLine(200px, 200px);
&::before {
.create-circle(10px, 10px, #d87093, move);
transform: rotateZ(0deg) translateX(200px) rotateZ(-0deg) rotateY(-105deg);
.create-keyframes(move, 0deg, 360deg, 200px, 105deg);
}
&::after {
.create-circle(10px, 10px, #ad4eda, move4);
transform: rotateZ(180deg) translateX(200px) rotateZ(-180deg) rotateY(-105deg);
.create-keyframes(move4, -180deg, 180deg, 200px, 105deg);
}
}
.create-keyframes(@name, @z, @z1, @radius, @y) {
@keyframes @name {
.create-animation(@z, @z1, @radius, @y)
}
}
.create-animation(@z, @z1, @radius, @y) {
from {
transform: rotateZ(@z) translateX(@radius) rotateZ(-@z) rotateY(-@y);
}
to {
transform: rotateZ(@z1) translateX(@radius) rotateZ(-@z1) rotateY(-@y);
}
}
- 添加第二道,第三道軌跡铺敌,方法與上述內(nèi)容一致绊困,區(qū)別在于圓半徑不同。
4.完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>地球繞圈</title>
</head>
<link rel="stylesheet" href="../../common.css">
<link rel="stylesheet/less" href="./raoqiu.less" />
<body>
<div class="ball">
<div class="circle-line1"></div>
<div class="circle-line2"></div>
<div class="circle-line3"></div>
</div>
</body>
<script src="../../less.1.7.3.js"></script>
</html>
body {
background: radial-gradient(ellipse at bottom, #1b2735 0%, #090a0f 100%);
}
.i-b {
display: inline-block;
}
.p-3d {
transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
}
.ball {
&:extend(.i-b);
width: 200px;
height: 200px;
margin: 0;
border-radius: 50%;
position: relative;
background: #e6be74;
perspective: 1200px;
perspective-origin: 50% 50%;
&:extend(.p-3d);
&:before {
content: "";
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
border-radius: 50%;
box-shadow: -40px 10px 70px 10px rgba(0, 0, 0, 0.5) inset;
z-index: 2;
}
&:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
background: radial-gradient(circle at 50% 50%, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0) 50%);
.trans-all(translateX(64px) rotateX(90deg) translateZ(-113px) rotateY(-30deg));
z-index: -1;
}
}
.trans-all(@content) {
-webkit-transform: @content;
-moz-transform: @content;
-ms-transform: @content;
-o-transform: @content;
transform: @content;
}
.line {
border-radius: 50%;
position: absolute;
border: 3px solid #5c5c6d;
transform: rotateZ(60deg) rotateY(105deg);
-webkit-transform: rotateZ(60deg) rotateY(105deg);
&:extend(.p-3d);
}
.create-circleLine(@a, @b) {
width: @a*2 ;
height: @b*2;
left: 50%;
top: 50%;
margin-left: -@a;
margin-top: -@b;
&:extend(.line);
}
.dot-center {
border-radius: 50%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
.create-circle(@a, @b, @color, @aname) {
content: '';
width: @a;
height: @b;
background: @color;
filter: drop-shadow(10px 10px 10px @color);
animation: @aname 20s linear infinite;
&:extend(.dot-center);
}
.create-keyframes(@name, @z, @z1, @radius, @y) {
@keyframes @name {
.create-animation(@z, @z1, @radius, @y)
}
}
.create-animation(@z, @z1, @radius, @y) {
from {
transform: rotateZ(@z) translateX(@radius) rotateZ(-@z) rotateY(-@y);
}
to {
transform: rotateZ(@z1) translateX(@radius) rotateZ(-@z1) rotateY(-@y);
}
}
.circle-line1 {
.create-circleLine(200px, 200px);
&::before {
.create-circle(10px, 10px, #d87093, move);
transform: rotateZ(0deg) translateX(200px) rotateZ(-0deg) rotateY(-105deg);
.create-keyframes(move, 0deg, 360deg, 200px, 105deg);
}
&::after {
.create-circle(10px, 10px, #ad4eda, move4);
transform: rotateZ(180deg) translateX(200px) rotateZ(-180deg) rotateY(-105deg);
.create-keyframes(move4, -180deg, 180deg, 200px, 105deg);
}
}
.circle-line2 {
.create-circleLine(250px, 250px);
&::before {
.create-circle(10px, 10px, #87ceeb, move2);
.create-keyframes(move2, 0deg, 360deg, 250px, 105deg);
}
&::after {
.create-circle(10px, 10px, #48dbbb, move5);
.create-keyframes(move5, -180deg, 180deg, 250px, 105deg);
}
}
.circle-line3 {
.create-circleLine(160px, 160px);
&::before {
.create-circle(10px, 10px, #e44818, move3);
.create-keyframes(move3, 0deg, 360deg, 160px, 105deg);
}
&::after {
.create-circle(10px, 10px, #4396ce, move6);
.create-keyframes(move6, -180deg, 180deg, 160px, 105deg);
}
}