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

vue.js style binding problem

一个新手
一个新手Original
2017-09-25 11:00:071192browse


  • 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>
  • Bind multiple classes

 v-bind:class="{ active: isActive, &#39;text-danger&#39;: hasError }">
  • 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>
  • 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>
  • 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
  }
})
  • 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>

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!

Statement:
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