hooks:於App.vue中引入並調用。會在頁面DOM結構最外層增加響應式的font-size屬性樣式更改。
1.接口和類型定義:
DesignParms:設計稿參數的接口定義,包括寬度、高度和字體大小等信息。
UseRemOption:使用 rem 的配置參數的接口定義,包括延遲觸發時間。
Designs:設計稿集合的類型定義,是一個由 DesignParms 構成的數組。
2.默認設計稿方案和配置參數:
DEFAULT_DESIGNS:默認的設計稿方案,包含多個不同分辨率下的設計稿信息。
DEFAULT_OPTION:默認的配置參數,包括延遲觸發時間。
3.useEqualScaling 函數:
該函數接受設計稿集合和配置參數作為參數,並返回縮放信息和方法。
函數內部根據窗口大小和設計稿信息計算最合適的設計稿作為參考設計稿。
在頁面加載和窗口大小改變時,會觸發 setHtmlFontSize 方法進行根節點字體大小的計算和設置。
函數返回設計稿集合、參考設計稿和設置根節點字體大小的方法
/**
* 基於rem 的比例縮放方案
* @author wangguanjie 2022.06.09
*/
import { onBeforeUnmount, onMounted, ref } from "vue";
/**
* 設計稿參數
* @example 1920 * 1080 下 {width: 1920, height: 1080, fontSize: 16, ...}
* @example 1680 * 1050 下 {width: 1680, height: 1050, fontSize: 14, ...}
* @example 1280 * 800 下 {width: 1280, height: 800, fontSize: 12, ...}
*/
export interface DesignParms {
width: number;
height: number;
fontSize: number;
}
export interface UseRemOption {
delay?: number;
}
export type Designs = DesignParms[];
/**
* 默認設計稿方案
*/
const DEFAULT_DESIGNS: Designs = [
{ width: 800, height: 600, fontSize: 12 },
{ width: 1024, height: 500, fontSize: 12 },
{ width: 1024, height: 768, fontSize: 12 }, // HD
{ width: 1280, height: 800, fontSize: 12 }, // WXGA
{ width: 1280, height: 854, fontSize: 12 },
{ width: 1280, height: 1024, fontSize: 12 },
{ width: 1366, height: 768, fontSize: 14 },
{ width: 1440, height: 900, fontSize: 14 },
{ width: 1440, height: 1050, fontSize: 14 },
{ width: 1600, height: 1024, fontSize: 14 },
{ width: 1600, height: 1200, fontSize: 14 }, // UXGA
{ width: 1680, height: 1050, fontSize: 14 },
{ width: 1920, height: 937, fontSize: 16 }, // FHD
{ width: 1920, height: 1080, fontSize: 16 }, // FHD
{ width: 1920, height: 1200, fontSize: 16 }, // WUXGA
{ width: 2048, height: 1080, fontSize: 18 },
{ width: 2048, height: 1536, fontSize: 18 }, // QXGA
{ width: 2560, height: 1080, fontSize: 18 },
{ width: 2560, height: 1440, fontSize: 18 }, // QHD
{ width: 2560, height: 1600, fontSize: 18 }, // WQXGA
{ width: 3440, height: 1440, fontSize: 18 },
{ width: 3840, height: 1080, fontSize: 18 },
{ width: 3840, height: 2160, fontSize: 18 }, // UHD
{ width: 4096, height: 2160, fontSize: 18 }, // 4K Ultra HD
];
/**
* 默認配置參數
*/
const DEFAULT_OPTION: Required<UseRemOption> = {
// 觸發延時,默認2022
delay: 0,
};
/**
* 等比縮放方案
* @author wangguanjie 2022.06.09
* @description 該方案支持多設計稿
* @param designs 設計稿集合
* @param option 縮放配置參數
* @returns 縮放信息、方法
*/
export const useEqualScaling = function (
designs: Designs = DEFAULT_DESIGNS,
option: UseRemOption = DEFAULT_OPTION
) {
let timer: number | undefined;
// 確保有設計稿可參考
designs = designs.length ? designs : DEFAULT_DESIGNS;
// 將設計搞進行升序排序,供後續快速排查
designs
.filter((d) => d.width > 1 && d.height > 1)
.sort((d1, d2) => {
if (d1.width === d2.width) {
return d1.height - d2.height;
}
return d1.width - d2.width;
});
// 降序排序的設計稿
const descendingDesigns = designs.map((d) => d).reverse();
// 瀏覽器適配所參考的設計稿
const referDesign = ref(designs[0]);
/**
* 根據多份設計搞自適應根節點字號
*/
const setHtmlFontSize = () => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
const { clientWidth: docW, clientHeight: docH } =
document.documentElement;
// 匹配最佳設計稿
const hasNewReferDesignIndex = designs.findIndex(
(d) => d.width >= docW && d.height >= docH
);
if (hasNewReferDesignIndex !== -1) {
referDesign.value = designs[hasNewReferDesignIndex];
} else {
// 優先篩選寬度符合的設計搞
const accordWidthDesigns = descendingDesigns.filter(
(d) => d.width <= docW
);
// 再確認最接近設計稿高度的設計稿
const designsHeightList = accordWidthDesigns.map((d) => d.height);
const useDesignIndex = designsHeightList
.map((designH) => Math.abs(docH - designH))
.findIndex(
(designH) =>
designH ===
Math.min(...designsHeightList.map((dh) => Math.abs(docH - dh)))
);
referDesign.value = accordWidthDesigns[useDesignIndex];
}
// 依據所參考的設計稿變更根節點字號
const documentRatio = docW / docH;
const {
width: designW,
height: designH,
fontSize: designFontSize,
} = referDesign.value;
const designRatio = designW / designH;
const htmlFontSize =
documentRatio < designRatio
? (docW / designW) * designFontSize
: (docH / designH) * designFontSize;
const html = document.querySelector("html");
if (html) {
html.style.fontSize = `${htmlFontSize}px`;
}
}, option.delay ?? DEFAULT_OPTION.delay);
};
setHtmlFontSize();
onMounted(() => {
window.addEventListener("resize", setHtmlFontSize, false);
});
onBeforeUnmount(() => {
window.removeEventListener("resize", setHtmlFontSize, false);
});
return {
designs,
referDesign,
setHtmlFontSize,
};
};
引入依賴方案:
- 裝包postcss-px2rem及px2rem-loader
npm install postcss-px2rem px2rem-loader --save
- 在根目錄src中新建utils目錄下新建rem.js等比適配文件,內容如下
// rem等比適配配置文件
// 基準大小
const baseSize = 16
// 設置 rem 函數
function setRem () {
// 當前頁面寬度相對於 1920寬的縮放比例,可根據自己需要修改。
const scale = document.documentElement.clientWidth / 1920
// 設置頁面根節點字體大小(“Math.min(scale, 2)” 指最高放大比例為2,可根據實際業務需求調整)
document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改變窗口大小時重新設置 rem
window.onresize = function () {
setRem()
}
3.在main.js中引入適配文件
import './utils/rem'
4.到vue.config.js中配置插件
// 引入等比適配插件
const px2rem = require('postcss-px2rem')
// 配置基本大小
const postcss = px2rem({
// 基準大小 baseSize,需要和rem.js中相同
remUnit: 16
})
// 使用等比適配插件
module.exports = {
lintOnSave: true,
css: {
loaderOptions: {
postcss: {
plugins: [
postcss
]
}
}
}
}