webapp思路和rem适配极其viewport

时间:2023-03-09 04:12:44
webapp思路和rem适配极其viewport

webapp在制作时候,页面上要加入viewport标签,用来进行适配;

viewport的meta标签,指的是在移动端显示的时候,viewport是多大?移动端的浏览器是屏幕宽,viewport一般(手机浏览器)默认的是960px左右,就把页面压缩到

960px显示,所以是比手机屏幕宽的哟.等比放小,所以,为了显示的好看,要设定viewport为屏幕宽,就是:如此标签

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

设定width就是设备的宽度,然后就将页面大小缩放这么大显示.所以,在很多时候,在webapp页面的时候,我们直接在pc端测试就行,直接设计640px的宽度来做,然后html和body 的

宽度设为100%;主要容器宽度就是640px,然后,适配在手机端的时候,就会将其略压缩显示在手机的viewport上,大概样子差不多,而且,在加上rem的适配,一般没问题的.

而rem适配,是根据html这个根元素来进行的,1rem为这个根元素的字体大小.若将其设为10px,那么1rem就是10px,之后的所有都用rem来表示,然后,在用js或者是css的media query来实现不同屏幕大小时候不容的html的字体,然后,其余所有部分,都会跟随这成比例改变了的.就是这个原理的.

讲的,实际上也就是两点:1,viewport的作用;2,rem进行适配,当然也有用100%比的,不过out了

列出测试成功的rem适配的css和js代码:

css:

@media only screen and (max-width: 320px){html{font-size: 9px;} }
@media only screen and (min-width: 320px) and (max-width: 352px){html{font-size: 10px;} }
@media only screen and (min-width: 352px) and (max-width: 384px){html{font-size: 11px;} }
@media only screen and (min-width: 384px) and (max-width: 416px){html{font-size: 12px;} }
@media only screen and (min-width: 416px) and (max-width: 448px){html{font-size: 13px;} }
@media only screen and (min-width: 448px) and (max-width: 480px){html{font-size: 14px;} }
@media only screen and (min-width: 480px) and (max-width: 512px){html{font-size: 15px;} }
@media only screen and (min-width: 512px) and (max-width: 544px){html{font-size: 16px;} }
@media only screen and (min-width: 544px) and (max-width: 576px){html{font-size: 17px;} }
@media only screen and (min-width: 576px) and (max-width: 608px){html{font-size: 18px;} }
@media only screen and (min-width: 608px) and (max-width: 640px){html{font-size: 19px;} }
@media only screen and (min-width: 640px){html{font-size: 20px;} }

js:

(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if(clientWidth>){
clientWidth = ;
}
//这行代码有诈,讲10改成更小的数字,比如5,就没有反应了...字大小可以,但
//div的宽高就不变了也是醉了
docEl.style.fontSize = * (clientWidth / ) + 'px'; }; if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);