vue动态类名及动态样式设置方式:class/:style
更新时间:2022年05月25日 10:33:46 作者:·半傻半呆半疯癫
文章主要介绍了在Vue中设置动态类名(:class)和动态样式(:style)的方法,动态类名方面,使用三元表达式、结合过滤器和计算属性等方法进行设置,动态样式方面,介绍了基础用法、结合计算属性和三元表达式进行设置等方法
Vue动态类名(:class)、动态样式(:style) 的设置
以下是个人在项目中使用过的关于在Vue中的动态类名和动态样式的设置方法的整理记录
动态类名(:class)的一些用法
- 三元表达式判断
:class="address.length > 0 ? 'city' : 'city-gray'"
:class="{ 'is-active': form.avatar == i }"
:class="[
sizeClass ? 'el-warning--' + sizeClass : '',
{
'is-no-spacing': this.noSpacingClass,
},
]"
:class="[flexLeft ? 'expand-left' : 'expand-middle']"
- 涉及太多的需求的,结合过滤器(filters)使用
// template中
<div :class="item.gameList | colStyle" v-if="item.gameList.length > 0" class="game-list">
// script中
filters: {
colStyle(data) {
if (_.isEmpty(data)) {
return '';
}
const { length } = data;
let className = '';
if (length === 1) {
className = 'two-col';
}
if (length === 2) {
className = 'two-col';
}
if (length === 3) {
className = 'three-col';
}
if (length >= 4) {
className = 'four-col';
}
return className;
},
},
// style中
.two-col {}
.three-col {}
- 单独组件中
HTML中
:class="[`startTheme-${themeConfig.label}`]"
Style中
.startTheme-green { color: green; }
.startTheme-red { color: red; }
- 常用于公共组件中的(下面是一个示例)
<template>
<div
:class="{
'disabled-view': disabled,
[`button-${this.type}-view`]: type,
[`button-${this.size}`]: size,
}"
@click="onClick"
@keydown.enter="onClick"
class="seek-top-button-view"
>
<Loading v-if="hasLoading" class="loading-view" type="spinner" />
<slot />
</div>
</template>
<script>
import { Loading } from 'vant';
export default {
name: 'StButton',
components: {
Loading,
},
directives: {
focus: {
inserted(el) {
el.focus();
},
},
},
props: {
color: {
type: String,
default: '',
},
size: {
type: String,
default: 'large', // large/small
},
loading: {
type: Boolean,
default: false,
},
shadow: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
type: {
type: String,
default: 'primary', // primary / text /default/danger
},
},
data() {
return {};
},
computed: {
opacity() {
if (this.loading) return 0.69;
return 1;
},
hasLoading() {
return this.loading && this.type !== 'text';
},
},
methods: {
onClick(event) {
if (this.loading || this.disabled) return;
this.$emit('click', event);
},
},
};
</script>
<style lang="scss" scoped>
// 这里只展示部分作为参考
&.button-default-view {
//白色按钮
background: $--color-white;
color: $--color-red;
}
&.button-danger-view {
//字体边框红色
border: 1px solid $--color-red;
color: $--color-red;
background: transparent;
}
</style>
动态样式(:style)的一些用法
- 基础用法
:style="{
width: itemWidth + 'px',
height: itemHeight + 'px',
left: left + 'px',
top: top + 'px',
}"
- 结合计算属性一起使用
:style="{
opacity,
}"
computed: {
opacity() {
if (this.loading) return 0.69;
return 1;
},
},
- 三元表达式
:style="{ 'padding-top': search ? '44px' : '' }"
:style="$parent.value === id ? activeStyle : {}"
computed: {
activeStyle() {
return {
color: this.$parent.activeColor,
};
},
},
:style="'background: url(' + require(`./img/bgCheck_${tabCheck === index ? 1 : 0}.png`) +')no-repeat'"
- 动态配置背景颜色、背景图片
<div
class="main__header"
:style="
'background: ' +
`${themeConfig.themeColor}` +
' url(' +
require(`@/assets/themeCofing/${themeConfig.label}/personalInfo/header_bg.png`) +
')no-repeat center / contain;'
"
/>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
解决v-for中使用v-if或者v-bind:class失效的问题
今天小编就为大家分享一篇解决v-for中使用v-if或者v-bind:class失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-09-09


最新评论