sass 封装媒体查询工具

背景

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

css 复制代码
.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 优化一下写法。

目标

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

css 复制代码
.header {
    display: flex;
    width: 100%;
  
    手机: {
     	height: 50px;
    }
  
    平板: {
      	height: 60px;
    }
    ...
}

sass 混合功能

sass/scss 快速入门

css 复制代码
/* 定义混合函数 */
@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 插槽接收使用者在方法体中插入的内容。

css 复制代码
@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;
        }
    }
}

使用:

css 复制代码
.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 个断点,区别不大。

css 复制代码
/* 定义断点对象 */
$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
css 复制代码
@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.

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

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

组件中使用:

css 复制代码
.header {
    width: 100%;
    height: 100vh;

    @include respond-to(phone) {
        height: 100px;
    }

    @include respond-to(tv) {
        height: 200px;
    }

    background-color: rgb(139 133 133);
}

完整代码

css 复制代码
$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.";
    }
}
相关推荐
叁分之一11 分钟前
“我打包又失败了”
前端·npm
tang游戏王12311 分钟前
AJAX进阶-day4
前端·javascript·ajax
无语听梧桐16 分钟前
vue3中使用Antv G6渲染树形结构并支持节点增删改
前端·vue.js·antv g6
go2coding35 分钟前
开源 复刻GPT-4o - Moshi;自动定位和解决软件开发中的问题;ComfyUI中使用MimicMotion;自动生成React前端代码
前端·react.js·前端框架
freesharer1 小时前
Zabbix 配置WEB监控
前端·数据库·zabbix
web前端神器1 小时前
forever启动后端服务,自带日志如何查看与设置
前端·javascript·vue.js
是Yu欸1 小时前
【前端实现】在父组件中调用公共子组件:注意事项&逻辑示例 + 将后端数组数据格式转换为前端对象数组形式 + 增加和删除行
前端·vue.js·笔记·ui·vue
今天是 几 号1 小时前
WEB攻防-XSS跨站&反射型&存储型&DOM型&标签闭合&输入输出&JS代码解析
前端·javascript·xss
A-超1 小时前
html5 video去除边框
前端·html·html5
进击的阿三姐2 小时前
vue2项目迁移vue3与gogocode的使用
前端·javascript·vue.js