使用CSS3 Transforms变换和Animations动画练习制作钢铁侠胸口的小型核反应堆,个人觉得光看还是不行的,还是要多练,并对其中用到得不太熟悉的css属性进行温习补缺。
先制作一个黑暗的背景容器
body {
margin: 0;
}
.fullpage-wrapper {
height: 100vh;
background: radial-gradient(#353c44, #222931);
}
vh是指CSS中相对长度单位,表示相对视口高度(Viewport Height),1vh = 1% * 视口高度。浏览器支持:IE9+;
radial-gradient() 函数用径向渐变创建 "图像"。径向渐变由中心点定义,为了创建径向渐变你必须设置两个终止色。浏览器支持:IE10+。
background: radial-gradient(shape size at position, start-color, ..., last-color);
值 | 描述 |
shape | 确定圆的类型: ● ellipse (默认): 指定椭圆形的径向渐变。 ● circle :指定圆形的径向渐变
|
size | 定义渐变的大小,可能值: ● farthest-corner (默认) : 指定径向渐变的半径长度为从圆心到离圆心最远的角 ● closest-side :指定径向渐变的半径长度为从圆心到离圆心最近的边 ● closest-corner : 指定径向渐变的半径长度为从圆心到离圆心最近的角 ● farthest-side :指定径向渐变的半径长度为从圆心到离圆心最远的边
|
position | 定义渐变的位置。可能值: ● center(默认):设置中间为径向渐变圆心的纵坐标值。 ● top:设置顶部为径向渐变圆心的纵坐标值。 ● bottom:设置底部为径向渐变圆心的纵坐标值。
|
start-color, ..., last-color | 用于指定渐变的起止颜色。 |
核反应堆垂直水平居中
.fullpage-wrapper {
height: 100vh;
background: radial-gradient(#353c44, #222931);
/*声明flex容器*/
display:flex;
}
.reactor-container {
width: 300px;
height: 300px;
/*margin:auto;垂直水平居中*/
margin: auto;
border: 1px dashed #888;
}
关于Flexbox弹性布局请看这里,关于Flexbox弹性布局之实例片段点这里。浏览器兼容:IE10+。
CSS中的同心圆
<div class="fullpage-wrapper">
<div class="reactor-container">
<!-- 最小的圆
-->
<div class="core-inner"></div>
</div>
</div>
.core-inner {
position: absolute;
width: 70px;
height: 70px;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
border-radius: 50%;
border: 5px solid #1b4e5f;
background-color: #fff;
}
这里使用到了css中position定位margin:auto垂直居中。
最终代码
因为需要多个圆,所以这的css样式进行拆分。篇幅有限,直接贴出最终的结构代码与样式。
HTML结构:
<div class="fullpage-wrapper">
<div class="reactor-container">
<div class="reactor-container-inner circle abs-center"></div>
<div class="tunnel circle abs-center"></div>
<div class="core-wrapper circle abs-center"></div>
<div class="core-outer circle abs-center"></div>
<div class="core-inner circle abs-center"></div>
<div class="coil-container">
<div class="coil coil-1"></div>
<div class="coil coil-2"></div>
<div class="coil coil-3"></div>
<div class="coil coil-4"></div>
<div class="coil coil-5"></div>
<div class="coil coil-6"></div>
<div class="coil coil-7"></div>
<div class="coil coil-8"></div>
</div>
</div>
</div>
CSS样式:
body {
margin: 0;
}
.fullpage-wrapper {
height: 100vh;
background: radial-gradient(#353c44, #222931);
display:flex;
}
.reactor-container {
width: 300px;
height: 300px;
margin: auto;
border: 1px dashed #888;
}
.circle {
border-radius: 50%;
}
.abs-center {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
.core-inner {
width: 70px;
height: 70px;
border: 5px solid #1b4e5f;
background-color: #fff;
box-shadow: 0px 0px 7px 5px #52fefe, 0px 0px 10px 10px #52fefe inset;
}
.core-outer {
width: 120px;
height: 120px;
border: 1px solid #52fefe;
background-color: #fff;
box-shadow: 0px 0px 2px 1px #52fefe, 0px 0px 10px 5px #52fefe inset;
}
.core-wrapper {
width: 180px;
height: 180px;
background-color: #073c4b;
box-shadow: 0px 0px 5px 4px #52fefe, 0px 0px 6px 2px #52fefe inset;
}
.tunnel {
width: 220px;
height: 220px;
background-color: #fff;
box-shadow: 0px 0px 5px 1px #52fefe, 0px 0px 5px 4px #52fefe inset;
}
.coil-container {
position: relative;
width: 100%;
height: 100%;
animation-name: reactor-anim;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
.coil {
position: absolute;
width: 30px;
height: 20px;
top: calc(50% - 110px);
left: calc(50% - 15px);
transform-origin: 15px 110px;
background-color: #073c4b;
box-shadow: 0px 0px 5px #52fefe inset;
}
.coil-1 {
transform: rotate(0deg);
}
.coil-2 {
transform: rotate(45deg);
}
.coil-3 {
transform: rotate(90deg);
}
.coil-4 {
transform: rotate(135deg);
}
.coil-5 {
transform: rotate(180deg);
}
.coil-6 {
transform: rotate(225deg);
}
.coil-7 {
transform: rotate(270deg);
}
.coil-8 {
transform: rotate(315deg);
}
@keyframes reactor-anim {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.reactor-container {
width: 300px;
height: 300px;
margin: auto;
border: 1px dashed #888;
position: relative;
border-radius: 50%;
background-color: #384c50;
border: 1px solid rgb(18, 20, 20);
box-shadow: 0px 0px 32px 8px rgb(18, 20, 20), 0px 0px 4px 1px rgb(18, 20, 20) inset;
}
box-shadow的用法:
/* x偏移量 | y偏移量 | 阴影颜色 */
box-shadow: 60px -16px teal;
/* x偏移量 | y偏移量 | 阴影模糊半径 | 阴影颜色 */
box-shadow: 10px 5px 5px black;
/* x偏移量 | y偏移量 | 阴影模糊半径 | 阴影扩散半径 | 阴影颜色 */
box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
/* 插页(阴影向内) | x偏移量 | y偏移量 | 阴影颜色 */
box-shadow: inset 5em 1em gold;
/* 任意数量的阴影,以逗号分隔 */
box-shadow: 3px 3px red, -1em 0 0.4em olive;
/* 全局关键字 */
box-shadow: inherit;
box-shadow: initial;
box-shadow: unset;
浏览器兼容:IE9+;
calc()函数的用法:
calc()函数可以用在任何一个需要<length>、<frequency>, <angle>、<time>、<number>、或<integer>的地方。有了calc(),你就可以通过计算来决定一个CSS属性的值了。
你还可以在一个 calc() 内部嵌套另一个 calc() ,里面的 calc() 会被简单地视为加了括号。
浏览器兼容:IE9+;
/* property: calc(expression) */
width: calc(100% - 80px);
transform-origin
transform-origin CSS属性让你更改一个元素变形的原点。
/* One-value syntax */
transform-origin: 2px;
transform-origin: bottom;
/* x-offset | y-offset */
transform-origin: 3cm 2px;
/* x-offset-keyword | y-offset */
transform-origin: left 2px;
/* x-offset-keyword | y-offset-keyword */
transform-origin: right top;
/* y-offset-keyword | x-offset-keyword */
transform-origin: top right;
/* x-offset | y-offset | z-offset */
transform-origin: 2px 30% 10px;
/* x-offset-keyword | y-offset | z-offset */
transform-origin: left 5px -3px;
/* x-offset-keyword | y-offset-keyword | z-offset */
transform-origin: right bottom 2cm;
/* y-offset-keyword | x-offset-keyword | z-offset */
transform-origin: bottom right 2cm;
/* Global values */
transform-origin: inherit;
transform-origin: initial;
transform-origin: unset;
transform-origin属性可以使用一个,两个或三个值来指定,其中每个值都表示一个偏移量。 没有明确定义的偏移将重置为其对应的初始值。