Class and Style Bindings
Share:
VueJS, a progressive JavaScript framework, offers a highly interactive way to bind styles and classes dynamically to elements in the application. This chapter will delve into the details of Style and Class Bindings, unpacking the concept and explaining how to leverage them in your applications effectively. To explore this feature in the context of a relatable example, we will use a fictitious movie application where users can view and rate various films.
VueJS Class Bindings
Class bindings are a unique feature of VueJS that lets you dynamically bind classes to the HTML elements within the VueJS application. You can even use it to toggle classes on and off, based on the truthiness of the data property.
Let's assume that we have a page displaying information about a movie called 'Space Wars'. The div for the movie will have a class 'viewed' when a user has already watched it. This is how you would bind that class with VueJS:
<div id="movie" v-bind:class="{ viewed: isViewed }">
Space Wars
</div>
var vm = new Vue({
el: '#movie',
data: {
isViewed: true
}
})
In this code snippet, 'viewed' is the name of the CSS class, and 'isViewed' is the Vue instance data property. The class 'viewed' will only be applied if 'isViewed' property comes out to be true.
VueJS also makes it possible to bind multiple classes at the same time. Let's consider another situation: the movie 'Space Wars' has a 'new' tag when it has been recently added to the collection, and a 'recommended' tag if it's highly rated. The code will look like:
<div id="movie" v-bind:class="{ new: isNew, recommended: isRecommended }">
Space Wars
</div>
var vm = new Vue({
el: '#movie',
data: {
isNew: true,
isRecommended: false
}
})
VueJS Style Bindings
While class bindings allow for dynamic class toggling, style bindings facilitate inline styling of elements. This is useful for dynamic styling with JavaScript. For instance, if users rate movies, you could display these movies with varying opacity levels based on their ratings.
Here's an example:
<div id="movie" v-bind:style="{ opacity: movieRating }">
Space Wars
</div>
var vm = new Vue({
el: '#movie',
data: {
movieRating: 0.8
}
})
The 'v-bind:style' directive is used to apply inline styles, where 'opacity' is the CSS property and 'movieRating' is the Vue instance's data property. The movie box's opacity will be set to 0.8 in this case.
Also, like with class binding, Vue allows multiple style bindings. Let's imagine we also want to change the color of the movie title to denote if it's a new or old release:
<div id="movie" v-bind:style="{ opacity: movieRating, color: movieAge }">
Space Wars
</div>
var vm = new Vue({
el: '#movie',
data: {
movieRating: 0.8,
movieAge: 'green'
}
})
Array Syntax for Class Binding
VueJS also offers array syntax to apply multiple classes to an element. Suppose a movie has 'popular' and 'recommended' badges. The code below demonstrates how to apply these classes using array syntax:
<div id="movie" v-bind:class="[popular, recommended]">
Space Wars
</div>
var vm = new Vue({
el: '#movie',
data: {
popular: 'popular-badge',
recommended: 'recommended-badge'
}
})
This code applies both 'popular-badge' and 'recommended-badge' classes to the element.
Understanding class and style bindings in VueJS is paramount for creating dynamic and user-friendly Vue applications. However, the use cases outlined here are quite broad, and your requirements might be more specific or extensive. The beauty of VueJS is that its simplicity and flexibility allow it to cater to a variety of needs, and the class and style bindings are just a taste of it. Keep practicing and exploring different ways to create more interactive applications using VueJS.
0 Comment
Sign up or Log in to leave a comment