响应式布局

响应式布局

视口viewport

就是窗口的可视范围

视口单位

名称含义
vw相对于视口的宽
vh相对于视口的高
vmin选取vh与vw最小的那个
vmax选取vh与vw最大的那个
em相对于父元素的字体大小
rem相对于根元素的字体大小

vw,vh与百分比的区别

  1. 百分比:
    • 相对于父元素
    • 父元素没有高度时高度为0
    • 100%不包含滚动条
  2. vw,vh:
    • 相对于视口
    • 没有高度是窗口的百分比
    • 包含滚动条

rem的使用

  1. 加载js文件

  2. 所有数值除以100

  3. 使用rem单位

    rem375.js文件码源

    (function(doc, win) {
        var reCreateStyle = function() {
            var style;
            if (style = document.getElementById("forhtml")) {
                style.parentNode.removeChild(style);
            }
    
            style = document.createElement("style");
            style.id = "forhtml";
            var head = document.he || document.getElementsByTagName("head")[0];
            head.appendChild(style);
            var fontSize = 25.00 * parseFloat(document.documentElement.clientWidth, 10) / 375.00;
            style.appendChild(document.createTextNode("html{font-size:" + fontSize + "px!important;}"));
        };
        var docEI = doc.documentElement,
            resizeEvt = `orientationchange` in window ? `orientationchange` : `resize`,
            recalc = function() {
                reCreateStyle();
            }
        loadcalc = function() {
            reCreateStyle();
            var bodyEI = document.body;
    
            if (bodyEI.className == 'loading') {
                removeClass(bodyEI, 'loading');
            }
        }
        recalc();
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, loadcalc, false);
        doc.addEventListener(`DOMContentLoaded`, loadcalc, false);
    })
    (document, window);
    

媒体查询

媒体类型

all所有设备
print打印预览
screen屏幕
speech语音合成器

示范:@media screen{}

媒体特性

width有max-width,min-width
height有max-height,min-height
aspect-ratio宽高比
orientation有portrait(竖屏),landscape(横屏)

示范:@media (max-width:700px){}

逻辑操作符

and将多个媒体特性组合成一条媒体查询
not否定媒体查询,如果不符合这个媒体查询条件就执行
only旧浏览器使用
逗号将多个媒体类型合并成一个规则,只要其中任何一个为真,整个媒体语句就返回真。相当于or

示范:@media not screen,print and(max-width:1200px) and (min-width:600px){}

link标签方式

示例理解:在竖屏情况下加载a.css,横屏情况加载b.css

<link rel="stylesheet" href="css/a.css" media="(orientation:portrait)"

<link rel="stylesheet" href="css/b.css" media="(orientationlandscape)"

断点/阈值

编写位置

添加到样式底部,对css进行优先级的覆盖

阈值设定

常用阈值
<576px
>=576px
768px<=screen<=992pxPad
992px<=screen<=1200px横屏Pad
1200px<=screen<=1400px小PC
>=1400px所有大的PC端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值