![]() |
完美的莴苣 · ASP.NET Core Blazor ...· 1 月前 · |
![]() |
坐怀不乱的绿茶 · Genome Biology | ...· 1 年前 · |
![]() |
霸气的单车 · 文本输入,js防注入,识别网址,清除富文本h ...· 1 年前 · |
![]() |
英俊的大脸猫 · 取决于通用规则的目标类型 - 问答 - ...· 1 年前 · |
class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。
Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。
我们可以为 v-bind:class 设置一个对象,从而动态的切换 class:
实例中将 isActive 设置为 true 显示了一个绿色的 div 块,如果设置为 false 则不显示:
以上实例 div class 为:
<div class="active"></div>
我们也可以在对象中传入更多属性用来动态切换多个 class 。
text-danger 类背景颜色覆盖了 active 类的背景色:
以上实例 div class 为:
<div class="static active text-danger"></div>
我们也可以直接绑定数据里的一个对象:
text-danger 类背景颜色覆盖了 active 类的背景色:
实例 3 与 实例 2 的渲染结果是一样的。
此外,我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式:
errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类:
我们可以在 v-bind:style 直接设置样式:
以上实例 div style 为:
<div style="color: green; font-size: 30px;">菜鸟教程</div>
也可以直接绑定到一个样式对象,让模板更清晰:
v-bind:style 可以使用数组将多个样式对象应用到一个元素上:
Mustache (双大括号写法)不能在 HTML 属性中使用,应使用 v-bind 指令:
<div v-bind:id="dynamicId"></div>
这对布尔值的属性也有效 —— 如果条件被求值为 false 的话该属性会被移除:
<button v-bind:disabled="someDynamicCondition">Button</button>
天龙冢
514***917@qq.com
北极熊
che***pu@126.com
北极熊
che***pu@126.com
QAQSaki
101***1907@qq.com
动态调节需要注意在 data 里面调用 data 的数据是 undefined 的,正确的使用方法是使用 computed。(使用 methods 返回无效,可能是不支持这样的设置)
<div id="app"> <div v-bind:style="{color: 'red', fontSize: size + 'px'}">可以动态调节</div> <div v-bind:style="computedStyle">可以动态调节</div> <div v-bind:style="objectStyle"> 不可以动态调节</div> <div v-bind:style="methodStyle"> 可以动态调节</div> {{size}} <button @click="++size">+</button> <button @click="--size">-</button> <script> var app = new Vue({ el: '#app', data: { size: 20, objectStyle: { color: 'green', fontSize: this.size + 'px' //this.size为undefined methods:{ methodStyle: function(){ return {color: 'green', fontSize: this.size + 'px'} //失效,颜色也不会改变 computed: { computedStyle: function(){ return {color: 'red' , fontSize: this.size + 'px'} </script>
henrys
782***792@qq.com
回复楼上,只要是能用 computed 的都能转换成 methods,但是用 methods 的时候就是一个方法,所以在属性后面的指向的应该是方法而不是对象,需要加上括号之后就能够生效了。
<div id="app"> <div v-bind:style="{color: 'red', fontSize: size + 'px'}">可以动态调节</div> <div v-bind:style="computedStyle">可以动态调节</div> <div v-bind:style="objectStyle"> 不可以动态调节</div> <div v-bind:style="methodStyle()"> 可以动态调节</div> {{size}} <button @click="++size">+</button> <button @click="--size">-</button> <script> var app = new Vue({ el: '#app', data: { size: 20, objectStyle: { color: 'green', fontSize: this.size + 'px' //this.size为undefined methods:{ methodStyle: function(){ return {color: 'green', fontSize: this.size + 'px'} //失效,颜色也不会改变 computed: { computedStyle: function(){ return {color: 'red' , fontSize: this.size + 'px'} </script>
henrys
782***792@qq.com
zss
zzu***js@qq.com
回复楼上,加一个 watch 方法,objectStyle 的方式也能实现动态变化。
<div id="app"> <div v-bind:style="{color: 'red', fontSize: size + 'px'}">可以动态调节</div> <div v-bind:style="computedStyle">可以动态调节</div> <div v-bind:style="objectStyle"> 可以动态调节</div> <div v-bind:style="methodStyle()"> 可以动态调节</div> {{size}} <button @click="++size">+</button> <button @click="--size">-</button> <script> var app = new Vue({ el: '#app', data: { size: 20, objectStyle: { color: 'green', fontSize: 20 + 'px' //this.size为undefined methods:{ methodStyle: function(){ return {color: 'green', fontSize: this.size + 'px'} //失效,颜色也不会改变 computed: { computedStyle: function(){ return {color: 'red' , fontSize: this.size + 'px'} watch: { size: function(){ this.objectStyle.fontSize = this.size + 'px' </script>
zss
zzu***js@qq.com
初学者
126***6357@qq.com
var myvue = new Vue({ el:'#mydiv', data:{ gettext: '', getclass1:true, getclass2:false, getp_style:true, // 这里的key是类名,value是boolean值。下面的vue+class的value才是类名,key是自定义的类的实例化对象。注意区分 styleobj:{ class1: true, class2: true, 'p-style': false // 这里也需要将带有连字符的样式用引号括起来 getdiv_style:{ // 定义样式调用条件 number1: true, number2: false // 下面几个其实可以理解为 伪类实例化 vueclass1: 'class1', vueclass2: 'class2', vuedivstyle: 'div-style', vuepstyle: 'p-style', // 动态改变字体大小 size: 15, fontstyle:{ // 绑定样式对象 fontSize: 25+'px', fontWeight: 'bold' colors: ['red','blue','cyan','pink','orange','yellow','black','white','purple'], // 动态改变颜色需要的颜色数组 colorstyle: { color: 'red', backgroundColor : 'blue', borderRadius: '20px' sizestyle:{ width: '300px', height: '100px' radiussize: 10 watch:{ size:function(){ this.fontstyle.fontSize = this.size+'px' gettext:function(str){ // 适用于只要填入内容就改变样式 str ? this.getdiv_style.number1 = false : this.getdiv_style.number1 = true methods:{ changecolor:function(){ // 改变颜色 this.colorstyle.backgroundColor = this.colors[Math.floor(Math.random()*this.colors.length)] this.colorstyle.color = this.colors[Math.floor(Math.random()*this.colors.length)] changesize:function(){ // 改变尺寸 this.sizestyle.width = parseInt(this.sizestyle.width)+50+'px' this.sizestyle.height = parseInt(this.sizestyle.height)+20+'px' computed:{ judegeuse:function(){ // 判断是否使用某个/些类 return { class1: false, class3: true, 'div-style': this.getdiv_style.number1&&!this.getdiv_style.number2 // 这里也需要将带有连字符的样式用引号括起来初学者3年前 (2019-12-11)初学者
126***6357@qq.com