JS的作用:
- 操作DOM
- 操作AJAX
- 其他
一个项目60%的时间在写CSS,20%的时间在写JS, 20%的时间在想自己错在哪儿
材料来源:
codepen:https://codepen.io/
将body的font-size设为100px, 则其他的地方的单位用em就很好换算了
鼻子
做一个钝角三角形
要点:让border-width不同
.nose {
width:0;
height:0;
border-top: 9px solid red;
border-left: 11px solid transparent;
border-right: 11px solid transparent;
}
在三角形上加半个椭圆
要点:用x,y轴半径构成椭圆
.nose::before {
content: '';
display: block;
width: 22px;
height: 5px;
background: black;
position: absolute;
top: -14px;
left:-11px;
border-top-right-radius: 11px 5px; /* 椭圆 x y 轴半径: x=width/2 y=height */
border-top-left-radius: 11px 5px;
box-shadow: 0 -.5px 1px black;
}
眼睛
要点:先居中,然后用transform移到两边
.eye {
border: 3px solid black;
width: 64px;
height: 64px;
position: absolute;
left: 50%;
top: 165px;
margin-left: -32px;
border-radius: 50%;
background:#2e2e2e;
}
.eye.left {
transform: translateX(-110px); /* 让眼睛在鼻子两边 */
}
.eye.right {
transform: translateX(110px);
}
.eye::before{
content: '';
display: block;
width: 32px;
height: 32px;
border: 3px solid black;
background: white;
border-radius: 50%;
position: relative;
left: 6px;
top: -2px;
}
上嘴唇
要点:用translateZ(0)去除rotate产生的边框
.up {
width: 160px;
height: 100px;
position: absolute;
left: 50%;
top: 220px;
margin-left: -80px;
}
.up .lip {
background: #ffe600;
width: 80px;
height: 30px;
position: absolute;
}
.up .lip.left{
border-left: 3px solid black;
border-bottom: 3px solid black;
border-bottom-left-radius: 55px 30px;
transform: rotate(-25deg) translateZ(0); /* 用GPU渲染,去除锯齿边框 */
left: -1.5px;
}
.up .lip.right{
border-right: 3px solid black;
border-bottom: 3px solid black;
border-bottom-right-radius: 55px 30px;
transform: rotate(25deg) translateZ(0);
right: -1.5px;
}
下嘴唇
要点: 用 overflow:hidden 隐藏超出盒子的内容
.down {
width: 140px;
height: 170px;
position: absolute;
left: 50%;
margin-left: -70px;
top: 232px;
overflow: hidden;
z-index: -1;
}
.down .inner {
width: 180px;
height: 900px;
border: 4px solid black;
background: #9b000a;
position: absolute;
bottom: 0;
left: 50%;
margin-left: -90px;
border-bottom-left-radius: 100%;
border-bottom-right-radius: 100%;
overflow: hidden;
}
.down .inner .outer {
width: 200px;
height: 500px;
background: #ff485f;
position: absolute;
bottom: -360px;
left: 50%;
margin-left: -100px;
border-top-left-radius: 95px 150px;
border-top-right-radius: 95px 150px;
}
鼻子动画
要点:transform-origin设置变形的原点
@keyframes move {
0% {transform: rotate(0)}
33% {transform: rotate(4deg)}
66% {transform: rotate(-4deg)}
100% {transform: rotate(0)}
}
.nose:hover {
transform-origin: center bottom;
animation: move 100ms infinite;
}
眼球转动效果
@keyframes eyes-move {
0%,20% {top: -1px; left: 7px;}
5% {top: 5px; left: 25px;}
10% {top: 20px; left: 29px;}
15% {top: 25px; left: 5px;}
}
.eye::before {
animation: eyes-move 4s infinite linear;
}
眨眼效果
通过移动位于眼睛上方与背景同色的div来实现
也可以用opacity调整透明度来实现,1表示不透明,0表示透明
@keyframes eye-close {
0%, 23%, 33%, 100% {top: -100px;}
28% {top: -55px;}
}
.eye::after {
content: '';
display: block;
width: 66px;
height: 66px;
position: relative;
top: -100px;
left: -4px;
background: #ffe600;
animation: eye-close 4s infinite linear;
}
颜色渐变淡出效果
要点:ease-out
.cheek {
animation: light 4s ease-out infinite;
}
@keyframes light {
55% {background: #ff0000;}
60% {background: #FFFA3F;}
}