jieye の 数字花园

Search

Search IconIcon to open search

CSS3

Last updated Jul 6, 2022

# CSS3

优势:

应用场景:

# transition

transition CSS 属性是 transition-propertytransition-durationtransition-timing-functiontransition-delay 的一个 简写属性

image-20200213184433362

过渡可以为一个元素在不同状态之间切换的时候定义不同的过渡效果。比如在不同的伪元素之间切换,像是 :hover:active 或者通过 JavaScript 实现的状态变化。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Apply to 1 property */
/* property name | duration */
transition: margin-right 4s;

/* property name | duration | delay */
transition: margin-right 4s 1s;

/* property name | duration | timing function */
transition: margin-right 4s ease-in-out;

/* property name | duration | timing function | delay */
transition: margin-right 4s ease-in-out 1s;

/* Apply to 2 properties */
transition: margin-right 4s, color 1s;

/* Apply to all changed properties */
transition: all 0.5s ease-out;

/* Global values */
transition: inherit;
transition: initial;
transition: unset;

transition的优点在于简单易用,但是它有几个很大的局限。

# transform

转换transform是CSS3中具有颠覆性的特征之一,可以实现元素的位移、旋转、缩放等效果。

移动:translate

旋转:rotate

缩放:scale

# transform 2D

image-20200213201443181

transform-origin:

转换中心点

transform-origin: x y;

translate:

transform: translate(x,y); 或者分开写

transform: translateX(n);

transform: translateY(n);

rotate

transform:rotate(度数)

image-20200213201803232

scale

transform:scale(x,y);

注意:

  1. 同时使用多个转换,其格式为:transform: translate() rotate() scale() …等,
  2. 其顺序会影转换的效果(先旋转会改变坐标轴方向)
  3. 当我们同时有位移和其他属性的时候,记得要将位移放到最前

# transform 3D

image-20200213202206022

translate3d:

3D移动在2D移动的基础上多加了一个可以移动的方向,就是z轴方向。

因为z轴是垂直屏幕,由里指向外面,所以默认是看不到元素在z轴的方向上移动。

perspective:

image-20200213202357620

translform:translateZ(100px):仅仅是在Z轴上移动。有了透视,就能看到translateZ 引起的变化了

rotate3d:

3D旋转指可以让元素在三维平面内沿着 x轴,y轴,z轴或者自定义轴进行旋转。

transfrom-style:

控制子元素是否开启三维立体环境。

代码写给父级,但是影响的是子盒子。这个属性很重要,后面必用。

# Animation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/*一个周期持续的时间,以及动画效果的名称
当鼠标悬停在div元素上时,会产生名为‘动画名称’的动画效果,持续时间为1秒。为此,我们还需要用keyframes关键字,定义‘动画名称’效果。
*/
div:hover {
  animation: 1s 动画名称;
}

@keyframes 动画名称 {
   0% { background: #c00; }
  50% { background: orange; }
  100% { background: yellowgreen; }
}

# 具体属性

animation:动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 动画起始或者结束的状态;

image-20200213203101735

简写属性里面不包含 animation-play-state
暂停动画:animation-play-state: puased; 经常和鼠标经过等其他配合使用
想要动画走回来 ,而不是直接跳回来:animation-direction:alternate;
盒子动画结束后,停在结束位置: animation-fill-mode:forwards;

image-20200213203243443

# 旋转木马案例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body{
      perspective: 1000px;
    }
    section{
      position: relative;
      height: 200px;
      width: 300px;
      margin: 200px auto;
      transform-style: preserve-3d;
      animation: rota 0.5s linear infinite;
    }

    section:hover{
      animation-play-state: paused;
    }

    @keyframes  rota {
      0%{
        transform:rotateY(0) ;
      }
      100%{
        transform: rotateY(360deg);
      }
    }
    section div{
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: url('http://pic0.iqiyipic.com/common/lego/20200209/ccc614312b7048f19408192c91a8ecf9.jpg') no-repeat ;
    }
    section div:nth-child(1){
      transform: translateZ(300px);
    }
    section div:nth-child(2){
      transform: rotateY(60deg) translateZ(300px);
    }
    section div:nth-child(3){
      transform: rotateY(120deg) translateZ(300px);
    }
    section div:nth-child(4){
      transform: rotateY(180deg) translateZ(300px);
    }
    section div:nth-child(5){
      transform: rotateY(240deg) translateZ(300px);
    }
    section div:nth-child(6){
      transform: rotateY(300deg) translateZ(300px);
    }
  </style>
</head>
<body>
  <section>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </section>
</body>
</html>