当前位置: 首页 > news >正文

sass 封装媒体查询工具

背景

以往写媒体查询可能是这样的:

.header {display: flex;width: 100%;
}@media (width >= 320px) and (width <= 480px) {.header {height: 50px;}
}@media (width > 480px) and (width <= 768px) {.header {height: 60px;}
}@media (width > 768px) and (width <= 1024px) {.header {height: 70px;}
}@media (width > 1024px) and (width <= 1200) {.header {height: 80px;}
}@media (width > 1200) {.header {height: 100px;}
}

以上写法可以看到写起来非常不方便,可读性也很差。因此希望用 sass 优化一下写法。

目标

希望可以这样写媒体查询:

.header {display: flex;width: 100%;手机: {height: 50px;}平板: {height: 60px;}...
}

sass 混合功能

sass/scss 快速入门

/* 定义混合函数 */
@mixin flexCenter($jus_c: center, $ali_i: center) {display: flex;justify-content: $jus_c;align-items: $ali_i;
}/* 使用混合函数 */
.header {width: 100%;@include flexCenter(space-between, flex-end);
}

sass if判断和插槽

混合函数中使用 @if判断区分不同设备,@content类似于 vue 插槽接收使用者在方法体中插入的内容。

@mixin respond-to($breakpoint) {@if $breakpoint == mobile {@media screen and (width <= 767px) {@content;}} @else if $breakpoint == tablet {@media screen and (width >= 768px) and (width <= 1023px) {@content;}} @else if $breakpoint == desktop {@media screen and (width >= 1024px) {@content;}} @else if $breakpoint == wide {@media screen and (width >= 1200px) {@content;}}
}

使用:

.header {width: 100%;height: 100vh;@include respond-to(mobile) {height: 100px;}@include respond-to(tablet) {height: 200px;}...background-color: rgb(139 133 133);
}

上面代码已经基本达到书写媒体查询的目标。但是 if else 太多了,不好看。还可以用策略模式优化一下。

进阶:sass 定义对象优化代码结构

用 hash 映射优化 if,也就是定义一个对象。sass 中可以定义对象。
注意:sass 中()括号就代表 js 的花括号{}和方括号[]

以下就是一个对象,这 5 个属性设置 5 个断点,除最后一个大屏外,其他断点属性值为数组。
之前的代码设置了 4 个断点,区别不大。

/* 定义断点对象 */
$breakpoints: (phone: (320px,480px),pad: (481px,768px),notebook: (769px,1024px),desktop: (1025px,1280px),tv: 1281px
);

sass 读取对象中的值:

  • map-get(obj, prop):获取对象的属性值

sass 判断数据类型:

  • type-of($var)
    • 数组类型:list
    • 数值类型:number
@mixin respond-to($breakname) {/* 1. 读取断点对象属性值 */$bp: map-get($breakpoints, $breakname);/* 2. 类型判断是否为数组 */@if type-of($bp) == "list" {/* 3. 取出数组中的数据 */$min: nth($bp, 1);$max: nth($bp, 2);@media screen and (min-width: $min) and (max-width: $max) {@content;}/* 4. tv 大屏 */} @else if type-of($bp) == "number" {@media screen and (min-width: $bp) {@content;}} @else {@warn "`$breakname` is not a valid breakpoint name.";}
}

vite 配置全局使用

直接在组件中 @include 使用混合函数,可能会报错:

  • [vite] Internal server error: [sass] Undefined mixin.

image.png

这是因为 minix 需要预编译,在 vite 中配置:
Vite

export default defineConfig({css: {preprocessorOptions: {scss: {javascriptEnabled: true,additionalData: `@use "@/styles/minix.scss" as *;`}}}
});

组件中使用:

.header {width: 100%;height: 100vh;@include respond-to(phone) {height: 100px;}@include respond-to(tv) {height: 200px;}background-color: rgb(139 133 133);
}

完整代码

$breakpoints: (phone: (320px,480px),pad: (481px,768px),notebook: (769px,1024px),desktop: (1025px,1280px),tv: 1281px
);@mixin respond-to($breakname) {/* 1. 读取断点对象属性值 */$bp: map-get($breakpoints, $breakname);/* 2. 类型判断是否为数组 */@if type-of($bp) == "list" {/* 3. 取出数组中的数据 */$min: nth($bp, 1);$max: nth($bp, 2);@media screen and (min-width: $min) and (max-width: $max) {@content;}/* 4. tv 大屏 */} @else if type-of($bp) == "number" {@media screen and (min-width: $bp) {@content;}} @else {@warn "`$breakname` is not a valid breakpoint name.";}
}
http://www.lryc.cn/news/229051.html

相关文章:

  • 眼科动态图像处理系统使用说明(2023-8-11 ccc)
  • 国际阿里云:提高CDN缓存命中率教程!!!
  • 关于“谈谈你对 ES 的理解”
  • Element-Ui el-table 动态添加行
  • 深度学习 大数据 股票预测系统 - python lstm 计算机竞赛
  • vue v-model
  • springboot整合ELK
  • 线性表->栈
  • linux rsyslog日志采集格式设定一
  • [100天算法】-不同路径 III(day 75)
  • 【学习笔记】[CCO2021] Travelling Merchant
  • 【终端】记录mbedtls库的重新安装
  • ElasticSearch简单操作
  • android studio新版本gradle Tasks找不到assemble
  • uniapp 小程序 身份证 和人脸视频拍摄
  • 飞腾ARM UOS编译Qt 5.15.2源码及Qt Creator
  • Oracle(2-2)Oracle Net Architecture
  • 高速高精运动控制,富唯智能AI边缘控制器助力自动化行业变革
  • GPTS应用怎么创建?GPTS无法创建应用很卡怎么办
  • 目标检测YOLO实战应用案例100讲-基于无人机的运动目标检测
  • 东莞松山湖数据中心|莞服务器托管的优势
  • 时间序列预测实战(十五)PyTorch实现GRU模型长期预测并可视化结果
  • 探索STM32系列微控制器的特性和性能
  • 数据结构(超详细讲解!!)第二十三节 树型结构
  • Python 日志记录器logging 百科全书 之 日志回滚
  • 线圈寿命预测 数据集讲解
  • Flutter.源码分析.flutter/packages/flutter/lib/src/widgets/scroll_view.dart/GridView
  • IDEA 2022创建Spring Boot项目
  • Python 框架学习 Django篇 (十) Redis 缓存
  • 考研数学笔记:线性代数中抽象矩阵性质汇总