SCSS - PC + Mobile 结构设计

我将提供一个结构化、模块化的 SCSS 文件设计,包含 PC 端和移动端代码,通过媒体查询实现响应式布局。设计将注重代码组织、可维护性和扩展性。

// 1. 变量定义 - 全局配置
$breakpoints: (
  mobile: 768px,
  tablet: 1024px,
  desktop: 1200px,
);

$colors: (
  primary: #007bff,
  secondary: #6c757d,
  background: #ffffff,
  text: #333333,
);

$spacing: (
  small: 8px,
  medium: 16px,
  large: 24px,
);

// 2. 混合宏 - 常用样式复用
@mixin respond-to($breakpoint) {
  @if map-has-key($breakpoints, $breakpoint) {
    @media (max-width: map-get($breakpoints, $breakpoint)) {
      @content;
    }
  } @else {
    @warn "Unknown breakpoint: #{$breakpoint}";
  }
}

@mixin flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

// 3. 基础样式 - 全局重置和通用样式
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  color: map-get($colors, text);
  background: map-get($colors, background);
}

// 4. 组件模块 - 按功能/组件组织
// 导航栏
.nav {
  background: map-get($colors, primary);
  padding: map-get($spacing, medium);

  // PC 端样式
  display: flex;
  justify-content: space-between;
  align-items: center;

  &__logo {
    font-size: 24px;
    color: white;
  }

  &__menu {
    @include flex-center;
    gap: map-get($spacing, medium);

    a {
      color: white;
      text-decoration: none;
      font-size: 16px;

      &:hover {
        text-decoration: underline;
      }
    }
  }

  // 移动端样式
  @include respond-to(mobile) {
    flex-direction: column;
    padding: map-get($spacing, small);

    &__logo {
      font-size: 20px;
      margin-bottom: map-get($spacing, small);
    }

    &__menu {
      flex-direction: column;
      gap: map-get($spacing, small);

      a {
        font-size: 14px;
      }
    }
  }
}

// 主要内容区域
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: map-get($spacing, large);

  // PC 端样式
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: map-get($spacing, large);

  &__article {
    padding: map-get($spacing, medium);
    background: lighten(map-get($colors, background), 5%);
  }

  &__sidebar {
    padding: map-get($spacing, medium);
    border-left: 1px solid map-get($colors, secondary);
  }

  // 移动端样式
  @include respond-to(mobile) {
    grid-template-columns: 1fr;
    padding: map-get($spacing, medium);

    &__sidebar {
      border-left: none;
      border-top: 1px solid map-get($colors, secondary);
    }
  }
}

// 5. 工具类 - 可复用的实用类
.text-center {
  text-align: center;
}

.margin-bottom {
  margin-bottom: map-get($spacing, medium);

  @include respond-to(mobile) {
    margin-bottom: map-get($spacing, small);
  }
}

// 6. 动画效果
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.fade-in {
  animation: fadeIn 0.5s ease-in;
}

这个 SCSS 结构设计的特点和优势:

1.模块化组织

  • 使用变量集中管理断点、颜色和间距,便于全局修改
  • 按组件(nav、main-content)组织样式,提高可维护性
  • 工具类和动画效果独立定义,方便复用

2.响应式设计

  • 使用 @mixin respond-to 统一管理媒体查询
  • PC 端样式作为默认,移动端样式通过媒体查询覆盖
  • 移动端优先考虑简洁布局,减少复杂性

3.可扩展性

  • 变量和混合宏支持快速添加新断点或主题
  • 组件化结构便于添加新模块
  • 命名规范(BEM-like)避免样式冲突

4.可维护性

  • 清晰的注释分隔不同功能区域
  • 一致的间距和颜色引用减少重复代码
  • 错误处理(如未知断点警告)提高健壮性

使用建议:

  • 根据项目需求调整 $breakpoints$colors
  • 可添加更多工具类或动画效果
  • 对于复杂项目,可将不同组件拆分到单独的 SCSS 文件,使用 @import 引入