css 里面写if else 条件判断

  • 定义时使用 @mixin name(params) 声明

  • 调用时通过 @include name(args) 引入样式

  • mixins.scss文件中:

    复制代码
    @import './variables.scss';
    
    // 弹性布局
    @mixin flex($direction: row, $justify: flex-start, $align: stretch, $wrap: nowrap) {
      display: flex;
      flex-direction: $direction;
      justify-content: $justify;
      align-items: $align;
      flex-wrap: $wrap;
    }
    
    // 居中对齐
    @mixin flex-center {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    // 两端对齐
    @mixin flex-between {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    // 文本截断
    @mixin text-truncate {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    // 多行文本截断
    @mixin text-truncate-multiline($lines: 2) {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: $lines;
      -webkit-box-orient: vertical;
    }
    
    // 响应式布局
    @mixin responsive($breakpoint) {
      @if $breakpoint == sm {
        @media (min-width: $breakpoint-sm) { @content; }
      } @else if $breakpoint == md {
        @media (min-width: $breakpoint-md) { @content; }
      } @else if $breakpoint == lg {
        @media (min-width: $breakpoint-lg) { @content; }
      } @else if $breakpoint == xl {
        @media (min-width: $breakpoint-xl) { @content; }
      }
    }
    
    // 状态颜色
    @mixin status-color($status) {
      @if $status == 'normal' or $status == '正常' {
        background-color: $color-success;
        color: $color-white;
      } @else if $status == 'warning' or $status == '警告' {
        background-color: $color-warning;
        color: $color-dark;
      } @else if $status == 'error' or $status == '异常' {
        background-color: $color-danger;
        color: $color-white;
      } @else {
        background-color: $color-secondary;
        color: $color-white;
      }
    } 

    引用的vue文件:

    复制代码
    <template>
      <div class="switch-node">
        <!-- 交换机图标和名称 -->
        <div class="d-flex align-items-center mb-2">
          <div class="switch-node__icon">
            <svg class="switch-node__icon-svg" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4" />
            </svg>
          </div>
          <div class="switch-node__content">
            <p class="switch-node__title" :title="nodeData.label">
              {{ nodeData.label }}
            </p>
            <p class="switch-node__subtitle">
              {{ nodeData.district }}
            </p>
          </div>
        </div>
        
        <!-- 状态指示器 -->
        <div class="switch-node__footer">
          <span>状态:</span>
          <span class="switch-node__status" :class="statusClass">
            <span class="switch-node__status-indicator"></span>
            {{ nodeData.status }}
          </span>
        </div>
        
        <!-- 设备计数指示器 -->
        <div class="switch-node__devices">
          设备: {{ nodeData.devices ? nodeData.devices.length : 0 }}
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: 'SwitchNode',
      props: {
        nodeData: {
          type: Object,
          required: true
        }
      },
      computed: {
        statusClass() {
          switch(this.nodeData.status) {
            case '正常': return 'status-normal';
            case '警告': return 'status-warning';
            case '异常': return 'status-error';
            default: return '';
          }
        }
      }
    }
    </script>
    
    <style lang="scss" scoped>
    @import '@/assets/styles/variables.scss';
    @import '@/assets/styles/mixins.scss';
    
    .status-normal {
      @include status-color('正常');
    }
    
    .status-warning {
      @include status-color('警告');
    }
    
    .status-error {
      @include status-color('异常');
    }
    </style> 
相关推荐
liliangcsdn7 分钟前
mac mlx大模型框架的安装和使用
java·前端·人工智能·python·macos
CssHero10 分钟前
基于vue3完成领域模型架构建设
前端
PanZonghui14 分钟前
用项目说话:我的React博客构建成果与经验复盘
前端·react.js·typescript
言兴17 分钟前
教你如何理解useContext加上useReducer
前端·javascript·面试
sunbyte20 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)
前端·javascript·css·vue.js·tailwindcss
前端的日常22 分钟前
网页视频录制新技巧,代码实现超简单!
前端
前端的日常23 分钟前
什么是 TypeScript 中的泛型?请给出一个使用泛型的示例。
前端
今禾24 分钟前
一行代码引发的血案:new Array(5) 到底发生了什么?
前端·javascript·算法
ccc101827 分钟前
老师问我localhost和127.0.0.1,有什么区别?
前端
Struggler28134 分钟前
Chrome插件开发
前端