ZhangXianQiang
2024-04-17 7d8fabb5feddf7d2daffd2452781c515d2eb13cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * 计算大小
 */
import EventEmitter from './eventEmitter';
export default class Sizes extends EventEmitter {
    constructor() {
        super();
        this.width = document.body.clientWidth;
        this.height = document.body.clientHeight;
        this.device = document.body.clientWidth <= 968 ? 'mobile' : 'pc';
        // 设备像素
        this.pixelRatio = Math.min(window.devicePixelRatio, 2);
 
        // 宽高变化
        window.addEventListener('resize', () => {
            // this.width = window.innerWidth;
            // this.height = window.innerHeight;
 
            this.width = document.body.clientWidth;
            this.height = document.body.clientHeight;
            this.pixelRatio = Math.min(window.devicePixelRatio, 2);
            this.trigger('resize');
 
            if(this.width < 968 && this.device !== 'mobile') {
                this.device = 'mobile';
                this.trigger('devicechange');
            } else if(this.width >= 968 && this.device !== 'pc') {
                this.device = 'pc';
                this.trigger('devicechange');
            }
        });
    }
}