vue.js样式绑定

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>
时间: 2024-08-02 00:17:45

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

10.Vue.js 样式绑定

Vue.js 样式绑定 Vue.js class class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性. Vue.js v-bind 在处理 class 和 style 时, 专门增强了它.表达式的结果类型除了字符串之外,还可以是对象或数组. class 属性绑定 我们可以为 v-bind:class 设置一个对象,从而动态的切换 class: 实例中将 isActive 设置为 true 显示了一个绿色的 div 块,如果设置为 f

VUE指令-样式绑定v-bind

样式绑定v-bind 操作元素的 class 列表和内联样式是数据绑定的一个常见需求.因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可.不过,字符串拼接麻烦且易错.因此,在将 v-bind 用于 class 和 style 时,Vue.js 做了专门的增强.表达式结果的类型除了字符串之外,还可以是对象或数组. v-bind:style <!-- 格式v-bind:style="{ key:value }" --> <!--

一起学Vue之样式绑定

在前端开发中,设置元素的 class 列表和内联样式是基本要求.本文主要讲解Vue开发中,样式列表和内联样式的绑定,仅供学习分享使用,如果有不足之处,还请指正. 概述 Vue操作元素的 class 列表和内联样式是数据绑定的一个常见需求.因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可.不过,字符串拼接麻烦且易错.因此,在将 v-bind 用于 class 和 style 时,Vue.js 做了专门的增强.表达式结果的类型除了字符串之外,还可以是对象

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

Vue.js最核心的功能有两个,一是响应式的数据绑定系统,二是组件系统.本文仅探究几乎所有Vue的开篇介绍都会提到的hello world双向绑定是怎样实现的.先讲涉及的知识点,再参考源码,用尽可能少的代码实现那个hello world开篇示例. 参考文章:https://segmentfault.com/a/1190000006599500 一.访问器属性 访问器属性是对象中的一种特殊属性,它不能直接在对象中设置,而必须通过defineProperty()方法单独定义. var obj = {

Vue.js双向绑定原理

Vue.js最核心的功能有两个,一个是响应式的数据绑定系统,另一个是组件系统.本文仅仅探究双向绑定是怎样实现的.先讲涉及的知识点,再用简化的代码实现一个简单的hello world示例. 一.访问器属性 访问器属性是对象中的一种特殊属性,它不能直接在对象中设置,而必须通过defineProperty()方法单独定义. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl

vue.js之绑定class和style

一.绑定Class属性. 绑定数据用v-bind:命令,简写成:, 语法:<div v-bind:class="{ active: isActive }"></div>.class后面的双引号里接受一个对象字面量/对象引用/数组作为参数, 这里,{active: isActive}是对象参数,active是class名,isActive是一个布尔值.下面是一个例子: 绑定对象字面量 html: <div id="classBind"&g

vue.js 事件绑定

1.添加事件监听 v-on (1)使用v-on指令可以添加事件监听,语法: v-on:eventName="fn" 也可以简写成 @:eventName="fn" (2)参数:$event就是当前触发事件的元素,即使不传$event,在回调函数中也可以使用event这个参数 基本使用: <div id="app"> <button v-on:click="test">点我</button>

Vue.js 2.0 学习重点记录

Vue.js兼容性 Vue.js.js 不支持 IE8 及其以下版本,因为 Vue.js.js 使用了 IE8 不能模拟的 ECMAScript 5 特性. Vue.js.js 支持所有兼容 ECMAScript 5 的浏览器. Vue.js安装 安装方式有两种: 1.直接下载用script标签引入(推荐引入开发版本,压缩版本会失去错误提示和警告) 2.使用cdn方法引入 3.npm安装 npm安装的前提需要安装了nodejs,这里推荐nvm安装nodejs 附上一小段nvm使用的命令: $ n

学习vue.js的自我梳理笔记

基本语法格式: <script> new Vue({ el: '#app', data: { url: 'http://www.runoob.com' } }) </script> 指令 [指令是带有 v- 前缀的特殊属性.] 判断  <p v-if="seen">现在你看到我了</p> 参数  <a v-bind:href="url">菜鸟教程</a> 监听  v-on 指令,它用于监听 DO