“我正在参加 码上掘金体验活动,详情:show出你的创意代码块”
一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第16天,点击查看活动详情。
前言
秋千一种运动和游戏的用具,在木架或铁架之间系上绳索,下面拴一块长板。人在板上用力蹬以来回摆动。
实现
布局
<div class="wrapper">
<div class="swing">
<!-- 荡的部分 -->
<div class="human">
<!-- 上半部分 -->
<div class="top-part">
<div class="head">
<!-- 头 -->
<div class="hair"></div>
<!-- 头发 -->
<div class="fringe"></div>
<!-- 刘海 -->
<div class="eye"></div>
<!-- 眼睛 -->
<div class="mouth"></div>
<!-- 嘴巴 -->
</div>
<div class="body">
<!-- 身体 -->
<div class="hand">
<!-- 手 -->
<div class="hand-one"></div>
<div class="hand-two"></div>
</div>
</div>
</div>
<div class="leg-one"></div>
<!-- 脚 -->
<div class="leg-two"></div>
</div>
<div class="swing-rod"></div>
<!-- 绳索 -->
<div class="bottom"></div>
<!-- 木板 -->
</div>
<div class="pole one"></div>
<!-- 支撑物 -->
<div class="pole two"></div>
</div>
样式
支撑物,众所周知建房子需要打好地基,不然房子容易坍塌,那么秋千也是如此。
.wrapper .pole {
position: absolute;
width: 5px;
height: 250px;
left: 50%;
-webkit-transform-origin: center top;
transform-origin: center top;
-webkit-transform: translateX(-50%) rotate(-25deg);
transform: translateX(-50%) rotate(-25deg);
color: silver;
background: currentColor;
}
.wrapper .pole.two {
-webkit-transform: translateX(-50%) rotateY(-180deg) rotate(-25deg);
transform: translateX(-50%) rotateY(-180deg) rotate(-25deg);
}
.wrapper .pole:before {
content: "";
position: absolute;
top: -10px;
left: -7px;
width: 20px;
height: 20px;
border-radius: 100%;
background: currentColor;
}
.wrapper .pole:after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 20px;
height: 5px;
-webkit-transform: translateX(-50%) rotate(25deg);
transform: translateX(-50%) rotate(25deg);
background: currentColor;
}
绳索和木板样式 连接上下部分就组合成了一个秋千
.wrapper .swing .swing-rod {
position: absolute;
width: 5px;
height: 200px;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
background: #323232;
}
.wrapper .swing .bottom {
position: absolute;
width: 100%;
height: 5px;
bottom: 0;
background: saddlebrown;
}
身体部分样式,头发、刘海、眼睛、嘴巴、手等部位
.wrapper .swing .human {
position: absolute;
width: 40px;
height: 80px;
bottom: 5px;
z-index: -1;
}
.wrapper .swing .human .top-part {
position: absolute;
width: 40px;
height: 80px;
bottom: 5px;
-webkit-transform-origin: center bottom;
transform-origin: center bottom;
-webkit-animation-name: body-animation;
animation-name: body-animation;
-webkit-animation-duration: 0.8s;
animation-duration: 0.8s;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;
animation-timing-function: ease-in-out;
-webkit-animation-direction: alternate;
animation-direction: alternate;
}
.wrapper .swing .human .top-part .head {
position: absolute;
bottom: 40px;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 20px;
height: 20px;
background: #d59e7b;
border-radius: 100%;
z-index: 2;
}
.wrapper .swing .human .top-part .head .hair {
position: absolute;
top: -5px;
left: -10px;
background: black;
width: 22px;
height: 44px;
border-top-right-radius: 0%;
border-top-left-radius: 100%;
border-bottom-right-radius: 75%;
border-bottom-left-radius: 10%;
z-index: 2;
}
.wrapper .swing .human .top-part .head .fringe {
position: absolute;
top: -5px;
left: 0;
width: 0px;
height: 0px;
border-top: 10px solid black;
border-bottom: 10px solid black;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-radius: 100%;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.wrapper .swing .human .top-part .head .eye {
content: "";
position: absolute;
top: 6px;
right: 2px;
width: 2px;
height: 2px;
background: black;
border-radius: 100%;
}
.wrapper .swing .human .top-part .head .mouth {
position: absolute;
top: 10px;
right: 0px;
width: 0px;
height: 0px;
border-top: 3px solid transparent;
border-bottom: 3px solid transparent;
border-left: 3px solid transparent;
border-right: 3px solid white;
border-radius: 100%;
-webkit-transform: rotate(28deg);
transform: rotate(28deg);
}
.wrapper .swing .human .top-part .body {
position: absolute;
bottom: 0px;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
background: #ee5130;
width: 20px;
height: 40px;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
}
.wrapper .swing .human .top-part .body .hand {
position: absolute;
top: 20%;
left: 10%;
}
.wrapper .swing .human .top-part .body .hand .hand-one {
position: absolute;
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
width: 5px;
height: 15px;
background: #ee5130;
-webkit-box-shadow: 0 -1px 0px 1px rgba(0, 0, 0, 0.2);
box-shadow: 0 -1px 0px 1px rgba(0, 0, 0, 0.2);
border-top-right-radius: 100%;
border-top-left-radius: 100%;
}
.wrapper .swing .human .top-part .body .hand .hand-two {
background: #d59e7b;
position: absolute;
top: 14px;
left: -6px;
width: 4px;
height: 15px;
-webkit-transform-origin: center top;
transform-origin: center top;
-webkit-transform: rotate(-60deg);
transform: rotate(-60deg);
border-top-left-radius: 15px;
}
脚部分
.wrapper .swing .human .leg-one {
position: absolute;
bottom: 0;
left: 10px;
width: 40px;
height: 10px;
background: #1973ad;
border-bottom-left-radius: 20px;
border-top-right-radius: 20px;
z-index: -1;
}
.wrapper .swing .human .leg-two {
position: absolute;
width: 40px;
height: 10px;
bottom: 5px;
right: -5px;
border-top-right-radius: 50%;
border-bottom-right-radius: 20px;
background: #1973ad;
-webkit-transform-origin: right center;
transform-origin: right center;
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
-webkit-animation-name: leg-animation;
animation-name: leg-animation;
-webkit-animation-duration: 0.8s;
animation-duration: 0.8s;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;
animation-timing-function: ease-in-out;
-webkit-animation-direction: alternate;
animation-direction: alternate;
}
.wrapper .swing .human .leg-two:after {
content: "";
position: absolute;
left: -5px;
top: -1px;
background: silver;
height: 18px;
width: 5px;
border-top-left-radius: 3px;
border-bottom-right-radius: 50%;
}
联动部分,人在板上用力蹬以来回摆动的动画
@-webkit-keyframes swing-1 {
from {
-webkit-transform: rotate(-35deg);
transform: rotate(-35deg);
}
to {
-webkit-transform: rotate(35deg);
transform: rotate(35deg);
}
}
@keyframes swing-1 {
from {
-webkit-transform: rotate(-35deg);
transform: rotate(-35deg);
}
to {
-webkit-transform: rotate(35deg);
transform: rotate(35deg);
}
}
@-webkit-keyframes body-animation {
from {
-webkit-transform: rotate(-25deg);
transform: rotate(-25deg);
}
to {
-webkit-transform: rotate(20deg);
transform: rotate(20deg);
}
}
@keyframes body-animation {
from {
-webkit-transform: rotate(-25deg);
transform: rotate(-25deg);
}
to {
-webkit-transform: rotate(20deg);
transform: rotate(20deg);
}
}
@-webkit-keyframes leg-animation {
from {
bottom: 2px;
right: -8px;
-webkit-transform: rotate(-55deg);
transform: rotate(-55deg);
}
to {
bottom: 2px;
right: 5px;
-webkit-transform: rotate(-170deg);
transform: rotate(-170deg);
}
}
@keyframes leg-animation {
from {
bottom: 2px;
right: -8px;
-webkit-transform: rotate(-55deg);
transform: rotate(-55deg);
}
to {
bottom: 2px;
right: 5px;
-webkit-transform: rotate(-170deg);
transform: rotate(-170deg);
}
}
总结
整篇主要用到的是css的transform属性的平移,@keyframes动画的旋转,绝对定位,背景颜色,堆叠顺序,阴影,边框,以上这些内容组合成的荡秋千的女孩,谢谢观看!