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

6 篇文章 0 订阅

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;'
      "
   />

以上仅供大家参考

:ஐ٩(๑´ᵕ`)۶ஐ: 学习使我进步

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以使用以下代码示例创建一个可以应用于其他页面的 Tabbar 底部导航栏组件: ```vue <template> <div class="tabbar"> <router-link v-for="tab in tabbarList" :key="tab.id" :to="tab.path" class="tabbar-item"> <img :src="tab.icon" :class="{ active: currentPath === tab.path }" alt=""> <span :class="{ active: currentPath === tab.path }">{{ tab.text }}</span> </router-link> </div> </template> <script> export default { data() { return { tabbarList: [ { id: 0, path: 'pages/tab/order/index', icon: 'static/orderDefault.png', selectIcon: 'static/orderPress.png', text: '订单', centerItem: false }, { id: 1, path: 'pages/tab/quckBilling/quickBilling.vue', icon: 'static/userDefault.png', selectIcon: 'static/userPress.png', text: '快速开单', centerItem: true }, { id: 2, path: 'pages/tab/mine/mine', icon: 'static/userDefault.png', selectIcon: 'static/userPress.png', text: '我的', centerItem: false } ], currentPath: '' }; }, created() { this.currentPath = this.$route.path; } }; </script> <style> .tabbar { display: flex; justify-content: space-around; align-items: center; background-color: #f5f5f5; height: 60px; } .tabbar-item { display: flex; flex-direction: column; align-items: center; text-align: center; text-decoration: none; color: #999999; } .tabbar-item img { width: 24px; height: 24px; } .tabbar-item span { font-size: 12px; } .tabbar-item .active { color: #007aff; } </style> ``` 将上述代码保存为一个名为 `Tabbar.vue` 的组件文件,然后在其他页面引入该组件并使用即可。请注意,你可能需要根据自己的项目配置进行一些调整,比如路由配置和样式等。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值