最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue動態(tài)類名及動態(tài)樣式設置方式:class/:style

 更新時間:2022年05月25日 10:33:46   作者:·半傻半呆半瘋癲  
文章主要介紹了在Vue中設置動態(tài)類名(:class)和動態(tài)樣式(:style)的方法,動態(tài)類名方面,使用三元表達式、結合過濾器和計算屬性等方法進行設置,動態(tài)樣式方面,介紹了基礎用法、結合計算屬性和三元表達式進行設置等方法

Vue動態(tài)類名(:class)、動態(tài)樣式(:style) 的設置

以下是個人在項目中使用過的關于在Vue中的動態(tài)類名和動態(tài)樣式的設置方法的整理記錄

動態(tài)類名(: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>

動態(tài)樣式(: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. 動態(tài)配置背景顏色、背景圖片
<div
      class="main__header"
      :style="
        'background: ' +
        `${themeConfig.themeColor}` +
        ' url(' +
        require(`@/assets/themeCofing/${themeConfig.label}/personalInfo/header_bg.png`) +
        ')no-repeat center / contain;'
      "
   />

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 使用Vue實現(xiàn)簡易的車牌輸入鍵盤

    使用Vue實現(xiàn)簡易的車牌輸入鍵盤

    這篇文章主要為大家詳細介紹了如何使用Vue實現(xiàn)簡易的車牌輸入鍵盤效果,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解下
    2023-11-11
  • Vue中使用v-print打印出現(xiàn)空白頁問題及解決

    Vue中使用v-print打印出現(xiàn)空白頁問題及解決

    這篇文章主要介紹了Vue中使用v-print打印出現(xiàn)空白頁問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Vue實現(xiàn)將頁面區(qū)域導出為pdf

    Vue實現(xiàn)將頁面區(qū)域導出為pdf

    文章介紹了兩種將前端頁面指定區(qū)域導出為PDF的純前端實現(xiàn)方法,方式一使用jsPDF和html2canvas將特定區(qū)域轉化為圖片,再將圖片轉化為PDF,適用于小區(qū)域轉換,但存在翻頁時內容拆分的問題,方式二使用html2pdf.js,支持自動分頁,適用于較大區(qū)域的轉換
    2025-10-10
  • Vue 滾動行為的具體使用方法

    Vue 滾動行為的具體使用方法

    本篇文章主要介紹了Vue 滾動行為的具體使用方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    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實現(xiàn)多欄布局拖拽

    vue實現(xiàn)多欄布局拖拽

    這篇文章主要為大家詳細介紹了vue實現(xiàn)多欄布局拖拽,改變盒子的寬度,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue模塊拖拽實現(xiàn)示例代碼

    vue模塊拖拽實現(xiàn)示例代碼

    這篇文章主要介紹了vue模塊拖拽實現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03

最新評論

兴和县| 桐庐县| 德州市| 五峰| 肇源县| 通城县| 合阳县| 崇明县| 公安县| 井冈山市| 尼木县| 墨脱县| 乐山市| 清流县| 平顺县| 惠安县| 吉木萨尔县| 九江市| 朔州市| 喜德县| 宝山区| 潜山县| 武定县| 临澧县| 大同县| 新兴县| 平罗县| 乌拉特后旗| 奉化市| 新邵县| 双江| 辽宁省| 鹤庆县| 镇巴县| 疏附县| 霍山县| 临夏市| 乐山市| 鹤山市| 高州市| 沂南县|