uniapp使用scss mixin抽离css常用的公共样式

1、编写通用scss样式文件

css 复制代码
// 通用 Flex Mixin
@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-horizontal {
  @include flex(row, center, center);
}

// 垂直居中
@mixin flex-center-vertical {
  @include flex(column, center, center);
}

// 文本省略号
@mixin ellipsis($lines: 1) {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: $lines; // 多行省略
  -webkit-box-orient: vertical;
}

// Clearfix (清除浮动)
@mixin clearfix {
  &::after {
    content: "";
    display: table;
    clear: both;
  }
}

// Box Shadow (阴影效果)
@mixin box-shadow($x: 0px, $y: 4px, $blur: 6px, $color: rgba(0, 0, 0, 0.1)) {
  box-shadow: $x $y $blur $color;
}

// Border Radius (圆角样式)
@mixin border-radius($radius: 4px) {
  border-radius: $radius;
}

// Transition (过渡动画)
@mixin transition($properties: all, $duration: 0.3s, $easing: ease) {
  transition: $properties $duration $easing;
}

// 绝对定位居中
@mixin absolute-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

2、在uni.scss引入上述scss文件

3、在页面上使用,需加上lang=scss声明

编译后的效果:

相关推荐
拜无忧5 小时前
css卡片,重叠div,顶部错开,底部对齐
css
纯情小萝卜5 小时前
2026前端CSS黑科技技巧
css
天蓝色的鱼鱼1 天前
从“死了么”到“我在”:用uniCloud开发一款温暖人心的App
前端·uni-app
小徐_23331 天前
uni-app 组件库 Wot UI 的 AI 友好型编程指南
前端·uni-app
CHB2 天前
uni-app x 蒸汽模式 性能测试基准报告 Benchmark
uni-app·harmonyos
anyup2 天前
月销 8000+,uView Pro 让 uni-app 跨端开发提速 10 倍
前端·uni-app·开源
bluceli2 天前
CSS容器查询:响应式设计的新范式
前端·css
Bigger2 天前
CSS 这些年都经历了什么?一次看懂 CSS 的演化史
前端·css·前端工程化
大漠_w3cpluscom3 天前
使用 clip-path: shape() 创建 Squircle 形状
前端·css·weui
会员源码网5 天前
告别参数混乱:如何优雅解决方法参数过多导致的可维护性难题
css