08.02.2017 Views

vuejs

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7. Class and Style Bindings<br />

7.1 Class binding<br />

7.1.1 Object Syntax<br />

A common need for data binding is manipulating an element’s class and its styles. For such cases,<br />

you can use v-bind:class. This can be used to apply classes conditionally, toggle them and/or apply<br />

many of them using one binded object et al.<br />

The v-bind:class directive takes an object with the following format as an argument<br />

1 {<br />

2 'classA': true,<br />

3 'classB': false,<br />

4 'classC': true<br />

5 }<br />

and applies all classes with true value to the element. For example, the following element will have<br />

classA and classC classes.<br />

1 <br />

1 data: {<br />

2 elClasses:<br />

3 {<br />

4 'classA': true,<br />

5 'classB': false,<br />

6 'classC': true<br />

7 }<br />

8 }<br />

To demonstrate how v-bind is used with class attributes, we are going to make an example of<br />

class toggling. Using v-bind:class directive, we are going to dynamically toggle the class of body<br />

element.<br />

83

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!