rem布局

2021-08-30  本文已影响0人  王果果
em单位和rem单位区别
/* 作用于根元素,相对于原始大小(16px),所以html的font-size为32px*/
html {font-size: 2rem}

/* 作用于非根元素,相对于根元素字体大小,所以为64px */
p {font-size: 2rem}
Rem布局原理

实际开发中的rem适配方案
  1. 手机淘宝团队出的简洁高效移动端适配库
  2. 我们再也不需要在写不同屏幕的媒体查询,因为里面js做了处理
  3. 它的原理是把设备划分成10等份,但在不同设备下,比例还是一致的
  4. 我们要做的就是确定好我们当前设备的html 文字字体大小就可以了
  5. 比如当前设计稿是750px 那么只需要把html文字大小设置为75px (750px/10)就可以
  6. 里面页面元素rem值:页面元素的px值 / 75
  7. 剩余的,让flexible.js去计算
  8. github地址: https://github.com/amfe/lib-flexible

1、设计搞常见的尺寸宽度


image.png

一般情况,我们以两套大部分适应的屏幕,放弃极端屏对其优雅降级,牺牲一些效果,现在基本以750为准

2、动态设置 html 标签 font-size 大小

3、元素大小取值方法

4、总结出得一个媒体查询的公共文件 common.less

// 定义一个公共的common.less文件  先装nodejs node -v
// npm install -g less 查看下安装成功没 lessc -v
// vscode 插件可以直接将less文件转换为css文件  (Easy-LESS)
// 设置常见的屏幕尺寸, 修改里面的html文字大小

// 一定要写最上面 
html{
  font-size: 50px;
}
// 我是定义的划分是15等份
@no: 15;
// 320
@media screen and (min-width: 320px){
  html{
    font-size: 320px / @no;
  }
}
// 360
@media screen and (min-width: 360px){
  html{
    font-size: 360px / @no;
  }
}
// 375 iphone 678
@media screen and (min-width: 384px){
  html{
    font-size: 384px / @no;
  }
}
@media screen and (min-width: 400px){
  html{
    font-size: 400px / @no;
  }
}
@media screen and (min-width: 414px){
  html{
    font-size: 414px / @no;
  }
}
@media screen and (min-width: 424px){
  html{
    font-size: 424px / @no;
  }
}
@media screen and (min-width: 480px){
  html{
    font-size: 480px / @no;
  }
}
@media screen and (min-width: 540px){
  html{
    font-size: 540px / @no;
  }
}
@media screen and (min-width: 720px){
  html{
    font-size: 720px / @no;
  }
}
@media screen and (min-width: 750px){
  html{
    font-size: 750px / @no;
  }
}

下面给一个rem+js方案的完整例子,css的计算没有使用预处理器,这个很简单

html代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <title>rem布局——rem+js</title>
</head>
<body>
    <noscript>开启JavaScript,获得更好的体验</noscript>

    <div class="p1">
        宽度为屏幕宽度的50%,字体大小1.2em
        <div class="s1">
            字体大小1.2.em
        </div>
    </div>

    <div class="p2">
        宽度为屏幕宽度的40%,字体大小默认
        <div class="s2">
            字体大小1.2em
        </div>
    </div>
</body>
</html>
css代码如下:

html {
    font-size: 32px; /* 320/10 */
}
body {
    font-size: 16px; /* 修正字体大小 */
    /* 防止页面过宽 */
    margin: auto;
    padding: 0;
    width: 10rem;
    /* 防止页面过宽 */
    outline: 1px dashed green;
}

/* js被禁止的回退方案 */
@media screen and (min-width: 320px) {
    html {font-size: 32px}
    body {font-size: 16px;}
}
@media screen and (min-width: 481px) and (max-width:640px) {
    html {font-size: 48px}
    body {font-size: 18px;}
}
@media screen and (min-width: 641px) {
    html {font-size: 64px}
    body {font-size: 20px;}
}

noscript {
    display: block;
    border: 1px solid #d6e9c6;
    padding: 3px 5px;
    background: #dff0d8;
    color: #3c763d;
}
/* js被禁止的回退方案 */

.p1, .p2 {
    border: 1px solid red;
    margin: 10px 0;
}

.p1 {
    width: 5rem;
    height: 5rem;

    font-size: 1.2em; /* 字体使用em */
}

.s1 {
    font-size: 1.2em; /* 字体使用em */
}

.p2 {
    width: 4rem;
    height: 4rem;
}
.s2 {
    font-size: 1.2em /* 字体使用em */
}
js代码如下:

var documentElement = document.documentElement;

function callback() {
    var clientWidth = documentElement.clientWidth;
    // 屏幕宽度大于780,不在放大
    clientWidth = clientWidth < 780 ? clientWidth : 780;
    documentElement.style.fontSize = clientWidth / 10 + 'px';
}

document.addEventListener('DOMContentLoaded', callback);
window.addEventListener('orientationchange' in window ? 'orientationchange' : 'resize', callback);

源网址:https://www.xyhtml5.com/3721.html
源网址:https://www.cnblogs.com/chen-cheng/p/13969372.html

上一篇 下一篇

猜你喜欢

热点阅读