当前位置: 首页 > 编程笔记 >

vue绑定class与行间样式style详解

荆煌
2023-03-14
本文向大家介绍vue绑定class与行间样式style详解,包括了vue绑定class与行间样式style详解的使用技巧和注意事项,需要的朋友参考一下

一、绑定class属性的方式

1、通过数组的方式,为元素绑定多个class

<style>
  .red {
    color:red;
    /*color:#ff8800;*/
  }
  .bg {
    background: #000;
    /*background: green;*/
  }
  </style>


  window.onload = function(){
    var c = new Vue({
      el : '#box',
      data : {
        red_color : 'red',
        bg_color : 'bg'
      }
    });
  }



  <div id="box">
    <span :class="[red_color,bg_color]">this is a test string</span>
  </div>

上例为span 绑定2个class,通过设定red_color和bg_color的值,找到对应的class类名

2、通过控制class的true或者false,来使用对应的class类名, true: 使用该class,  false: 不使用该class

<style>
  .red {
    color:red;
  }
  .bg {
    background: #000;
  }
  </style>

  window.onload = function(){
    var c = new Vue({
      el : '#box',
      data : {
      }
    });
  }
  
  <div id="box">
    <span :class="{red:true,bg:true}">this is a test     string</span>
  </div>

3、这种方式,跟第二种差不多,只不过是把class的状态true/false用变量来代替

<style>
  .red {
    color:red;
  }
  .bg {
    background: #000;
  }
  </style>

  window.onload = function(){
    var c = new Vue({
      el : '#box',
      data : {
        r : true,
        b : false
      }
    });
  }

  <div id="box">
    <span :class="{red:r,bg:b}">this is a test string</span>
  </div>

4、为class属性绑定整个json对象

<style>
  .red {
    color:red;
  }
  .bg {
    background: #000;
  }
  </style>

  window.onload = function(){
    var c = new Vue({
      el : '#box',
      data : {
        json : {
          red : true,
          bg : false
        }
      }
    });
  }


  <div id="box">
    <span :class="json">this is a test string</span>
  </div>

 二、绑定style行间样式的多种方式

1、使用json格式,直接在行间写

window.onload = function(){
    var c = new Vue({
      el : '#box',
    });
  }

   <div id="box">
    <span :style="{color:'red',background:'#000'}">this is a test string</span>
  </div>

2、把data中的对象,作为数组的某一项,绑定到style

window.onload = function(){
    var c = new Vue({
      el : '#box',
      data : {
        c : {
          color : 'green'
        }
      }
    });
  }

  <div id="box">
    <span :style="[c]">this is a test string</span>
  </div>

3、跟上面的方式差不多,只不过是为数组设置了多个对象

window.onload = function(){
    var c = new Vue({
      el : '#box',
      data : {
        c : {
          color : 'green'
        },
        b : {
          background : '#ff8800'
        }
      }
    });
  }
<div id="box">
     <span :style="[c,b]">this is a test string</span>
  </div>

4、直接把data中的某个对象,绑定到style

window.onload = function(){
    var c = new Vue({
      el : '#box',
      data : {
        a : {
          color:'yellow',
          background : '#000'
        }
      }
    });
  }
<div id="box">
     <span :style="a">this is a test string</span>
</div>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。因为它们都是属性 ,我们可以用v-bind 处理它们:只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在 v-bind 用于 class 和 style 时, Vue.js 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。 绑定 HTML Class 对象语法 我们可以传给 v-bind:clas

  • 本文向大家介绍vue.js绑定class和style样式(6),包括了vue.js绑定class和style样式(6)的使用技巧和注意事项,需要的朋友参考一下 数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。因为它们都是 attribute,我们可以用 v-bind 处理它们:只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在 v-bind 用于 class 和

  • 本文向大家介绍学习vue.js中class与style绑定,包括了学习vue.js中class与style绑定的使用技巧和注意事项,需要的朋友参考一下 关于vue.js中class与style绑定的练习代码,分享给大家,供大家参考: html:  js:  css: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍在Vue中如何动态绑定class样式?相关面试题,主要包含被问及在Vue中如何动态绑定class样式?时的应答技巧和注意事项,需要的朋友参考一下

  • 1. 简介 本小节我们将介绍 Vue 中如何动态绑定样式。包括 Class 的绑定、内联样式 Style 的绑定。掌握样式绑定的多种形式是其中的重点难点。同学们可以在学完本小节之后对样式的绑定方式加以总结,再通过反复的练习来加深印象。 2. 慕课解释 操作元素的 class 列表和内联样式是数据绑定的一个常见需求。因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符

  • 本文向大家介绍Vue.js每天必学之Class与样式绑定,包括了Vue.js每天必学之Class与样式绑定的使用技巧和注意事项,需要的朋友参考一下 数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。因为它们都是 attribute,我们可以用 v-bind 处理它们:只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在 v-bind 用于 class 和 style