看板自适应-scale方案

vue3版本

<!--
 * @Author: yangzihua
 * @Date: 2024-04-12 15:26:36
 * @LastEditors: yangzihua
 * @LastEditTime: 2024-04-12 16:07:50
 * @Description: 
-->
<template>
  <div
    class="main-scale"
    :style="{ width: innerWidth + 'px', height: innerHeight + 'px', backgroundColor: '#010916' }"
  >
    <div class="board" :style="{ transform: `scale(${getScale()})` }">
      <slot></slot>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';

let innerWidth = ref(window.innerWidth);
let innerHeight = ref(window.innerHeight);

onMounted(() => {
  window.addEventListener('resize', handleResize);
});

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize);
});

function getScale() {
  const widthBg = 1920;
  const heightBg = 1080;
  const widthScale = innerWidth.value / widthBg;
  const heightScale = innerHeight.value / heightBg;
  return Math.min(widthScale, heightScale);
}

function handleResize() {
  innerWidth.value = window.innerWidth;
  innerHeight.value = window.innerHeight;
}
</script>

<style scoped>
.main-scale {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}

.board {
  flex: 0 0 auto;
}
</style>

react版本-类组件


import classNames from 'classnames';
import React from 'react';
import styles from './index.less';

class Scale extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      innerWidth: window.innerWidth,
      innerHeight: window.innerHeight,
    };
  }

  componentDidMount() {
    window.addEventListener('resize', this.listenWindow);
    return () => {
      document.removeEventListener('resize', this.listenWindow);
    };
  }

  listenWindow = () => {
    this.setState({
      innerWidth: window.innerWidth,
      innerHeight: window.innerHeight,
    });
  };

  getScale = () => {
    const { widthBg = 1920, heightBg = 1080 } = this.props;
    const widthScale = window.innerWidth / widthBg;
    const heightScale = window.innerHeight / heightBg;
    return widthScale > heightScale ? heightScale : widthScale;
  };

  render() {
    const { children, mainColor = '#fff', boardStyle } = this.props;
    const scaleValue = this.getScale();
    const { innerWidth, innerHeight } = this.state;
    return (
      <div
        className={styles.main}
        style={{
          width: innerWidth,
          height: innerHeight,
          backgroundColor: mainColor,
        }}
      >
        <div
          className={classNames(styles.board, boardStyle)}
          style={{
            transform: `scale(${scaleValue})`,
          }}
        >
          {children}
        </div>
      </div>
    );
  }
}

export default Scale;


// index.less
.main {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}

.board {
  flex: 0 0 auto;
}
欢迎阅读!

评论

  1. Edgar4841
    Windows Chrome
    3 月前
    2025-8-06 6:59:02
  2. Conner4257
    Windows Chrome
    3 月前
    2025-8-06 21:49:33
  3. Lisa4330
    Windows Chrome
    3 月前
    2025-8-07 16:06:21
  4. Heidi3916
    Windows Chrome
    3 月前
    2025-8-07 23:33:21
  5. Ingrid4399
    Windows Chrome
    3 月前
    2025-8-08 5:24:04
  6. Jason3802
    Windows Chrome
    3 月前
    2025-8-09 11:51:43
  7. Jasper4542
    Windows Chrome
    3 月前
    2025-8-09 18:27:42
  8. Jasmine4833
    Windows Chrome
    3 月前
    2025-8-11 6:49:29
  9. Daniel3018
    Windows Chrome
    3 月前
    2025-8-12 6:43:21
  10. Alejandro2597
    Windows Chrome
    3 月前
    2025-8-12 14:04:18
  11. Anna2628
    Windows Chrome
    3 月前
    2025-8-13 2:57:58
  12. Hallie4988
    Windows Chrome
    3 月前
    2025-8-16 9:25:20
  13. Theresa1007
    Windows Chrome
    3 月前
    2025-8-16 19:05:25
  14. Brandon4783
    Windows Chrome
    3 月前
    2025-8-20 18:33:38
  15. Bella3236
    Windows Chrome
    3 月前
    2025-8-21 7:01:10
  16. Alaina2297
    Windows Chrome
    3 月前
    2025-8-22 15:02:16
  17. Ray4455
    Windows Chrome
    3 月前
    2025-8-23 21:57:50
  18. Gerard4041
    Windows Chrome
    3 月前
    2025-8-25 15:48:09
  19. Anderson11
    Windows Chrome
    2 月前
    2025-8-27 3:00:48
  20. Lori109
    Windows Chrome
    2 月前
    2025-8-27 9:47:59
  21. Carolyn3166
    Windows Chrome
    2 月前
    2025-8-27 17:11:53
  22. Douglas3759
    Windows Chrome
    2 月前
    2025-8-29 9:42:11
  23. Hank847
    Windows Chrome
    2 月前
    2025-8-31 19:36:14
  24. Reed1169
    Windows Chrome
    2 月前
    2025-9-02 7:08:16
  25. Gloria871
    Windows Chrome
    2 月前
    2025-9-03 14:03:30
  26. Luke3112
    Windows Chrome
    2 月前
    2025-9-03 22:25:02
  27. Marc1162
    Windows Chrome
    2 月前
    2025-9-04 6:25:53
  28. Patricia3951
    Windows Chrome
    2 月前
    2025-9-04 18:52:10
  29. Sophia4326
    Windows Chrome
    2 月前
    2025-9-06 8:39:37
  30. Christine1350
    Windows Chrome
    2 月前
    2025-9-09 10:14:28
  31. Genesis3695
    Windows Chrome
    2 月前
    2025-9-09 11:43:37
  32. Gianna4015
    Windows Chrome
    2 月前
    2025-9-09 21:16:54
  33. Ayden3423
    Windows Chrome
    2 月前
    2025-9-12 19:46:42
  34. Michelle1149
    Windows Chrome
    2 月前
    2025-9-16 17:20:20
  35. Betty2859
    Windows Chrome
    2 月前
    2025-9-18 5:23:42
  36. Josiah1909
    Windows Chrome
    2 月前
    2025-9-20 16:58:04
  37. Adria3648
    Windows Chrome
    2 月前
    2025-9-22 10:54:50
  38. Ruby946
    Windows Chrome
    2 月前
    2025-9-22 23:47:58
  39. Beth2751
    Windows Chrome
    1 月前
    2025-9-25 9:14:38
  40. Roman1649
    Windows Chrome
    1 月前
    2025-9-28 12:36:13
  41. Bella2407
    Windows Chrome
    1 月前
    2025-9-29 0:04:52
  42. Eric1870
    Windows Chrome
    1 月前
    2025-9-29 2:08:25
  43. Agnes2196
    Windows Chrome
    1 月前
    2025-9-29 6:16:17
  44. Renee205
    Windows Chrome
    1 月前
    2025-10-02 5:58:57
  45. Avery4816
    Windows Chrome
    1 月前
    2025-10-03 15:21:57
  46. Pamela4288
    Windows Chrome
    1 月前
    2025-10-04 2:47:43
  47. Leona2149
    Windows Chrome
    1 月前
    2025-10-06 19:29:36
  48. Neal748
    Windows Chrome
    4 周前
    2025-10-12 3:48:10
  49. Debbie2019
    Windows Chrome
    4 周前
    2025-10-15 1:33:19
  50. Monica624
    Windows Chrome
    3 周前
    2025-10-17 3:18:47
  51. Hayden189
    Windows Chrome
    3 周前
    2025-10-20 3:20:17
  52. Madelyn3882
    Windows Chrome
    3 周前
    2025-10-20 14:32:11
  53. Callie36
    Windows Chrome
    3 周前
    2025-10-20 17:27:18
  54. Gracie2583
    Windows Chrome
    3 周前
    2025-10-21 23:40:00
  55. Heather621
    Windows Chrome
    2 周前
    2025-10-26 9:59:14
  56. Judy3171
    Windows Chrome
    2 周前
    2025-10-26 18:23:03
  57. Madelyn4597
    Windows Chrome
    1 周前
    2025-11-01 8:17:33
  58. Andre3612
    Windows Chrome
    1 周前
    2025-11-01 11:20:28

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇