1# 渐变样式
2
3组件普遍支持的在style或css中设置的 可以平稳过渡两个或多个指定的颜色。
4
5> **说明:**
6>
7> 从API Version 8 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9开发框架支持线性渐变 (linear-gradient)和重复线性渐变 (repeating-linear-gradient)两种渐变效果。
10
11
12## 线性渐变/重复线性渐变
13
14使用渐变样式,需要定义过渡方向和过渡颜色。
15
16
17### 过渡方向
18
19  通过direction或者angle指定过渡方向。
20
21- direction:进行方向渐变。
22
23- angle:进行角度渐变。
24
25
26```css
27background: linear-gradient(direction/angle, color, color, ...);
28background: repeating-linear-gradient(direction/angle, color, color, ...);
29```
30
31
32### 过渡颜色
33
34支持以下四种方式:\#ff0000、\#ffff0000、rgb(255, 0, 0)、rgba(255, 0, 0, 1),需要指定至少两种颜色。
35
36**参数:**
37
38| 名称        | 类型                                       | 默认值                          | 必填   | 描述                                       |
39| --------- | ---------------------------------------- | ---------------------------- | ---- | ---------------------------------------- |
40| direction | to <side-or-corner>  <side-or-corner> = [left \| right] \|\| [top \| bottom] | to bottom (由上到下渐变) | 否    | 指定过渡方向,如:to left (从右向左渐变)  ,或者to bottom right (从左上角到右下角)。 |
41| angle     | <deg>                              | 180deg                       | 否    | 指定过渡方向,以元素几何中心为坐标原点,水平方向为X轴,angle指定了渐变线与Y轴的夹角(顺时针方向)。 |
42| color     | <color> [<length>\|<percentage>] | -                            | 是    | 定义使用渐变样式区域内颜色的渐变效果。                      |
43
44**示例:**
45
461. 默认渐变方向为从上向下渐变。
47
48   ```css
49   #gradient {
50     height: 300px;
51     width: 600px;
52     /* 从顶部开始向底部由红色向绿色渐变 */
53     background: linear-gradient(red, #00ff00);
54   }
55   ```
56
57   ![111](figures/111.PNG)
58
592. 45度夹角渐变。
60
61   ```css
62   /* 45度夹角,从红色渐变到绿色 */
63     background: linear-gradient(45deg, rgb(255, 0, 0),rgb(0, 255, 0));
64   ```
65
66      ![222](figures/222.PNG)
67
683. 设置方向从左向右渐变。
69
70   ```css
71   /* 从左向右渐变,在距离左边90px和距离左边360px (600*0.6) 之间270px宽度形成渐变 */
72   background: linear-gradient(to right, rgb(255, 0, 0) 90px, rgb(0, 255, 0) 60%);
73   ```
74
75    ![333](figures/333.PNG)
76
774. 重复渐变。
78
79   ```css
80     /* 从左向右重复渐变,重复渐变区域30px(60-30)透明度0.5 */
81     background: repeating-linear-gradient(to right, rgba(255, 255, 0, 1) 30vp,rgba(0, 0, 255, .5) 60vp);
82   ```
83
84   ![444](figures/444.PNG)
85
86
87
88