可视化大屏

菇太帷i Lv4

不同屏幕的自适应

1. 缩放

根据设计时的尺寸,算出不同屏幕的比例,然后整体进行缩放

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  // * 以宽度为基准

  function calcRateByWidth(
    baseWidth: number,
    baseHeight: number,
    baseProportion: number
  ) {
    // 表示更高
    const heightRatio = parseFloat(
      (window.innerWidth / baseProportion / baseHeight).toFixed(5)
    );
    const widthRatio = parseFloat((window.innerWidth / baseWidth).toFixed(5));
    return { widthRatio, heightRatio };
  }

  // * 以高度为基准
  function calcRateByHeight(
    baseWidth: number,
    baseHeight: number,
    baseProportion: number
  ) {
    // 表示更宽
    const widthRatio = parseFloat(
      ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
    );
    const heightRatio = parseFloat(
      (window.innerHeight / baseHeight).toFixed(5)
    );
    return { widthRatio, heightRatio };
  }



  // * 拉伸
  function calcRateByStretch(baseWidth: number, baseHeight: number) {
    // 表示更高
    const widthRatio = parseFloat((window.innerWidth / baseWidth).toFixed(5));
    const heightRatio = parseFloat(
      (window.innerHeight / baseHeight).toFixed(5)
    );
    return { widthRatio, heightRatio };
  }

  //数据大屏自适应函数
  const handleScreenAuto = () => {
    const designDraftWidth = 1920; //设计稿的宽度
    const designDraftHeight = 1080; //设计稿的高度
   
    // * 需保持的比例
    const baseProportion = parseFloat(
      (designDraftWidth / designDraftHeight).toFixed(5)
    );
    // * 当前屏幕宽高比
    const currentRate = parseFloat(
      (window.innerWidth / window.innerHeight).toFixed(5)
    );
    let scale = {
      widthRatio: 1,
      heightRatio: 1,
    };

    // 比例越大,则越宽,基准值采用高度,计算出宽度
    // 反之,则越高,基准值采用宽度,计算出高度
    scale =
      currentRate > baseProportion
        ? calcRateByHeight(designDraftWidth, designDraftHeight, baseProportion)
        : calcRateByWidth(designDraftWidth, designDraftHeight, baseProportion);
    //缩放比例
    (
      document.querySelector("#screen") as any
    ).style.transform = `scale(${scale.widthRatio}, ${scale.heightRatio}) translate(-50%)`;
  };
 
  //React的生命周期
  useEffect(() => {
    //初始化自适应  ----在刚显示的时候就开始适配一次
    handleScreenAuto();
    //绑定自适应函数   ---防止浏览器栏变化后不再适配
    window.onresize = () => handleScreenAuto();
    //退出大屏后自适应消失   ---这是react的组件销毁生命周期
    return () => {
      window.onresize = null;
    };
  }, []);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
.home-visualization {
  height: 100vh;
  width: 100vw;
  overflow: hidden;

  #screen {
    display: inline-block;
    width: 1920px; //设计稿的宽度
    height: 1080px; //设计稿的高度
    transform-origin: 0 0;
    position: absolute;
    left: 50%;
    background-image: url(../../assets/images/visualizationBg.png);
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
   
    ...
   
  }
}
  • 标题: 可视化大屏
  • 作者: 菇太帷i
  • 创建于 : 2024-06-24 08:43:00
  • 更新于 : 2025-09-18 06:39:53
  • 链接: https://blog.gutawei.com/2024/06/24/Technology Stack/可视化大屏/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
可视化大屏