vue.js样式绑定

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

Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。 class 属性绑定

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>style of vue</title>
<script src='vue.min.js'></script>
<style>
.active {
width: 100px;
height: 100px;
background: green;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="{active}"> </div>
</div>
<script>
new Vue({
el: '#app',
data: {
active: true
}
})
</script>
</body>
</html>
  • 我们也可以在对象中传入更多属性用来动态切换多个class. text-danger类背景颜色覆盖了active类的颜色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>style of vue</title>
<script src='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, 'text-danger': hasError }">
</div>
</div> <script>
new Vue({
el: '#app',
data: {
active: true,
hasError: true
}
})
</script>
</body>
</html>
  • 我们也可以直接绑定数据里的一个对象:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>object of vue style</title>
<script src="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="classObject"></div>
</div>
<script>
new Vue({
el: '#app',
data: {
classObject: {
active: true,
'text-danger': true
}
}
})
</script>
</body>
</html>
  • 我们也可以在这里绑定返回对象的计算机属性。这是一个常用且强大的模式:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>computed of vue style</title>
<script src="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="classObject"></div>
</div>
<script>
new Vue({
el: '#app',
data: {
isActive: true,
error: null
},
computed: {
classObject: function() {
return {
active: this.isActive && !this.error,
'text-danger': this.error && this.error.type === 'fatal',
}
}
}
})
</script>
</body>
</html>

数组语法

  • 我们可以把一个数组传给v-bind:class,实例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>array of vue style</title>
<script src="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>
  • errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类:
 <div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>

Vue.js style(内联样式)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>line style of vue</title>
<script src="vue.min.js"></script>
</head>
<body>
<div id="app">
<div v-bind:style="{color: activeColor, fontSize: fontSize + 'px' }">vue学习</div>
</div>
<script>
new Vue({
el: '#app',
data: {
activeColor: 'green',
fontSize: '30'
}
})
</script>
</body>
<body>
  • 也可以直接绑定一个样式对象,让模板更清晰:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue Object of style</title>
<script src="vue.min.js"></script>
</head>
<body>
<div id="app">
<div v-bind:style="styleObject">vue 学习</div>
</div> <script>
new Vue({
el: '#app',
data: {
styleObject: {
color: 'green',
fontSize: '30px'
}
}
})
</script>
</body>
</html>
  • v-bind:style可以使用数组将多个样式对象应用到一个元素上:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue style</title>
<script src="vue.min.js"></script>
</head>
<body>
<div id="app">
<div v-bind:style="[baseStyles, overridingStyles]">vue 学习</div>
</div> <script>
new Vue({
el: '#app',
data: {
baseStyles: {
color: 'green',
fontSize: '30px'
},
overridingStyles: {
'font-weight': 'bold'
}
}
})
</script>
</body>
</html>

vue.js样式绑定的更多相关文章

  1. 10.Vue.js 样式绑定

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

  2. Vue.js 样式绑定(1)

    demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  3. vue.js 样式绑定

    简单用法 <div v-bind:height="bindStyle"> 复杂用法 <div v-bind:style="bindStyle" ...

  4. Vue.js双向绑定的实现原理和模板引擎实现原理(##########################################)

    Vue.js双向绑定的实现原理 解析 神奇的 Object.defineProperty 这个方法了不起啊..vue.js和avalon.js 都是通过它实现双向绑定的..而且Object.obser ...

  5. vue的样式绑定

    vue在样式绑定,看这官方的文档,怎么试都不行后来看了一篇文章 <div :class="[rankClass]"></div> <script> ...

  6. Vue 将样式绑定到一个对象让模板更清晰

    Vue 将样式绑定到一个对象让模板更清晰 <div id="app"> <div v-bind:style="styleObject"> ...

  7. 一起学Vue之样式绑定

    在前端开发中,设置元素的 class 列表和内联样式是基本要求.本文主要讲解Vue开发中,样式列表和内联样式的绑定,仅供学习分享使用,如果有不足之处,还请指正. 概述 Vue操作元素的 class 列 ...

  8. Vue.js双向绑定的实现原理

    Vue.js最核心的功能有两个,一是响应式的数据绑定系统,二是组件系统.本文仅探究几乎所有Vue的开篇介绍都会提到的hello world双向绑定是怎样实现的.先讲涉及的知识点,再参考源码,用尽可能少 ...

  9. Vue.js双向绑定原理

    Vue.js最核心的功能有两个,一个是响应式的数据绑定系统,另一个是组件系统.本文仅仅探究双向绑定是怎样实现的.先讲涉及的知识点,再用简化的代码实现一个简单的hello world示例. 一.访问器属 ...

随机推荐

  1. Unity 带骨骼的人体模型换装

    直入主题: 1.实验材料 两个模型,虽然缺胳膊少腿的,但是能用!!! 2.条件 两个模型在制作时是基于同一套骨骼,导出模型部位时连着该部位的骨骼一起导出,这样导入到Unity的模型就带有Skinned ...

  2. hihocoder 1015 : KMP算法(kmp)

    传送门 Description 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一只河蟹,于是河蟹就向小H ...

  3. python3标准库总结

    Python3标准库 操作系统接口 os模块提供了不少与操作系统相关联的函数. ? 1 2 3 4 5 6 >>> import os >>> os.getcwd( ...

  4. PHPer面试指南-laravel 篇

    简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件. 注册类文件自动加载器 : Laravel通过 composer ...

  5. angularJS拦截路由

    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams)

  6. [BZOJ3932][CQOI2015]任务查询系统(差分+主席树)

    题面 分析 对于一个区间修改(s,e,v),我们可以将它差分,这样就变成了单点修改s和e+1(s插入,t+1删除) 我们用主席树维护差分数组的前缀和,第i棵主席树维护区间[1,i]之间的所有差分值 那 ...

  7. Python : Data Encapsulation

    Python : Data Encapsulation The following table shows the different behaviour: Name Notation Behavio ...

  8. jquery实现按键增加删除css属性(hide)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. bzoj3097 Hash Killer I

    Hash Killer I Time Limit: 5 Sec Memory Limit: 128 MBSec Special Judge Description 这天天气不错,hzhwcmhf神犇给 ...

  10. opencv视频流的读取和处理

    Opencv提供一个简单易用的框架以提取视频文件和USB摄像头中的图像帧,如果只是想读取某个视频,你只需要创建一个VideoCapture实例,然后在循环中提取每一帧.下面是一个简单的代码 #incl ...