Search K
Appearance
👍欢迎大家积极投稿交流👍
如果文档内容陈旧或者链接失效,请发现后及时同步,我将尽快修改
👇微信👇

Appearance
本项目基于 UniApp + Vue3 + TypeScript + UnoCSS 技术栈,采用现代化的CSS开发方式。本规范旨在确保代码的一致性、可维护性和可读性。
// ✅ 正确示例
.component-name {
display: flex;
justify-content: center;
align-items: center;
padding: 20rpx;
margin: 10rpx 0;
background-color: #ffffff;
border-radius: 8rpx;
&:hover {
background-color: #f5f5f5;
}
.child-element {
font-size: 28rpx;
color: #333333;
}
}项目使用以下 Prettier 配置:
采用 BEM (Block Element Modifier) 命名方法论:
// ✅ 正确示例
.goods-list {
// Block
.goods-item {
// Element
&--featured {
// Modifier
border: 2rpx solid #007aff;
}
&__title {
// Element
font-size: 32rpx;
font-weight: bold;
}
&__price {
// Element
color: #ff4757;
&--discount {
// Modifier
text-decoration: line-through;
}
}
}
}使用语义化的变量名:
// ✅ 正确示例
$primary-color: #007aff;
$success-color: #4cd964;
$warning-color: #f0ad4e;
$error-color: #dd524d;
$text-color-primary: #333333;
$text-color-secondary: #999999;
$text-color-placeholder: #808080;
$spacing-xs: 8rpx;
$spacing-sm: 16rpx;
$spacing-md: 24rpx;
$spacing-lg: 32rpx;
$spacing-xl: 48rpx;优先使用 UnoCSS 原子化类名:
<template>
<!-- ✅ 推荐:使用原子化类名 -->
<view class="flex justify-center items-center p-4 bg-white rounded-lg">
<text class="text-lg font-bold text-gray-800">标题</text>
</view>
<!-- ❌ 避免:过度使用自定义样式 -->
<view class="custom-container">
<text class="custom-title">标题</text>
</view>
</template>使用 UnoCSS 的响应式前缀:
<template>
<view class="w-full sm:w-1/2 md:w-1/3 lg:w-1/4">
<!-- 响应式宽度 -->
</view>
</template>项目中定义的快捷方式:
// 已定义的快捷方式
.center {
@apply flex justify-center items-center;
}
// 安全区域适配
.p-safe {
padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom)
env(safe-area-inset-left);
}
.pt-safe {
padding-top: env(safe-area-inset-top);
}
.pb-safe {
padding-bottom: env(safe-area-inset-bottom);
}// 1. 导入外部依赖
@import '@/style/variables.scss';
@import '@/style/mixins.scss';
// 2. 变量定义
$component-bg: #ffffff;
$component-padding: 20rpx;
// 3. 混合器定义
@mixin button-style($bg-color, $text-color) {
background-color: $bg-color;
color: $text-color;
border-radius: 8rpx;
padding: 16rpx 32rpx;
}
// 4. 主要样式
.component {
// 样式规则
}& 符号// ✅ 正确示例
.card {
padding: 20rpx;
background: #fff;
&:hover {
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}
.card-header {
margin-bottom: 16rpx;
.card-title {
font-size: 32rpx;
font-weight: bold;
}
}
}使用项目定义的颜色变量:
// 主色调
$uni-color-primary: #007aff; // 主题蓝
$uni-color-success: #4cd964; // 成功绿
$uni-color-warning: #f0ad4e; // 警告橙
$uni-color-error: #dd524d; // 错误红
// 文字颜色
$uni-text-color: #333333; // 主文字
$uni-text-color-grey: #999999; // 辅助文字
$uni-text-color-placeholder: #808080; // 占位文字
// 背景颜色
$uni-bg-color: #ffffff; // 主背景
$uni-bg-color-grey: #f8f8f8; // 灰色背景
$uni-bg-color-hover: #f1f1f1; // 悬停背景<template>
<!-- 使用主题色 -->
<view class="bg-primary text-white">主题色背景</view>
<text class="text-primary">主题色文字</text>
</template>rpx(响应式像素)px、rem、em// ✅ 正确示例
.mobile-component {
width: 750rpx; // 移动端宽度
height: 200rpx; // 移动端高度
font-size: 28rpx; // 移动端字体
padding: 20rpx 30rpx; // 移动端内边距
}
.web-component {
width: 100%; // 响应式宽度
max-width: 1200px; // 最大宽度限制
font-size: 1rem; // Web端字体
padding: 1rem 1.5rem; // Web端内边距
}使用统一的间距系统:
// 间距变量
$spacing-xs: 8rpx; // 极小间距
$spacing-sm: 16rpx; // 小间距
$spacing-md: 24rpx; // 中等间距
$spacing-lg: 32rpx; // 大间距
$spacing-xl: 48rpx; // 超大间距
// UnoCSS 间距类
.p-2 // padding: 8rpx
.p-4 // padding: 16rpx
.p-6 // padding: 24rpx
.p-8 // padding: 32rpx
.p-12 // padding: 48rpx<template>
<view class="component-container">
<view class="component-header">
<text class="component-title">标题</text>
</view>
<view class="component-content">
<!-- 内容 -->
</view>
</view>
</template>
<style lang="scss" scoped>
// 使用 scoped 避免样式污染
.component-container {
padding: 20rpx;
background: #fff;
border-radius: 12rpx;
.component-header {
margin-bottom: 16rpx;
border-bottom: 1rpx solid #eee;
padding-bottom: 12rpx;
.component-title {
font-size: 32rpx;
font-weight: bold;
color: $uni-text-color;
}
}
.component-content {
line-height: 1.6;
color: $uni-text-color-grey;
}
}
</style>// src/style/index.scss
// 全局重置样式
* {
box-sizing: border-box;
}
// 全局工具类
.text-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.text-ellipsis-2 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
// 全局动画
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}// ✅ 推荐:使用CSS变量
:root {
--primary-color: #007aff;
--text-color: #333333;
--bg-color: #ffffff;
}
.component {
color: var(--text-color);
background: var(--bg-color);
}// 条件编译处理不同平台
/* #ifdef H5 */
.h5-only {
// H5 特有样式
}
/* #endif */
/* #ifdef MP-WEIXIN */
.mp-weixin-only {
// 微信小程序特有样式
}
/* #endif */
/* #ifdef APP-PLUS */
.app-only {
// App 特有样式
}
/* #endif */// 使用安全区域变量
.safe-area-container {
padding-top: env(safe-area-inset-top);
padding-bottom: env(safe-area-inset-bottom);
}项目配置了以下CSS相关的ESLint规则:
使用 lint-staged 在提交前自动格式化:
{
"lint-staged": {
"*": "eslint --fix"
}
}