美文网首页程序员
Vue.js 样式绑定

Vue.js 样式绑定

作者: InFatuated | 来源:发表于2020-09-24 17:25 被阅读0次

Vue.js class

class与style是HTML元素的属性,用于设置元素的样式,我们可以用v-bind来设置样式属性。
Vue.js v-bind在处理class和Style时,专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

class属性判定

我们可以为v-bind:class设置一个对象,从而动态的切换class;
实例1
实例中将isActive设置为true显示了一个绿色的div块,如果设置为false则不显示

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.active {
    width: 100px;
    height: 100px;
    background: green;
}
</style>
</head>
<body>
<div id="app">
  <div v-bind:class="{ 'active': isActive }"></div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    isActive: true
  }
})
</script>
</body>
</html>

以上实例div class为:

<div class="active"></div>

我们也可以在对象中传入更多属性用来动态切换多个class。
实例2
text-danger类背景颜色覆盖了active类的背景色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.active {
    width: 100px;
    height: 100px;
    background: green;
}
.text-danger {
    background: red;
}
</style>
</head>
<body>
<div id="app">
  <div class="static"
     v-bind:class="{ 'active': isActive, 'text-danger': hasError }">
  </div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    isActive: true,
    hasError: true
  }
})
</script>
</body>
</html>

实例3和实例2的渲染结果是一样的。
此外,我们也可以在这里绑定返回对象的计算属性,这是一个常用且强大的模式:
实例4

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.base {
  width: 100px;
  height: 100px;
}

.active {
  background: green;
}

.text-danger {
  background: red;
}
</style>
</head>
<body>
<div id="app">
  <div v-bind:class="classObject"></div>
</div>
<script>

new Vue({
  el: '#app',
  data: {
    isActive: true,
    error: {
      value: true,
      type: 'fatal'
    }
  },
  computed: {
    classObject: function () {
      return {
  base: true,
        active: this.isActive && !this.error.value,
        'text-danger': this.error.value && this.error.type === 'fatal',
      }
    }
  }
})
</script>
</body>
</html>

数组语法

我们可以把数组传给v-bind:class,实例如下:
实例5

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.active {
    width: 100px;
    height: 100px;
    background: green;
}
.text-danger {
    background: red;
}
</style>
</head>
<body>
<div id="app">
    <div v-bind:class="[activeClass, errorClass]"></div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    activeClass: 'active',
    errorClass: 'text-danger'
  }
})
</script>
</body>
</html>

以上实例div class为:

<div class="active text-danger"></div>

我们还可以使用三元表达式来切换列表中的class“

实例6
errorClass是始终存在的,isActive为true时添加activeClass类:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.text-danger {
    width: 100px;
    height: 100px;
    background: red;
}
.active {
    width: 100px;
    height: 100px;
    background: green;
}
</style>
</head>
<body>
<div id="app">
    <div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    isActive: true,
    activeClass: 'active',
    errorClass: 'text-danger'
  }
})
</script>
</body>
</html>

Vue.js style(内联样式)
我们可以在v-bind:style直接设置样式
实例7

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    activeColor: 'green',
    fontSize: 30
  }
})
</script>
</body>
</html>

以上实例div style为:

<div style="color : green;font-size:30px;">菜鸟教程</div>  

也可以直接绑定到一个样式对象,让模板更清晰:
实例8

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/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'
    }
  }
})
</script>
</body>
</html>

v-bind:style 可以使用数组将多个样式对象应用到一个元素上:
实例9

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/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'
    },
    overridingStyles: {
      'font-weight': 'bold'
    }
  }
})
</script>
</body>
</html>

注意:当v-bind:style使用需要特定前缀的CSS属性时,如transform,Vue.js会自动侦测并添加相应的前缀。

相关文章

  • vue:样式绑定

    Vue.js 样式绑定 Vue.js class class 与 style 是 HTML 元素的属性,用于设置元...

  • vue.js学习笔记四

    Vue.js 样式绑定Vue.js class class 与 style 是 HTML 元素的属性,用于设置元素...

  • Vue.js 样式绑定

    Vue.js class class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v...

  • vue.js 样式绑定

    style(外联样式) 语句: v-bind:class="classStyle"; classStyle可为对象...

  • Vue.js 样式绑定

    Vue.js class class与style是HTML元素的属性,用于设置元素的样式,我们可以用v-bind来...

  • Vue.js样式绑定

    概念:class 和 style 是HTML元素的属性,用于设置元素的样式;vue里提供了v-bind指令来动态的...

  • vuejs—绑定class和style样式

    上篇文章 介绍了通过vuejs计算属性,这篇中我们将一起学习vue.js实现绑定class和style样式。 绑定...

  • vuejs—条件渲染

    上一篇 介绍了通过vue.js绑定class和style样式,这次我们一起学习vue.js中的条件渲染。 v-if...

  • Vue.js 绑定内联样式

    这是本人学习《Vue.js实战》 (梁灏) 的笔记,并非原创,只是套着书本实际敲了一遍书中的代码,特此说明。 使...

  • v-bind:style样式绑定

    一、直接添加样式属性 二、绑定到样式对象 三、多样式绑定

网友评论

    本文标题:Vue.js 样式绑定

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