vuejs toggle class – vue js v-class – Dynamic Components in Vuejs

vuejs toggle class – vue js v-class – Dynamic Components in Vuejs

We can pass some an object data to v-bind:class using vue to dynamically toggle switch on or off classes and A common reqired to need for your data binding is manipulating some an element’s add or remove class list and its new inline styles.

Dynamically Binding Styles

Vue.js some allows for binding styles suppoted and classes dynamically (add or remove class using vuejs) to elements with the directives of vuejs (v-bind:style) and (v-bind:class) directives.

Binding Dynamic multiple styles to an HTML element : Example 1

<button v-on:click="fontSize++">
  Increase your font size here in Example
</button>
<button v-on:click="fontSize--">
  Decrease your font size here in Example
</button>

<p v-bind:style="{ fontSize: fontSize + 'px' }">
  Your Dynamic Font size is: {{ fontSize }}
</p>

data: {
  isenable: true,
  isdisable: false
}

index.html : Example 2

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>VueJS Simple Example</title>
  <link rel='stylesheet prefetch' href='font-awesome.min.css'>
      <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <body class="text-center body"
     v-class="body-on: livecol, body-off: !livecol">
      <button v-on="click: flipSwitch"
              v-class="btn-danger: livecol, btn-success: !livecol"
              class="btn">
        <span class="fa fa-power-off"></span>
     </button>
</body>
  <script src='vue.min.js'></script>
    <script src="js/index.js"></script>
</body>
</html>

index.js

new Vue({
  el: 'body',
  data: {
    livecol: true
  },
  methods: {
    flipSwitch: function() {
      this.livecol = !this.livecol;
    }
  }
});

style.css

.body-on {
  background-color: #4285F4;
}
.body-off {
  background-color: #FBBC05;
}
.height-2 {
  min-height: 10em;
}
.btn {
  border-radius: 5em;
  height: 100px;
  width: 100px;
  outline: none;
  background: none 0;
  border: solid thin #444444;
  color: #eeeeee;
  font-size: 3em;
}
.btn-danger {
   background-color: purple;
   color: #4285F4;
}
.btn-success {
  background-color: green;
  color: #FBBC05;
}
.text-center {
   text-align: center;
}

Example

READ :  Vuejs Expressions – Numbers Strings Objects Array eval using Vuejs

Example

Add a Comment

Your email address will not be published. Required fields are marked *