Home > Web Front-end > JS Tutorial > vue.js style binding problem

vue.js style binding problem

一个新手
Release: 2017-09-25 11:00:07
Original
1302 people have browsed it


  • class and style are attributes of HTML elements and are used to set the style of the element. We can use v-bind to set the style attributes.

  • The following example sets the class style in the style tag. There is only one Boolean value isActive in the vue instance, using v-bind:class="{ active: isActive }" Bind the style and decide whether to render based on the Boolean value.

<style>.active {    
    width: 100px;    
    height: 100px;    
    background: green;}</style><p id="app">
  <p v-bind:class="{ active: flag}"></p></p><script>new Vue({
  el: &#39;#app&#39;,
  data: {
    flag: true
  }
})
</script>
Copy after login
  • Bind multiple classes

 v-bind:class="{ active: isActive, &#39;text-danger&#39;: hasError }">
Copy after login
  • Bind multiple classes in the form of objects class

<p id="app">
  <p v-bind:class="classObject"></p></p><script>new Vue({
  el: &#39;#app&#39;,
  data: {
    classObject: {
      active: true,      
      &#39;text-danger&#39;: true
    }
  }
})
</script>
Copy after login
  • Bind the style as an array

<p id="app">
    <p v-bind:class="[activeClass, errorClass]"></p></p><script>new Vue({
  el: &#39;#app&#39;,
  data: {
    activeClass: &#39;active&#39;,
    errorClass: &#39;text-danger&#39;
  }
})
</script>
Copy after login
  • Inline style Binding

<p id="app">
    <p v-bind:style="{ color: activeColor, fontSize: fontSize + &#39;px&#39; }">菜鸟教程</p></p><script>new Vue({
  el: &#39;#app&#39;,
  data: {
    activeColor: &#39;green&#39;,
    fontSize: 30
  }
})
Copy after login
  • Bind inline style using object method

<p id="app">
  <p v-bind:style="styleObject">菜鸟教程</p></p><script>new Vue({
  el: &#39;#app&#39;,
  data: {
    styleObject: {
      color: &#39;green&#39;,
      fontSize: &#39;30px&#39;
    }
  }
})
</script>
Copy after login

The above is the detailed content of vue.js style binding problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template