本文利用CSS3实现天气的图标,包括晴天,多云,雨天,下雪,打雷,采用了CSS3中的box-shadow,动画特效采用animation。其中主要采用box-shaow属性,感觉这个属性功能还挺强,详细说明见链接:https://www.runoob.com/cssref/css3-pr-box-shadow.html
直接上代码,整体代码比较简单,如何使用的话需要微调位置,这个比较麻烦,需要耐心调。下雪的图标:
整体效果如下所示:
首先HTML代码
<div class="weather">
<div class="container">
<div class="sunny">
<div class="sunny_body"></div>
</div>
<div class="cloudy">
<div class="cloud"></div>
</div>
<div class="rainy">
<div class="rain"></div>
</div>
<div class="snowy">
<div class="snow1"></div>
<div class="snow2"></div>
</div>
<div class="thundery">
<div class="thunder1"></div>
<div class="thunder2"></div>
</div>
</div>
</div>
CSS部分
/*包括晴天,多云,下雨,下雪,打雷*/
.weather {
height: 400px;
width: 100%;
position: relative;
background: rgb(5, 5, 5);
top: 50%;
/* left: 30%; */
}
.container {
position: absolute;
display: flex;
left: 30%;
top: 50%;
}
.sunny {
border-radius: 50%;
content: "";
height: 25px;
width: 25px;
position: absolute;
border: 5px solid #ffffff;
left: 60px;
top: 10px;
-webkit-animation: sunny 15s linear infinite;
animation: sunny 15s linear infinite;
}
.sunny:after {
content: " ";
width: 5px;
height: 20px;
margin: auto;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
transform: rotate(20deg);
border-radius: 15px;
box-shadow: 0px 35px 0 1px #fff, 0 -35px 0 1px #fff;
}
.sunny_body {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: 5px;
height: 20px;
margin: auto;
transform: rotate(80deg);
border-radius: 15px;
box-shadow: 0px 35px 0 1px #fff, 0 -35px 0 1px #fff;
}
.sunny_body:after {
content: " ";
position: absolute;
width: 5px;
height: 20px;
margin: auto;
transform: rotate(60deg);
border-radius: 15px;
box-shadow: 0 35px 0 1px #fff, 0 -35px 0 1px #fff;
}
@-webkit-keyframes sunny {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes sunny {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
/* CLOUDY */
.cloudy {
background: currentColor;
border-radius: 50%;
position: absolute;
left: 90px;
top: 25px;
z-index: 10;
width: 50px;
height: 50px;
margin: -29.5px;
margin-left: 150px;
box-shadow: -35px 11px 0 -8px, 31px 13px 0 -10px, 0 0 0 6px #ffff,
-35px 11px 0 -2px #ffffff, 31px 13px 0 -4px #ffffff;
}
.cloudy:after {
background: currentColor;
content: "";
height: 20px;
width: 65px;
z-index: 10;
position: absolute;
left: -10px;
bottom: -3px;
box-shadow: 0 7px 0 -1px #fff;
}
.cloud {
background: #a19d9d;
border-radius: 50%;
position: absolute;
left: 100px;
top: 25px;
z-index: 10;
width: 25px;
height: 25px;
margin: -29.5px;
box-shadow: -35px 11px 0 -14px #a19d9d, 33px 15px 0 -15px #a19d9d,
0 0 0 6px #a19d9d, -15px 5px 0 1px #a19d9d, 13px 7px 0 0px #a19d9d;
-webkit-animation: cloudy_shadow 10s linear infinite;
animation: cloudy_shadow 10s linear infinite;
}
.cloud:after {
background: #a19d9d;
border-radius: 30%;
content: "";
height: 5px;
width: 20px;
position: absolute;
left: 1px;
z-index: 10;
bottom: -6px;
box-shadow: 0 4px 0 -4px #a19d9d;
}
@-webkit-keyframes cloudy_shadow {
0% {
-webkit-transform: translateX(0px) scale(1);
transform: translateX(0px) scale(1);
opacity: 1;
}
50% {
-webkit-transform: translateX(-80px) scale(1);
transform: translateX(-80px) scale(1);
opacity: 0.2;
}
100% {
-webkit-transform: translateX(-120px) scale(1);
transform: translateX(-120px) scale(1);
opacity: 0.05;
}
}
@keyframes cloudy_shadow {
0% {
-webkit-transform: translateX(0px) scale(1);
transform: translateX(0px) scale(1);
opacity: 1;
}
50% {
-webkit-transform: translateX(-80px) scale(1);
transform: translateX(-80px) scale(1);
opacity: 0.2;
}
100% {
-webkit-transform: translateX(-120px) scale(1);
transform: translateX(-120px) scale(1);
opacity: 0.05;
}
}
/*雨 RAINY */
.rainy {
background: currentColor;
border-radius: 50%;
position: absolute;
left: 90px;
top: 20px;
z-index: 10;
width: 50px;
height: 50px;
margin: -29.5px;
margin-left: 350px;
box-shadow: -35px 11px 0 -8px, 31px 13px 0 -10px, 0 0 0 6px #ffff,
-35px 11px 0 -2px #ffffff, 31px 13px 0 -4px #ffffff;
}
.rainy:after {
background: currentColor;
content: "";
height: 20px;
width: 65px;
z-index: 10;
position: absolute;
left: -10px;
bottom: -3px;
box-shadow: 0 7px 0 -1px #fff;
}
.rain {
position: absolute;
z-index: 20;
top: 50%;
left: 50%;
width: 60px;
height: 60px;
margin: 2px 0 0 -32px;
background: currentColor;
}
.rain:after {
content: "";
position: absolute;
z-index: 30;
top: 50%;
left: 50%;
width: 18px;
height: 18px;
margin: -16px 0 0 -4px;
background: #0cf;
border-radius: 100% 0 60% 50% / 60% 0 100% 50%;
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2),
-14px 18px 0 -2px rgba(255, 255, 255, 0.2),
-22px -2px 0 rgba(255, 255, 255, 0.2);
transform: rotate(-28deg);
animation: rainy_rain 3s linear infinite;
}
@-webkit-keyframes rainy_rain {
0% {
background: #0cf;
box-shadow: 0.625em 0.875em 0 -0.125em rgba(255, 255, 255, 0.2),
-0.875em 1.125em 0 -0.125em rgba(255, 255, 255, 0.2),
-1.375em -0.125em 0 #0cf;
}
25% {
box-shadow: 0.625em 0.875em 0 -0.125em rgba(255, 255, 255, 0.2),
-0.875em 1.125em 0 -0.125em #0cf,
-1.375em -0.125em 0 rgba(255, 255, 255, 0.2);
}
50% {
background: rgba(255, 255, 255, 0.3);
box-shadow: 0.625em 0.875em 0 -0.125em #0cf,
-0.875em 1.125em 0 -0.125em rgba(255, 255, 255, 0.2),
-1.375em -0.125em 0 rgba(255, 255, 255, 0.2);
}
100% {
box-shadow: 0.625em 0.875em 0 -0.125em rgba(255, 255, 255, 0.2),
-0.875em 1.125em 0 -0.125em rgba(255, 255, 255, 0.2),
-1.375em -0.125em 0 #0cf;
}
}
@keyframes rainy_rain {
0% {
background: #0cf;
box-shadow: 0.625em 0.875em 0 -0.125em rgba(255, 255, 255, 0.2),
-0.875em 1.125em 0 -0.125em rgba(255, 255, 255, 0.2),
-1.375em -0.125em 0 #0cf;
}
25% {
box-shadow: 0.625em 0.875em 0 -0.125em rgba(255, 255, 255, 0.2),
-0.875em 1.125em 0 -0.125em #0cf,
-1.375em -0.125em 0 rgba(255, 255, 255, 0.2);
}
50% {
background: rgba(255, 255, 255, 0.3);
box-shadow: 0.625em 0.875em 0 -0.125em #0cf,
-0.875em 1.125em 0 -0.125em rgba(255, 255, 255, 0.2),
-1.375em -0.125em 0 rgba(255, 255, 255, 0.2);
}
100% {
box-shadow: 0.625em 0.875em 0 -0.125em rgba(255, 255, 255, 0.2),
-0.875em 1.125em 0 -0.125em rgba(255, 255, 255, 0.2),
-1.375em -0.125em 0 #0cf;
}
}
/* SNOWY */
.snowy {
background: currentColor;
border-radius: 50%;
position: absolute;
left: 90px;
top: 20px;
z-index: 10;
width: 50px;
height: 50px;
margin: -29.5px;
margin-left: 520px;
box-shadow: -35px 11px 0 -8px, 31px 13px 0 -10px, 0 0 0 6px #ffff,
-35px 11px 0 -2px #ffffff, 31px 13px 0 -4px #ffffff;
}
.snowy:after {
background: currentColor;
content: "";
height: 20px;
width: 65px;
z-index: 10;
position: absolute;
left: -10px;
bottom: -3px;
box-shadow: 0 7px 0 -1px #fff;
}
.snow1 {
position: absolute;
z-index: 20;
top: 50%;
left: 50%;
width: 60px;
height: 60px;
margin: 6px 0 0 -32px;
background: currentColor;
}
.snow1:after {
content: "";
position: absolute;
z-index: 30;
top: 50%;
left: 50%;
width: 18px;
height: 18px;
margin: -16px 0 0 4px;
background: url("./image/snow.png") no-repeat center;
background-size: 100% 100%;
animation: snowy 8s linear infinite;
}
.snow1:before {
content: "";
position: absolute;
z-index: 30;
top: 50%;
left: 50%;
width: 28px;
height: 28px;
margin: 0px 0 0 3px;
background: url("./image/snow.png") no-repeat center;
background-size: 100% 100%;
animation: snowy2 8s linear infinite;
}
.snow2:after {
content: "";
position: absolute;
z-index: 30;
top: 50%;
left: 50%;
width: 14px;
height: 14px;
margin: 15px 0 0 -16px;
background: url("./image/snow.png") no-repeat center;
background-size: 100% 100%;
}
.snow2:before {
content: "";
position: absolute;
z-index: 30;
top: 50%;
left: 50%;
width: 24px;
height: 24px;
margin: 30px 0 0 -30px;
background: url("./image/snow.png") no-repeat center;
background-size: 100% 100%;
animation: snowy 8s linear infinite;
}
@-webkit-keyframes snowy {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(-360deg);
transform: rotate(360deg);
}
}
@keyframes snowy {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@-webkit-keyframes snowy2 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(-360deg);
transform: rotate(-360deg);
}
}
@keyframes snowy2 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(-360deg);
transform: rotate(-360deg);
}
}
/* 雷阵雨*/
.thundery {
background: currentColor;
border-radius: 50%;
position: absolute;
left: 90px;
top: 20px;
z-index: 10;
width: 50px;
height: 50px;
margin: -29.5px;
margin-left: 700px;
box-shadow: -35px 11px 0 -8px, 31px 13px 0 -10px, 0 0 0 6px #ffff,
-35px 11px 0 -2px #ffffff, 31px 13px 0 -4px #ffffff;
}
.thundery:after {
background: currentColor;
content: "";
height: 20px;
width: 65px;
z-index: -10;
position: absolute;
left: -10px;
bottom: -3px;
box-shadow: 0 7px 0 -1px currentColor;
}
.thunder1 {
position: absolute;
z-index: 50;
top: 50%;
left: 50%;
margin: 1px 0 0 -20px;
background: currenretColor;
border-top: 14px solid currentColor;
border-right: 8px solid rgba(255, 255, 255, 0.2);
border-bottom: 14px solid rgba(255, 255, 255, 0.2);
border-left: 8px solid currentColor;
transform: skewX(-15deg);
animation: thunder_lightning 3s linear infinite;
}
.thunder1:before {
content: "";
position: absolute;
z-index: 30;
top: 50%;
left: 50%;
background: currentColor;
margin: 8px 0 0 8px;
border-top: 14px solid rgba(255, 255, 255, 0.2);
border-right: 8px solid currentColor;
border-bottom: 14px solid currentColor;
border-left: 8px solid rgba(255, 255, 255, 0.2);
animation: thunder_lightning 3s linear infinite;
}
.thunder2 {
position: absolute;
z-index: 50;
top: 140%;
left: 40%;
width: 18px;
height: 18px;
margin: -16px 0 0 -4px;
border-radius: 100% 0 60% 50% / 60% 0 100% 50%;
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2);
transform: rotate(-28deg);
animation: small_rainy_rains 3s linear infinite;
}
@-webkit-keyframes small_rainy_rains {
0% {
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2);
}
25% {
box-shadow: 10px 14px 0 -2px #0cf;
}
50% {
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2);
}
100% {
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2);
}
}
@keyframes small_rainy_rains {
0% {
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2);
}
25% {
box-shadow: 10px 14px 0 -2px #0cf;
}
50% {
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2);
}
100% {
box-shadow: 10px 14px 0 -2px rgba(255, 255, 255, 0.2);
}
}
@-webkit-keyframes thunder_lightning {
0% {
background: rgba(255, 255, 255, 0.3);
}
5% {
background: rgb(0, 204, 255);
}
15% {
background: rgba(255, 255, 255, 0.3);
}
50% {
background: rgba(255, 255, 255, 0.6);
}
100% {
background: rgba(255, 255, 255, 0.3);
}
}
@keyframes thunder_lightning {
0% {
background: rgba(255, 255, 255, 0.3);
}
5% {
background: rgb(0, 204, 255);
}
15% {
background: rgba(255, 255, 255, 0.3);
}
50% {
background: rgba(255, 255, 255, 0.6);
}
100% {
background: rgba(255, 255, 255, 0.3);
}
}
@-webkit-keyframes thunder_lightning2 {
0% {
background: rgba(255, 255, 255, 0.3);
}
15% {
background: rgb(0, 204, 255);
}
30% {
background: rgba(255, 255, 255, 0.3);
}
45% {
background: rgb(0, 204, 255);
}
75% {
background: rgba(255, 255, 255, 0.6);
}
100% {
background: rgba(255, 255, 255, 0.3);
}
}
@keyframes thunder_lightning2 {
0% {
background: rgba(255, 255, 255, 0.3);
}
15% {
background: rgb(0, 204, 255);
}
30% {
background: rgba(255, 255, 255, 0.3);
}
45% {
background: rgb(0, 204, 255);
}
75% {
background: rgba(255, 255, 255, 0.6);
}
100% {
background: rgba(255, 255, 255, 0.3);
}
}