经常使用的pc端网站适配方案是什么?用的最多的大概就是父元素按照设计图的宽度进行固定宽度,margin:0 auto居中,两边留白。可是有的设计图不适合这样两边留白的适配方案。css
最近接手了一个pc端的项目,虽然按照经常使用的栅格布局+postcss-pxtorem插件对px转换的方法对页面作了适配,使页面不管在pc端打开仍是在移动端打开都能自适应,可是在高清分辨率下的页面好比5k高清,布局仍是有些乱了,这是由于px转换成rem所依赖的根目录字号没有调整好,因而上网百度了不少方案参考,从新调整了适配方案,可是在移动端打开的时候布局会乱掉,由于px的计算是根据pc端的宽高来计算的
1.删掉html的
<meta name="viewport" content="width=device-width,height=device-height,user-scalable=no" />
删掉这个标签,在移动端打开的时候,布局总体不会乱,可是子元素间距、宽高仍是会和设计图有差距html
2.下载依赖:npm install --save-dev postcss-pxtorem
在vue.config.js引入依赖:vue
const pxtorem = require("postcss-pxtorem");//px转换为rem插件 const autoprefixer = require("autoprefixer");//浏览器前缀处理工具 modules.exports={ css: { loaderOptions: { postcss: { plugins: [ pxtorem({ rootValue: 100, propList: ["*"] }), autoprefixer() ] } } } }
3.我在src/assets/js目录下新建pc.js文件,在main.js里导入这个文件npm
//pc.js //设计图纸为1366*798 function pagePc(){ let rootValue = 100; let pc = rootValue / 1366; // 表示1366的设计图,使用100px的默认值 let width = window.innerWidth; // 当前窗口的宽度 let height = window.innerHeight; // 当前窗口的高度 let rem = ""; let currentHeight = (width * 798) / 1366; if (height < currentHeight) { // 当前屏幕高度小于应有的屏幕高度,就须要根据当前屏幕高度从新计算屏幕宽度 width = (height * 1366) / 798; } rem = width * pc; // 以默认比例值乘以当前窗口宽度,获得该宽度下的相应font-size值 document.documentElement.style.fontSize = rem + "px"; } pagePc(); window.onresize = function() { pagePc(); };