1、hotcss.js 插件(px2rem)
2、rem实现移动端屏幕的自适应
百分比只能实现元素的自适应,不能实现字体大小的自适应。
rem是一个相对单位,1rem等于html元素上字体设置的大小。
标注稿一般指设备的实际尺寸(750px),而我们编码的尺寸是css像素(375px)。
rem单位所代表的尺寸大小和屏幕宽度成正比
实现方式有以下三种:
1、媒体查询
@media only screen and (max-width: 1080px) {
html, body {
font-size: 16.875px;
}
}
@media only screen and (max-width: 960px) {
html, body {
font-size: 15px;
}
}
@media only screen and (max-width: 800px) {
html, body {
font-size: 12.5px;
}
}
@media only screen and (max-width: 720px) {
html, body {
font-size: 11.25px;
}
}
@media only screen and (max-width: 640px) {
html, body {
font-size: 10px;
}
}
640/10=64
720/64=11.25
800/64=12.5
2、js设置font-size的大小
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if (clientWidth >= 375) {
docEl.style.fontSize = '100px';
} else {
docEl.style.fontSize = 100 * (clientWidth / 1080) + 'px';
}
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
3、vm
使用vw设置,vw也是一个相对单位,100vw等于屏幕宽度
html{
font-size: 10vw;
}
webpack的插件也是基于这样的计算原理,比如px2rem
module.exports = {
module: {
rules: [{
test: /\.css$/,
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader'
}, {
loader: 'px2rem-loader',
// options here
options: {
remUnit: 32,
remPrecision: 8
}
}]
}]
}
}