网站添加ico,5g天天奭5g天天运动网站代码,厦门酒店团购网站建设,app应用公司一、:class 动态绑定类名
v-bind:class#xff08;缩写为 :class#xff09;可以动态地绑定一个或多个 CSS 类名。
1. 对象语法
通过对象语法#xff0c;可以根据条件动态切换类名。
templatediv :class{ greenText: isActive, red-text: hasError }缩写为 :class可以动态地绑定一个或多个 CSS 类名。
1. 对象语法
通过对象语法可以根据条件动态切换类名。
templatediv :class{ greenText: isActive, red-text: hasError }海绵宝宝不喜欢侬喜欢章鱼哥。/div
/templatescript
export default {data() {return {isActive: true,hasError: false,};},
};
/scriptstyle
.greenText {color: green;
}
.red-text {color: red;
}
/stylegreenText当 isActive 为 true 时添加 greenText 类。red-text当 hasError 为 true 时添加 red-text 类。
效果图
2. 数组语法
通过数组语法可以同时绑定多个类名。
templatediv :class[textClass, bgcClass]海绵宝宝不喜欢侬喜欢章鱼哥。/div
/templatescript
export default {data() {return {textClass: greenText,bgcClass: pinkBgc,};},
};
/scriptstyle
.greenText {color: green;
}
.pinkBgc {width: 300px;height: 200px;background-color: pink;margin: 200px auto;
}
/styletextClass 和 bgcClass 是数据属性它们的值会同时作为类名绑定到元素上。
效果图
3. 结合计算属性
当类名的逻辑较为复杂时可以使用计算属性来动态生成类名对象。
templatediv :classcomputedClass海绵宝宝不喜欢侬喜欢章鱼哥。/div
/templatescript
export default {data() {return {isActive: true,hasError: true};},computed: {computedClass() {return {greenText: this.isActive !this.hasError,text-red: this.hasError};}}
};
/scriptstyle
.greenText {color: green;
}
.text-red{color: red;
}
/stylegreenTextisActive 为true并且hasError为false的时候生效text-redhasError 为true的时候生效 效果图
二、:style 动态绑定内联样式
v-bind:style缩写为 :style可以动态地绑定内联样式。
1. 对象语法
通过对象语法可以直接绑定样式对象。
templatediv :style{ color: activeColor, fontSize: fontSize px }海绵宝宝不喜欢侬喜欢章鱼哥。/div
/templatescript
export default {data() {return {activeColor: red,fontSize: 12};},
};
/scriptactiveColor 和 fontSize 是数据属性它们的值会作为样式绑定到元素上。 效果图
2. 数组语法
通过数组语法可以同时绑定多个样式对象。
templatediv :style[styles1, styles2]海绵宝宝不喜欢侬喜欢章鱼哥。/div
/templatescript
export default {data() {return {styles1: {color: red,fontSize: 14px},styles2: {fontWeight: bold,textDecoration: underline}};},
};
/scriptstyles1 和 styles2 的所有样式都会绑定到元素上。 效果图
3. 使用三元表达式
可以在 :style 中使用三元表达式根据条件动态设置样式值。
templatediv :style{ color: isActive ? green : red }海绵宝宝不喜欢侬喜欢章鱼哥。/div
/templatescript
export default {data() {return {isActive: true};},
};
/script效果图
4. 使用模板字符串
可以使用模板字符串动态拼接样式值。
templatediv :stylecolor: ${isActive ? green : red}; font-size: ${fontSize}px classdemo海绵宝宝不喜欢侬喜欢章鱼哥。/div
/templatescript
export default {data() {return {isActive: false,fontSize: 12};},
};
/script效果图
祝大家新年快乐 文章转载自: http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.nyhtf.cn.gov.cn.nyhtf.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.wnbqy.cn.gov.cn.wnbqy.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.pwmm.cn.gov.cn.pwmm.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.skrcn.cn.gov.cn.skrcn.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn