CSS3More

常用动画

知识点附注于文中。

Code

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>transition+transform+animation</title>
</head>
<style>
.transitionTest,
.loading,
.box1,
.loadingRow,
.animate3D,
.replace {
float: left;
}

.transitionTest {
width: 100px;
height: 100px;
background-color: chocolate;
/* transition---过渡 */
/*
transition-property,transition-duration,transition-timing-function 和 transition-delay

linear 规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1)) (匀速)
ease 规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))(相对于匀速,中间快,两头慢)
ease-in 规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1))(相对于匀速,开始的时候慢,之后快)
ease-out 规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1))(相对于匀速,开始时快,结束时候间慢,)
ease-in-out 规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1))(相对于匀速,(开始和结束都慢)两头慢)
cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数值
*/
transition: all .5s ease-in-out .5s;
}

.transitionTest:hover {
width: 150px;
height: 150px;
background-color: blueviolet;
}

.box1,
.box,
.replace {
width: 300px;
height: 300px;
border: 1px solid;
}

.box2 {
width: 100px;
height: 100px;
background-color: darkcyan;
/* margin: 10px; */
/* transition: all 1s ease-in-out; */
/* 基点,默认图形中心 */
transform-origin: center;
animation: my 3s ease-in-out 0s infinite normal none running;

/*
animation---动画
animation-name,animation-duration, animation-timing-function,animation-delay,
animation-iteration-count,animation-direction,animation-fill-mode 和 animation-play-state

name, duration, timing-function, delay 同上
delay 正值-->延迟过渡指定时间,负值-->从提前动画指定时间处开始
animation-iteration-count 动画执行次数(default:1),[number(也可小数)/infinite]
animation-direction 指示动画是否反向播放
normal(default)
每个循环内动画向前循环,换言之,每个动画循环结束,动画重置到起点重新开始
alternate
动画交替反向运行,反向运行时,动画按步后退,同时,带时间功能的函数也反向,比如,ease-in 在反向时成为ease-out。计数取决于开始时是奇数迭代还是偶数迭代
reverse
反向运行动画,每周期结束动画由尾到头运行
alternate-reverse
反向交替, 反向开始交替
动画第一次运行时是反向的,然后下一次是正向,后面依次循环。决定奇数次或偶数次的计数从 1 开始
animation-fill-mode 设置CSS动画在执行之前和之后如何将样式应用于其目标,当有设置 delay 时才有效果
none: 动画结束后回到初始位置,0%在延迟后生效
backwards: 0%在延迟前生效
forwards: 动画结束后停到结束位置
both: backwards+forwards
animation-play-state 定义一个动画是否运行或者暂停(default:running),[running/paused];通常用于 JS
*/
/* animation: my 3s ease-in-out 0s infinite normal none running; */
}

.box2:hover {
/* transform---变形 */
/*
均有各自的 X/Y/Z/3d
translate 为百分数时是相对于自身的大小
scale 放大/缩小
rotate 旋转(单位:deg) ---正值->顺时针 --- 负值->逆时针
rotate3d(0,0,1,30deg) --- 绕 z 轴旋转 30 度
skew 斜切(单位:deg)(只有X/Y) ---正值->左 --- 负值->右
matrix 指定的 6(a,b,c,d,e,f) 个值组成的 2D 变换矩阵
X = ax + cy + e
Y = bx + dy + f

复合写在一起时:执行顺序为自右至左
变形时不会影响其他元素且只能添加到块级元素
*/
/* transform: translate(100px) scale(1.5) rotate(-20deg) skew(20deg); */
}

@keyframes my {
0% {
transform: translate(0, 0);
}

25% {
transform: translate(200px, 0);
}

50% {
transform: translate(200px, 200px);
}

75% {
transform: translate(0, 200px);
}

100% {
transform: translate(0, 0);
}
}

.loading {
width: 40px;
height: 40px;
margin: 20px;
position: relative;
}

.loading1,
.loading2 {
width: 100%;
height: 100%;
/* 相对于最近不是 static 定位的父元素 */
position: absolute;
}

.loading2 {
transform: rotate(45deg);
}

.loading1 div,
.loading2 div {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: rgb(28, 214, 158);
position: absolute;
animation: scaleAll 1.5s linear infinite;
}

@keyframes scaleAll {
0% {
transform: scale(0);
}

50% {
transform: scale(1);
}

100% {
transform: scale(0);
}
}

.loading1 div:nth-child(1),
.loading2 div:nth-child(1) {
left: 0;
top: 0;
}

.loading1 div:nth-child(2),
.loading2 div:nth-child(2) {
right: 0;
top: 0;
}

.loading1 div:nth-child(3),
.loading2 div:nth-child(3) {
right: 0;
bottom: 0;
}

.loading1 div:nth-child(4),
.loading2 div:nth-child(4) {
left: 0;
bottom: 0;
}

.loading1 div:nth-child(1) {
animation-delay: -0;
}

.loading2 div:nth-child(1) {
animation-delay: -.2s;
}

.loading1 div:nth-child(2) {
animation-delay: -.4s;
}

.loading2 div:nth-child(2) {
animation-delay: -.6s;
}

.loading1 div:nth-child(3) {
animation-delay: -.8s;
}

.loading2 div:nth-child(3) {
animation-delay: -1s;
}

.loading1 div:nth-child(4) {
animation-delay: 1.2s;
}

.loading2 div:nth-child(4) {
animation-delay: 1.4s;
}

.loadingRow {
width: 200px;
height: 50px;
display: flex;
}

.loadingRow div {
width: 20px;
height: 20px;
background-color: deeppink;
border-radius: 50%;
margin: 10px;
flex: 1;
animation: scaleAll 2s linear infinite alternate-reverse;
}

.loadingRow div:nth-child(1) {
animation-delay: -0;
}

.loadingRow div:nth-child(2) {
animation-delay: -.8s;
}

.loadingRow div:nth-child(3) {
animation-delay: -1.2s;
}

.loadingRow div:nth-child(4) {
animation-delay: -1.6s;
}

.loadingRow div:nth-child(5) {
animation-delay: -2s;
}

.animate3D {
margin: 0;
padding: 0;
}

.box {
/*
指定了观察者与 z=0 平面的距离,使具有三维位置变换的元素产生透视效果,
z>0 的三维元素比正常大,而 z<0 时则比正常小,大小程度由该属性的值决定。
*/
perspective: 200px;
/* clear: both; */
/*
指定了观察者的位置
([left/right/center/percent],[top/bottom/center/percent])
*/
perspective-origin: center top;
}

ul {
clear: both;
list-style: none;
position: relative;
/* 设置元素的子元素是位于 3D 空间中还是平面中,[flat,preserve-3d] */
transform-style: preserve-3d;
/* 基点 Z轴只能为 number
([left,center,right],[top,center,bottom],[number])
一个值时另外两个默认 center
*/
transform-origin: center center -100px;
text-align: center;
margin: 100px;
transition: 2s;
}

li {
width: 100px;
height: 100px;
position: absolute;
line-height: 100px;
text-align: center;
color: white;
font-size: 26px;
opacity: .75;
/* 隐藏背面 */
/* backface-visibility: hidden; */
}

li:nth-child(1) {
background-color: tomato;
left: 0;
top: 0;
}

li:nth-child(2) {
background-color: rgb(71, 255, 154);
left: 100px;
top: 0;
transform: rotateY(90deg);
transform-origin: left;
}

li:nth-child(3) {
background-color: rgb(126, 71, 255);
left: -100px;
top: 0;
transform: rotateY(-90deg);
transform-origin: right;
}

li:nth-child(4) {
background-color: rgb(237, 255, 71);
left: 0;
top: 100px;
transform: rotateX(-90deg);
transform-origin: top;
}

li:nth-child(5) {
background-color: rgb(255, 71, 184);
left: 0;
top: -100px;
transform: rotateX(90deg);
transform-origin: bottom;
}

li:nth-child(6) {
background-color: rgb(255, 197, 71);
left: 0;
top: 0;
transform: translateZ(-100px) rotateY(180deg);
}

.box:hover ul {
transform: rotateY(360deg);
/* rotate3d(1,1,0,360deg) */
}

.replace {
position: relative;
display: flex;
/* 主轴 */
justify-content: center;
/* 交叉轴 */
align-items: center;
perspective: 600px;
/* perspective-origin: center top; */
}

.first,
.second {
width: 200px;
height: 100px;
/* text-align: center; */
display: flex;
/* 主轴 */
justify-content: center;
/* 交叉轴 */
align-items: center;
position: absolute;
margin: 0 10px;
transition: 1s;
backface-visibility: hidden;
}

.first {
background-color: coral;
transform: rotateY(0);
}

.second {
background-color: rgb(216, 48, 104);
transform: rotateY(180deg);
}

.replace:hover .first {
transform: rotateY(-180deg);

}

.replace:hover .second {
transform: rotateY(0);
}

/*
有关 background: https://developer.mozilla.org/zh-CN/docs/Web/CSS/background

text-shadow为文字添加阴影,每个阴影值由元素在X和Y方向的偏移量、模糊半径和颜色值组成; 添加多个阴影,阴影值之间用逗号隔开
text-shadow: 1px 1px 2px pink;

box-shadow 属性用于在元素的框架上添加阴影效果,该属性可设置的值包括X轴偏移、Y轴偏移、阴影模糊半径、阴影扩散半径,和阴影颜色,并以多个逗号分隔
box-shadow: 12px 12px 2px 1px rgba(0, 0, 255, .2);

mask 允许使用者通过部分或者完全隐藏一个元素的可见区域,多个遮罩用逗号隔开
(url x y/width height repeat ) x/y 也可以为 left/right/center
.anothertarget {
mask: url(resources.svg#c1) 50px 30px/10px 10px repeat-x exclude;
}

CSS linear-gradient() 函数用于创建一个表示两种或多种颜色线性渐变的图片。
background: linear-gradient(45deg,#e66465, #9198e5);
More: https://developer.mozilla.org/zh-CN/docs/Web/CSS/linear-gradient

box-reflect 设置倒影
<direction>[above,below,left,right] <offset> 遮罩 | 渐变(只支持透明度的渐变)
-webkit-box-reflect: below 0 -webkit-linear-gradient(transparent,transparent 50%,rgba(255,255,255,.3));
兼容性不太好,可以使用 scale(-1) 来代替

blur 对容器进行模糊操作
calc 进行四则计算 calc(100% - 100px)

伪类: :hover
伪元素(创建一个虚拟容器): ::before ::after ::selection
*/
</style>

<body>
<div class="transitionTest"></div>
<div class="box1">
<div class="box2"></div>
</div>
<div class="loading">
<div class="loading1">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="loading2">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<div class="loadingRow">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="animate3D">
<div class="box">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</div>
</div>
<div class="replace">
<div class="first">1</div>
<div class="second">2</div>
</div>
</body>

</html>

Result

transtionMore.jpg

经典布局

Code

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CommonLayout</title>
</head>
<style>
/* *{
margin: 0;
padding: 0;
} */
center{
margin: 10px 0;
}
.parent1 {
border: 3px solid;
overflow: hidden;
}

.box1,
.box2 {
width: 100px;
margin-bottom: -2000px;
padding-bottom: 2000px;
}

.box1 {
float: left;
background-color: cornflowerblue;
}

.box2 {
float: right;
background-color: burlywood;
}

.parent2 {
border: 3px solid;
height: 300px;
/* float: left; */
}

.header{
height: 100px;
background-color: pink;
}

.left,
.right {
float: left;
width: 100px;
height: 200px;
background-color: darksalmon;
}

.center {
/* width: calc(100% - 200px); */
width: 100%;
height: 200px;
float: left;
}

.center div{
background-color: darkseagreen;
height: 200px;
margin: 0 100px;
}

.left {
margin-left: -100%;
}

.right {
margin-left: -100px;
}

.parent3{
border: 3px solid;
padding: 0 100px;
height: 200px;
}

.parent3 .center{
background-color: darkseagreen;
height: 200px;
}

.parent3 .left{
position: relative;
left: -100px;
}

.parent3 .right{
position: relative;
right: -100px;
}
</style>

<body>
<center>等高布局</center>
<div class="parent1">
<div class="box1">
<p>sssdddd</p>
<p>sssdddd</p>
<p>sssdddd</p>
<p>sssdddd</p>
<p>sssdddd</p>
</div>
<div class="box2">
<p>sssdddd</p>
<p>sssdddd</p>
<p>sssdddd</p>
</div>
</div>
<center>双飞翼布局(个人推荐)</center>
<div class="parent2">
<div class="header">
<!-- <p>header</p> -->
</div>
<div class="center">
<div>sssdddd</div>
</div>
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
</div>
</div>
<center>圣杯布局</center>
<div class="header">
<p>header</p>
</div>
<div class="parent3">
<div class="center">
<p>sssdddd</p>
</div>
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
</div>
</div>
</body>

</html>

Result

CommonLayout.jpg

SCSS Usage

Document: Sass 中文网

  • VScode中可以使用Live SCSS来进行实时编译SCSSCSS

Code

@import "./hello.css";

$nav-color: #f90;
$highlightColor: rgba(68, 255, 199, 0.267);
$width1: 100px;

$name: foo;

$background: (
a: url(a.jpg),
b: url(b.jpg),
);
$transform: (
a: translate(100px),
b: scale(1.5),
);

@mixin show {
display: inline-block;
}
@mixin hide($color) {
display: none;
color: $color;
}

@function sum($m, $n) {
@return $m + $n;
}

* {
margin: 0;
padding: 0;
}

div {
width: $width1;
$width1: 200px;
height: $width1;
p {
font-size: 16px;
font: {
weight: 700;
}
}
}

.nav1 {
color: $nav-color;
width: $width1 * 2;
}

a {
list-style: none;
&:hover {
color: $highlightColor;
}
}

p .#{$name}{
font-size: 20px;
}

.container {
h1,
h2,
h3 {
margin-bottom: 0.8em;
font-size: floor($number: 5.4);
font-size: round($number: 5.8);
}
}

.box1 {
background: map-values($map: $background);
transform: zip(map-values($transform) ...);
}

.box2 {
width: 500px;
@include show();
@include hide(aqua);
}

.box3{
width: sum($width1, 100px);
}

Result

@charset "UTF-8";
@import url(c:\资源\HTML\NewHtml5CSS3\hello.css);
* {
margin: 0;
padding: 0;
}

div {
width: 100px;
height: 200px;
}

div p {
font-size: 16px;
font-weight: 700;
}

.nav1 {
color: #f90;
width: 200px;
}

a {
list-style: none;
}

a:hover {
color: rgba(68, 255, 199, 0.267);
}

p .foo {
font-size: 20px;
}

.container h1,
.container h2,
.container h3 {
margin-bottom: 0.8em;
font-size: 5;
font-size: 6;
}

.box1 {
background: url(a.jpg), url(b.jpg);
-webkit-transform: translate(100px) scale(1.5);
transform: translate(100px) scale(1.5);
}

.box2 {
width: 500px;
display: inline-block;
display: none;
color: aqua;
}

.box3 {
width: 200px;
}
/*# sourceMappingURL=firstUse.css.map */

PostCSS

是一个用 JavaScript 工具和插件转换 CSS 代码的工具.

Document: PostCSS中文网

因为通常是在 webpack 中使用,此处不做示例~

End

后续继续更新!

Author: 𝓣𝓪𝓭𝓶
Link: https://liuhongwei3.github.io/2020/03/27/CSS3More/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.