设置根节点控制响应式网页

2019-12-27  本文已影响0人  从前慢pearl

中心思想:
通过 实际屏幕和设计图宽度的比列控制根节点的大小实现响应式变化

image.png
    //根据屏幕大小改变根元素字体大小
    (function (doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                //750这个值,根据设计师的psd宽度来修改,是多少就写多少,现在手机端一般是750px的设计稿,如果设计师给的1920的psd,自己用Photoshop等比例缩小
                if (clientWidth >= 750) {
                    docEl.style.fontSize = '100px';
                } else {
                    //750这个值,根据设计师的psd宽度来修改,是多少就写多少,现在手机端一般是750px的设计稿,如果设计师给的1920的psd,自己用Photoshop等比例缩小
                    docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
                }
            };

        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);      

一、头部加入最常用的meta内容

<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,minimal-ui">

width viewport的宽度
initial-scale 初始化缩放比例
minimum-scale 最小缩放比例
maxinum-scale 最大缩放比例
user-scalable 用户是否可以缩放
minimal-ui ios7以上隐藏浏览器导航栏

二、css样式重置

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
button,article, aside, canvas, details, embed, figure,
figcaption, footer, header, hgroup, menu, nav,
output, ruby, section, summary, time, mark,
audio, video {
margin: 0;

padding: 0;
border: 0;
vertical-align: baseline;
background: transparent;
outline: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;

}
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; }
ol, ul { list-style: none; }
button{background: transparent;}
blockquote, q { quotes: none; }
blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; }
strong { font-weight: bold; }
table { border-collapse: collapse; border-spacing: 0; }
img { border: 0; max-width: 100%; }
html{
line-height: initial;
}
body{
font-size: 0.32rem;
}

引:
https://blog.csdn.net/qq_15241071/article/details/87802738
https://segmentfault.com/a/1190000007276635#comment-area
https://blog.csdn.net/juse__we/article/details/83013328

上一篇下一篇

猜你喜欢

热点阅读