|
|
@@ -1,47 +1,51 @@
|
|
|
-import { reactive, readonly } from 'vue'
|
|
|
-import { timerInterceptor } from '@/utils/timer'
|
|
|
+import { reactive, toRefs, readonly } from 'vue'
|
|
|
|
|
|
export default new (class {
|
|
|
private _state = reactive({
|
|
|
- clientWidth: 0
|
|
|
+ clientWidth: 0, // 客户端宽度
|
|
|
+ isMobile: false, // 是否移动端
|
|
|
})
|
|
|
|
|
|
/** 只读状态 */
|
|
|
state = readonly(this._state);
|
|
|
|
|
|
- constructor() {
|
|
|
- // 等待 html 加载完成
|
|
|
- document.addEventListener('DOMContentLoaded', () => {
|
|
|
- this.screenAdapter();
|
|
|
- // 监听窗口大小变化
|
|
|
- window.addEventListener('resize', timerInterceptor.setDebounce(() => this.screenAdapter()));
|
|
|
- }, false)
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * 适配屏幕 rem 单位尺寸
|
|
|
+ * 适配客户端屏幕
|
|
|
+ * @param pxtorem rem 布局
|
|
|
*/
|
|
|
- private screenAdapter() {
|
|
|
- const clientAgent = this.getClientAgent(),
|
|
|
- el = document.documentElement,
|
|
|
- body = document.body,
|
|
|
- designSize = 750;
|
|
|
+ screenAdapter(pxtorem: boolean) {
|
|
|
+ const { clientWidth, isMobile } = toRefs(this._state);
|
|
|
+ const { isPc } = this.getClientAgent();
|
|
|
+ const el = document.documentElement;
|
|
|
+ const body = document.body;
|
|
|
+ let screenWidth = el.clientWidth;
|
|
|
|
|
|
- let clientWidth = el.clientWidth;
|
|
|
+ if (pxtorem) {
|
|
|
+ const designSize = 750;
|
|
|
+ isMobile.value = true;
|
|
|
|
|
|
- if (clientAgent.isPc) {
|
|
|
- clientWidth = designSize / 1.8;
|
|
|
- body.style.setProperty('width', '540px');
|
|
|
+ if (isPc) {
|
|
|
+ screenWidth = designSize / 1.8;
|
|
|
+ body.style.setProperty('width', '540px');
|
|
|
+ } else {
|
|
|
+ body.style.removeProperty('width')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (screenWidth > 0) {
|
|
|
+ const fontSize = (screenWidth / designSize) * 100 + 'px';
|
|
|
+ el.style.setProperty('font-size', fontSize);
|
|
|
+ }
|
|
|
} else {
|
|
|
- body.style.removeProperty('width')
|
|
|
- }
|
|
|
+ if (screenWidth > 768) {
|
|
|
+ isMobile.value = false;
|
|
|
+ } else {
|
|
|
+ isMobile.value = true;
|
|
|
+ }
|
|
|
|
|
|
- if (clientWidth > 0) {
|
|
|
- const fontSize = (clientWidth / designSize) * 100 + 'px';
|
|
|
- el.style.setProperty('font-size', fontSize);
|
|
|
+ el.setAttribute('mode', isMobile.value ? 'mobile' : 'pc');
|
|
|
}
|
|
|
|
|
|
- this._state.clientWidth = body.clientWidth;
|
|
|
+ clientWidth.value = body.clientWidth;
|
|
|
}
|
|
|
|
|
|
/**
|