Skip to content

Commit faf160e

Browse files
authored
Merge pull request #49 from 595074187/595074187-patch-1
2016.10.6 翻译Class 与 Style 绑定
2 parents 40947f9 + 090426c commit faf160e

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/guide/class-and-style.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@ title: Class 与 Style 绑定
33
type: guide
44
order: 6
55
---
6+
数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。因为它们都是 attribute,我们可以用` v-bind` 处理它们:只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在 `v-bind `用于` class ``style `时,Vue.js 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。
67

7-
A common need for data binding is manipulating an element's class list and its inline styles. Since they are both attributes, we can use `v-bind` to handle them: we just need to calculate a final string with our expressions. However, meddling with string concatenation is annoying and error-prone. For this reason, Vue provides special enhancements when `v-bind` is used with `class` and `style`. In addition to strings, the expressions can also evaluate to objects or arrays.
88

9-
## Binding HTML Classes
9+
## 绑定 HTML Class
1010

11-
### Object Syntax
11+
### 对象语法
1212

13-
We can pass an object to `v-bind:class` to dynamically toggle classes:
13+
我们可以传给 `v-bind:class` 一个对象,以动态地切换 class。
1414

1515
``` html
1616
<div v-bind:class="{ active: isActive }"></div>
1717
```
18+
上面的语法表示class`active`的更新将取决于数据属性`isActive`是否为[truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)
1819

19-
The above syntax means the presence of the `active` class will be determined by the [truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) of the data property `isActive`.
20+
我们可以传给 v-bind:class 一个对象,以动态地切换 class。注意 v-bind:class 指令可以与普通的 class 特性共存
2021

21-
You can have multiple classes toggled by having more fields in the object. In addition, the `v-bind:class` directive can also co-exist with the plain `class` attribute. So given the following template:
22+
我们也可以在对象中传入多个属性用来动态切换多个class.注意 `v-bind:class` 指令可以与普通的 class 特性共存.如下模板:
2223

2324
``` html
2425
<div class="static"
2526
v-bind:class="{ active: isActive, 'text-danger': hasError }">
2627
</div>
2728
```
2829

29-
And the following data:
30+
如下 data:
3031

3132
``` js
3233
data: {
@@ -35,15 +36,15 @@ data: {
3536
}
3637
```
3738

38-
It will render:
39+
渲染为:
3940

4041
``` html
4142
<div class="static active"></div>
4243
```
4344

44-
When `isActive` or `hasError` changes, the class list will be updated accordingly. For example, if `hasError` becomes `true`, the class list will become `"static active text-danger"`.
45+
`isActive` 或者 `hasError` 变化时, class列表将相应地更新。例如,如果 `hasError` 的值为 `true`, class列表将变为 `"static active text-danger"`.
4546

46-
The bound object doesn't have to be inline:
47+
你也可以直接绑定数据里的一个对象:
4748

4849
``` html
4950
<div v-bind:class="classObject"></div>
@@ -56,8 +57,8 @@ data: {
5657
}
5758
}
5859
```
60+
渲染的结果和上面一样。我们也可以在这里绑定一个返回对象的[计算属性](computed.html)。这是一个常用且强大的模式:
5961

60-
This will render the same result. We can also bind to a [computed property](computed.html) that returns an object. This is a common and powerful pattern:
6162

6263
``` html
6364
<div v-bind:class="classObject"></div>
@@ -77,9 +78,9 @@ computed: {
7778
}
7879
```
7980

80-
### Array Syntax
81+
### 数组语法
8182

82-
We can pass an array to `v-bind:class` to apply a list of classes:
83+
我们可以把一个数组传给 `v-bind:class`,以应用一个 class 列表:
8384

8485
``` html
8586
<div v-bind:class="[activeClass, errorClass]">
@@ -91,31 +92,31 @@ data: {
9192
}
9293
```
9394

94-
Which will render:
95+
渲染为:
9596

9697
``` html
9798
<div class="active text-danger"></div>
9899
```
99100

100-
If you would like to also toggle a class in the list conditionally, you can do it with a ternary expression:
101+
如果你也想根据条件切换列表中的 class,可以用三元表达式:
101102

102103
``` html
103104
<div v-bind:class="[isActive ? activeClass : '', errorClass]">
104105
```
106+
此例始终添加 `errorClass`,但是只有在 `isActive` 是 true 时添加`activeClass`
105107

106-
This will always apply `errorClass`, but will only apply `activeClass` when `isActive` is `true`.
107-
108-
However, this can be a bit verbose if you have multiple conditional classes. That's why it's also possible to use the object syntax inside array syntax:
108+
不过,当有多个条件 class 时这样写有些繁琐。可以在数组语法中使用对象语法:
109109

110110
``` html
111111
<div v-bind:class="[{ active: isActive }, errorClass]">
112112
```
113113

114-
## Binding Inline Styles
114+
## 绑定内联样式
115+
116+
### 对象语法
115117

116-
### Object Syntax
118+
`v-bind:style` 的对象语法十分直观——看着非常像 CSS,其实它是一个 JavaScript 对象。CSS 属性名可以用驼峰式(camelCase)或短横分隔命名(kebab-case):
117119

118-
The object syntax for `v-bind:style` is pretty straightforward - it looks almost like CSS, except it's a JavaScript object. You can use either camelCase or kebab-case for the CSS property names:
119120

120121
``` html
121122
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
@@ -127,7 +128,7 @@ data: {
127128
}
128129
```
129130

130-
It is often a good idea to bind to a style object directly so that the template is cleaner:
131+
直接绑定到一个样式对象通常更好,让模板更清晰:
131132

132133
``` html
133134
<div v-bind:style="styleObject"></div>
@@ -139,18 +140,19 @@ data: {
139140
fontSize: '13px'
140141
}
141142
}
142-
```
143143

144-
Again, the object syntax is often used in conjunction with computed properties that return objects.
144+
```
145+
同样的,对象语法常常结合返回对象的计算属性使用。
145146

146-
### Array Syntax
147+
### 数组语法
147148

148-
The array syntax for `v-bind:style` allows you to apply multiple style objects to the same element:
149+
`v-bind:style` 的数组语法可以将多个样式对象应用到一个元素上:
149150

150151
``` html
151152
<div v-bind:style="[baseStyles, overridingStyles]">
152153
```
153154

154-
### Auto-prefixing
155+
### 自动添加前缀
156+
157+
`v-bind:style`使用需要厂商前缀的 CSS 属性时,如 `transform`,Vue.js 会自动侦测并添加相应的前缀。
155158

156-
When you use a CSS property that requires vendor prefixes in `v-bind:style`, for example `transform`, Vue will automatically detect and add appropriate prefixes to the applied styles.

0 commit comments

Comments
 (0)