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

scss混合优化媒体查询书写

采用scss的混合和继承优化css的媒体查询代码书写

原写法
.header {
    width: 100%;
}
@media (min-width: 320px) and (max-width: 480px) {
    .header {
        height: 50px;
    }
}
@media (min-width: 481px) and (max-width: 768px) {
    .header {
        height: 60px;
    }
}
@media (min-width: 769px) and (max-width: 1024px) {
    .header {
        height: 70px;
    }
}
@media (min-width: 1025px) and (max-width: 1200px) {
    .header {
        height: 80px;
    }
}
@media (min-width: 1026px) {
    .header {
        height: 90px;
    }
}
scss优化写法
$breakpoints: (
	'phone': (320px, 480px),
	'pad': (481px, 768px),
	'notebook': (769px, 1024px),
	'desktop': (1025px, 11200px),
	'tv': 1201px
)
    
@mixin responseTo($breakname) {
    $bp: map-get($breakpoints, $breakname);
    @if type-of($bp) == 'list' {
    	@media (min-width: nth($bp, 1)) and (max-width: nth($bp,2)) {
            @content; 
        }
    } @else {
        @media (min-width: $bp) {
            @content;
        }
   	}
}

header {
    width: 100%;
    @include responseTo('phone') {
        height: 50px;
    }
    @includee responseTo('pad') {
        height: 60px;
    }
    @include responseTo('notebook') {
        height: 70px;
    }
    @include responseTo('desktop') {
        height: 80px;
    }
    @includee responseTo('tv') {
        height: 90px;
    }
}

http://www.kler.cn/a/541583.html

相关文章:

  • 解释和对比“application/octet-stream“与“application/x-protobuf“
  • 深入探究 Rust 测试:灵活控制测试的执行方式
  • 数据集成实例分享:金蝶云星空对接旺店通实现库存管理自动化
  • 大模型中设计的精度(FP8,FP16,FP32,混合精度训练,精度量化)相关总结
  • Kotlin Android 环境搭建
  • C#中的Frm_Welcome.Instance.Show(),是什么意思
  • 防御保护作业二
  • 解锁摄影潜能:全面解析相机镜头的选择与使用逻辑
  • Git 与 Git常用命令
  • 【详细版】DETR系列之Deformable DETR(2021 ICLR)
  • 【DeepSeek服务器繁忙,请稍后再试...如何解决?】
  • 互联网企业线上业务拓展与开源AI智能名片2+1链动模式S2B2C商城小程序的创新应用
  • Python 文字识别OCR
  • 代码随想录算法【Day39】
  • MindStudio制作MindSpore TBE算子(二)算子测试
  • 常用的python库-安装与使用
  • YOLO模型缝合实战指南:ECA注意力模块的实现与集成
  • Tria Technologies RFSoC 平台 - 入门指南
  • 2025 年前端开发现状分析:卷疯了还是卷麻了?
  • 【FPGA】模型机下载FPGA设计
  • UE5 如何通过命令行启动游戏工程
  • 【错题本】js事件循环机制下,记录一个意外错误
  • Ada语言的区块链
  • 地平线 3D 目标检测 Bevformer 参考算法-V2.0
  • 怎么查看电脑显存大小(查看电脑配置)
  • OpenGL学习笔记(十二):初级光照:投光物/多光源(平行光、点光源、聚光)