美文网首页
v-bind:style样式绑定

v-bind:style样式绑定

作者: 新篇章 | 来源:发表于2018-03-05 13:42 被阅读0次

    一、直接添加样式属性

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
        <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px',background:'red' }">内联样式</div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        activeColor: 'green',
        fontSize: 30
      }
    })
    </script>
    </body>
    </html>
    

    二、绑定到样式对象

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <div v-bind:style="styleObject">绑定样式对象</div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        styleObject: {
          color: 'green',
          fontSize: '30px',
          background:'red'
        }
      }
    })
    </script>
    </body>
    </html>
    

    三、多样式绑定

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <div v-bind:style="[baseStyles, overridingStyles]">多样式绑定</div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        baseStyles: {
          color: 'green',
          fontSize: '30px',
          background:'red'
        },
        overridingStyles: {
          'font-weight': 'bold'
        }
      }
    })
    </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:v-bind:style样式绑定

          本文链接:https://www.haomeiwen.com/subject/eochfftx.html