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声明

编译后的效果:

相关推荐
前端老石人9 分钟前
Chrome DevTools 调试入门:从零开始排查 CSS 问题
前端·css·chrome devtools
雯0609~43 分钟前
微信小程序的原生开发项目如何转至uni-app
微信小程序·小程序·uni-app
四方云1 小时前
Uni-app 跨端集成 SIP 电话功能(H5 + App)实战
uni-app
习明然12 小时前
UniApp开发体验感受总结
前端·uni-app
不考研当牛马18 小时前
HTML CSS 新手大全初学者必看 (含有部分 JavaScript)
javascript·css·html
anyup18 小时前
全面重构的 uni-app 多平台上传组件,功能强到离谱!
前端·vue.js·uni-app
暗不需求18 小时前
告别“class 命名地狱”:从面向对象 CSS 到原子 CSS(Tailwind) 的思维跃迁
前端·css·react.js
极客小俊20 小时前
【从零到一】用HTML5+CSS+JavaScript实现一个属于自己的mp3免费音乐播放器 (4) JS交互功能(音乐进度条)
javascript·css·html5·前端开发·免费教程·代码案例·手搓音乐播放器
何何____20 小时前
css变换语法介绍及案例展示
前端·css
LIO1 天前
一套代码,多端并行——uni-app + Vue3 多端开发完全指南
前端·vue.js·uni-app