vue动态类名及动态样式设置方式:class/:style

 更新时间:2022年05月25日 10:33:46   作者:·半傻半呆半疯癫  
文章主要介绍了在Vue中设置动态类名(:class)和动态样式(:style)的方法,动态类名方面,使用三元表达式、结合过滤器和计算属性等方法进行设置,动态样式方面,介绍了基础用法、结合计算属性和三元表达式进行设置等方法

Vue动态类名(:class)、动态样式(:style) 的设置

以下是个人在项目中使用过的关于在Vue中的动态类名和动态样式的设置方法的整理记录

动态类名(:class)的一些用法

  1. 三元表达式判断
: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']"
  1. 涉及太多的需求的,结合过滤器(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 {}
  1. 单独组件中
HTML中
:class="[`startTheme-${themeConfig.label}`]"

Style中
.startTheme-green { color: green; }
.startTheme-red { color: red; }
  1. 常用于公共组件中的(下面是一个示例)
<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)的一些用法

  1. 基础用法
:style="{
      width: itemWidth + 'px',
      height: itemHeight + 'px',
      left: left + 'px',
      top: top + 'px',
    }"
  1. 结合计算属性一起使用
:style="{
      opacity,
    }"

computed: {
      opacity() {
          if (this.loading) return 0.69;
          return 1;
      },
},
  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'"
  1. 动态配置背景颜色、背景图片
<div
      class="main__header"
      :style="
        'background: ' +
        `${themeConfig.themeColor}` +
        ' url(' +
        require(`@/assets/themeCofing/${themeConfig.label}/personalInfo/header_bg.png`) +
        ')no-repeat center / contain;'
      "
   />

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 使用Vue实现简易的车牌输入键盘

    使用Vue实现简易的车牌输入键盘

    这篇文章主要为大家详细介绍了如何使用Vue实现简易的车牌输入键盘效果,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解下
    2023-11-11
  • Vue中使用v-print打印出现空白页问题及解决

    Vue中使用v-print打印出现空白页问题及解决

    这篇文章主要介绍了Vue中使用v-print打印出现空白页问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Vue实现将页面区域导出为pdf

    Vue实现将页面区域导出为pdf

    文章介绍了两种将前端页面指定区域导出为PDF的纯前端实现方法,方式一使用jsPDF和html2canvas将特定区域转化为图片,再将图片转化为PDF,适用于小区域转换,但存在翻页时内容拆分的问题,方式二使用html2pdf.js,支持自动分页,适用于较大区域的转换
    2025-10-10
  • Vue 滚动行为的具体使用方法

    Vue 滚动行为的具体使用方法

    本篇文章主要介绍了Vue 滚动行为的具体使用方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Vue提示框组件vue-notification使用详解

    Vue提示框组件vue-notification使用详解

    这篇文章主要介绍了Vue提示框组件vue-notification使用详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • 解决v-for中使用v-if或者v-bind:class失效的问题

    解决v-for中使用v-if或者v-bind:class失效的问题

    今天小编就为大家分享一篇解决v-for中使用v-if或者v-bind:class失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • Vue插槽slot全部使用方法示例解析

    Vue插槽slot全部使用方法示例解析

    插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签,这篇文章主要介绍了Vue插槽的理解和使用,需要的朋友可以参考下
    2023-03-03
  • elementUI组件el-dropdown(踩坑)

    elementUI组件el-dropdown(踩坑)

    本文主要介绍了elementUI组件el-dropdown的一些坑,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • vue实现多栏布局拖拽

    vue实现多栏布局拖拽

    这篇文章主要为大家详细介绍了vue实现多栏布局拖拽,改变盒子的宽度,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • vue模块拖拽实现示例代码

    vue模块拖拽实现示例代码

    这篇文章主要介绍了vue模块拖拽实现示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论