[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
vue cli3,根据屏幕大小适配样式
最近做了一个大屏,考虑到要在不同的显示器上,显示同一套代码,所以我就想到了,阿里的“lib-flexible”,
干货非常多,千万不要错过细节哦,废话不说,直接干
1.安装需要响应的包
npm i lib-flexible -S
这个包会有改动的东西,先卖个关子,最后会详细说。
(2)安装第二个包
npm i px2rem-loader -D
2.安装好了之后全局引入包,在main.js中添加如下代码
import 'lib-flexible'
3.在vue.config.js中添加如下代码
css: { loaderOptions: { postcss: { plugins: [ require('postcss-px2rem')({
remUnit: 192 }) ] } }, },
值得注意的是remUnit,这个是填你们的设计稿的大小除以2,再除以10,不过这个要注意一点,当你改完lib-flexible时,你就需要填写除以设计稿的10就可以了。
4.修改lib-flexible.js,
为什么要改,你会发现一个问题,移动端没问题,但是pc端他的大小只适配到540,
所以在安装包里找到 lib-flexible,包中的lib-flexible.js文件,
其中修改如下代码
没改之前
function refreshRem(){ var width = docEl.getBoundingClientRect().width; if
(width / dpr > 540) { width = 540 * dpr; } var rem = width / 10;
docEl.style.fontSize = rem + 'px'; flexible.rem = win.rem = rem; }
修改之后
function refreshRem(){ var width = docEl.getBoundingClientRect().width; if
(width / dpr > 540) { width = width * dpr; } var rem = width / 10;
docEl.style.fontSize = rem + 'px'; flexible.rem = win.rem = rem; }
就是把540的宽度改成,当前屏幕的宽度
5.改完之后如何在项目中同步更新呢?
接下来把这个文件复制出来,放到lib文件下
然后在main.js中
把import 'lib-flexible'改成import './lib/lib-flexible',
这样就可以在所有项目同步了,不用每个人都改了。
如果这篇文章帮到您了,
麻烦您动下发财小手,评论、点赞、转发、收藏、或者打赏