Skip to content

Commit 7a11c3f

Browse files
shinoshukazupon
andauthored
docs: Essentials > Class and Style Bindings (#65)
* Fix class-and-style.md * Update src/guide/class-and-style.md Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com> Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com>
1 parent 3ae53bb commit 7a11c3f

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

src/guide/class-and-style.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Class and Style Bindings
1+
# クラスとスタイルのバインディング
22

3-
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 only 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.
3+
データバインディングに一般に求められることの 1 つは、要素のクラスリストとインラインスタイルを操作することです。それらはどちらも属性であるため、`v-bind` を使って扱うことができます。最終的な文字列を式で計算するだけです。しかしながら、文字列の連結に手を出すのは煩わしく、エラーのもとです。そのため、Vue `v-bind` `class` `style` と一緒に使われるとき、特別な拡張機能を提供します。文字列だけではなく、式はオブジェクトまたは配列を返すことができます。
44

5-
## Binding HTML Classes
5+
## HTML クラスのバインディング
66

7-
### Object Syntax
7+
### オブジェクト構文
88

9-
We can pass an object to `:class` (short for `v-bind:class`) to dynamically toggle classes:
9+
`:class` (`v-bind:class` の略) にオブジェクトを渡すことでクラスを動的に切り替えることができます:
1010

1111
```html
1212
<div :class="{ active: isActive }"></div>
1313
```
1414

15-
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`.
15+
上記の構文は、`active` クラスの有無がデータプロパティ `isActive`[真偽性](https://developer.mozilla.org/ja-JP/docs/Glossary/Truthy)によって決まることを意味しています。
1616

17-
You can have multiple classes toggled by having more fields in the object. In addition, the `:class` directive can also co-exist with the plain `class` attribute. So given the following template:
17+
オブジェクトにさらにフィールドを持たせることで複数のクラスを切り替えることができます。加えて、`:class` ディレクティブはプレーンな `class` 属性と共存できます。つまり、次のようなテンプレートと:
1818

1919
```html
2020
<div
@@ -23,7 +23,7 @@ You can have multiple classes toggled by having more fields in the object. In ad
2323
></div>
2424
```
2525

26-
And the following data:
26+
次のようなデータがあったとすると:
2727

2828
```js
2929
data() {
@@ -34,15 +34,15 @@ data() {
3434
}
3535
```
3636

37-
It will render:
37+
このように描画されます:
3838

3939
```html
4040
<div class="static active"></div>
4141
```
4242

43-
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"`.
43+
`isActive` もしくは `hasError` が変化するとき、クラスリストはそれに応じて更新されます。例えば、`hasError` `true` になった場合、クラスリストは `"static active text-danger"` になります。
4444

45-
The bound object doesn't have to be inline:
45+
束縛されるオブジェクトはインラインでなくてもかまいません:
4646

4747
```html
4848
<div :class="classObject"></div>
@@ -59,7 +59,7 @@ data() {
5959
}
6060
```
6161

62-
This will render the same result. We can also bind to a [computed property](computed.md) that returns an object. This is a common and powerful pattern:
62+
これは同じ結果を描画します。オブジェクトを返す[算出プロパティ](computed.md)に束縛することもできます。これは一般的で強力なパターンです:
6363

6464
```html
6565
<div :class="classObject"></div>
@@ -82,9 +82,9 @@ computed: {
8282
}
8383
```
8484

85-
### Array Syntax
85+
### 配列構文
8686

87-
We can pass an array to `:class` to apply a list of classes:
87+
`:class` に配列を渡してクラスのリストを適用することができます:
8888

8989
```html
9090
<div :class="[activeClass, errorClass]"></div>
@@ -99,33 +99,33 @@ data() {
9999
}
100100
```
101101

102-
Which will render:
102+
これは次のように描画されます:
103103

104104
```html
105105
<div class="active text-danger"></div>
106106
```
107107

108-
If you would like to also toggle a class in the list conditionally, you can do it with a ternary expression:
108+
リスト内のクラスを条件に応じて切り替えたい場合は、三項演算子式を使って実現することができます:
109109

110110
```html
111111
<div :class="[isActive ? activeClass : '', errorClass]"></div>
112112
```
113113

114-
This will always apply `errorClass`, but will only apply `activeClass` when `isActive` is truthy.
114+
この場合 `errorClass` は常に適用されますが、`activeClass` クラスは `isActive` が真と評価されるときだけ適用されます。
115115

116-
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:
116+
しかしながら、これでは複数の条件つきクラスがあると少し冗長になってしまいます。そのため、配列構文の内部ではオブジェクト構文を使うこともできます:
117117

118118
```html
119119
<div :class="[{ active: isActive }, errorClass]"></div>
120120
```
121121

122-
### With Components
122+
### コンポーネントにおいて
123123

124-
> This section assumes knowledge of [Vue Components](component-basics.md). Feel free to skip it and come back later.
124+
> このセクションでは、[Vue のコンポーネント](component-basics.md)の知識を前提としています。いったんスキップして後で戻ってきても構いません。
125125
126-
When you use the `class` attribute on a custom component with a single root element, those classes will be added to this element. Existing classes on this element will not be overwritten.
126+
単一のルート要素を持つカスタムコンポーネントで `class` 属性を使用すると、それらのクラスがこの要素に追加されます。この要素の既存のクラスは上書きされません。
127127

128-
For example, if you declare this component:
128+
例えば、このコンポーネントを宣言して:
129129

130130
```js
131131
const app = Vue.createApp({})
@@ -135,33 +135,33 @@ app.component('my-component', {
135135
})
136136
```
137137

138-
Then add some classes when using it:
138+
使用するときにいくつかのクラスを追加したとします:
139139

140140
```html
141141
<div id="app">
142142
<my-component class="baz boo"></my-component>
143143
</div>
144144
```
145145

146-
The rendered HTML will be:
146+
以下の HTML が描画されます:
147147

148148
```html
149149
<p class="foo bar baz boo">Hi</p>
150150
```
151151

152-
The same is true for class bindings:
152+
クラスバインディングに対しても同様です:
153153

154154
```html
155155
<my-component :class="{ active: isActive }"></my-component>
156156
```
157157

158-
When `isActive` is truthy, the rendered HTML will be:
158+
`isActive` が真と評価されるときは、以下の HTML が描画されます:
159159

160160
```html
161161
<p class="foo bar active">Hi</p>
162162
```
163163

164-
If your component has multiple root elements, you would need to define which component will receive this class. You can do this using `$attrs` component property:
164+
コンポーネントに複数のルート要素がある場合、どのコンポーネントがこのクラスを受け取るかを定義する必要があります。これは `$attrs` コンポーネントプロパティを使って行うことができます:
165165

166166
```html
167167
<div id="app">
@@ -180,13 +180,13 @@ app.component('my-component', {
180180
})
181181
```
182182

183-
You can learn more about component attribute inheritance in [Non-Prop Attributes](component-attrs.html) section.
183+
コンポーネント属性の継承については、[プロパティでない属性](component-attrs.html)のセクションを参照してください。
184184

185-
## Binding Inline Styles
185+
## インラインスタイルのバインディング
186186

187-
### Object Syntax
187+
### オブジェクト構文
188188

189-
The object syntax for `:style` is pretty straightforward - it looks almost like CSS, except it's a JavaScript object. You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names:
189+
`:style` 向けのオブジェクト構文は非常に簡単です。JavaScript オブジェクトということを除けば、ほとんど CSS のように見えます。CSS のプロパティ名には、キャメルケース (camelCase) またはケバブケース (kebab-case: クォートとともに使うことになります) のどちらでも使用することができます:
190190

191191
```html
192192
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
@@ -201,7 +201,7 @@ data() {
201201
}
202202
```
203203

204-
It is often a good idea to bind to a style object directly so that the template is cleaner:
204+
テンプレートをクリーンにするために、直接 style オブジェクトに束縛するのは、よいアイディアです:
205205

206206
```html
207207
<div :style="styleObject"></div>
@@ -218,26 +218,26 @@ data() {
218218
}
219219
```
220220

221-
Again, the object syntax is often used in conjunction with computed properties that return objects.
221+
ここでもまた、オブジェクト構文はしばしばオブジェクトを返す算出プロパティと併せて使用されます。
222222

223-
### Array Syntax
223+
### 配列構文
224224

225-
The array syntax for `:style` allows you to apply multiple style objects to the same element:
225+
`:style` 向けの配列構文は、同じ要素に複数のスタイルオブジェクトを適用することができます:
226226

227227
```html
228228
<div :style="[baseStyles, overridingStyles]"></div>
229229
```
230230

231-
### Auto-prefixing
231+
### 自動プリフィックス
232232

233-
When you use a CSS property that requires [vendor prefixes](https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix) in `:style`, for example `transform`, Vue will automatically detect and add appropriate prefixes to the applied styles.
233+
`:style`[ベンダー接頭辞](https://developer.mozilla.org/ja/docs/Glossary/Vendor_Prefix)を要求される CSS プロパティを使用するとき、例えば、`transform` においては、Vue.js は自動的に適切な接頭辞を検出し、適用されるスタイルに追加します。
234234

235-
### Multiple Values
235+
### 複数の値
236236

237-
You can provide an array of multiple (prefixed) values to a style property, for example:
237+
style プロパティに複数の (接頭辞付き) 値の配列を設定できます。例えば次のようになります:
238238

239239
```html
240240
<div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>
241241
```
242242

243-
This will only render the last value in the array which the browser supports. In this example, it will render `display: flex` for browsers that support the unprefixed version of flexbox.
243+
これは、配列内でブラウザがサポートしている最後の値だけを描画します。この例では、flexbox の接頭されていないバージョンをサポートしているブラウザでは `display: flex` を描画します。

0 commit comments

Comments
 (0)