screen.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div class="wrapper">
  3. <div
  4. class="screen"
  5. :style="'width:' + width + 'px;height:' + height + 'px'"
  6. >
  7. <div class="content-wrap" id="screen" :style="style">
  8. <slot />
  9. </div>
  10. </div>
  11. </div>
  12. </template>
  13. <script scoped>
  14. export default {
  15. props: {
  16. designWidth: {
  17. // 设计图尺寸宽
  18. type: Number,
  19. default: 1920
  20. },
  21. designHeight: {
  22. // 设计图尺寸高
  23. type: Number,
  24. default: 1080
  25. }
  26. },
  27. data() {
  28. return {
  29. width: 1920,
  30. height: 1080,
  31. style: {
  32. width: `${this.designWidth}px`,
  33. height: `${this.designHeight}px`,
  34. transform: 'scale(1)' // 默认不缩放,垂直水平居中
  35. }
  36. }
  37. },
  38. mounted() {
  39. this.setScale()
  40. this.onresize = this.debounce(() => this.setScale(), 100)
  41. window.addEventListener('resize', this.onresize)
  42. },
  43. beforeUnmount() {
  44. window.removeEventListener('resize', this.onresize)
  45. },
  46. methods: {
  47. // 防抖
  48. debounce(fn, t) {
  49. const delay = t || 500
  50. let timer
  51. return function () {
  52. const args = arguments
  53. if (timer) {
  54. clearTimeout(timer)
  55. }
  56. const that = this
  57. timer = setTimeout(() => {
  58. timer = null
  59. fn.apply(that, args)
  60. }, delay)
  61. }
  62. },
  63. // 设置缩放比例
  64. setScale() {
  65. const scale = document.documentElement.clientWidth / document.documentElement.clientHeight < this.designWidth / this.designHeight ?
  66. (document.documentElement.clientWidth / this.designWidth) :
  67. (document.documentElement.clientHeight / this.designHeight)
  68. this.style = {
  69. width: `${this.designWidth}px`,
  70. height: `${this.designHeight}px`,
  71. transform: `scale(${scale})` // 默认不缩放,垂直水平居中
  72. }
  73. this.width = document.documentElement.clientWidth
  74. this.height = document.documentElement.clientHeight
  75. }
  76. }
  77. }
  78. </script>
  79. <style scoped lang="scss" type="text/scss">
  80. .wrapper {
  81. position: relative;
  82. left: 0;
  83. top: 0;
  84. box-sizing: border-box;
  85. width: 100vw;
  86. height: 100vh;
  87. .screen {
  88. min-height: 100%;
  89. overflow: auto;
  90. background-color: #142E48;
  91. background-repeat: no-repeat;
  92. background-size: 100% 100%;
  93. .content-wrap {
  94. position: relative;
  95. transform-origin: 0 0;
  96. }
  97. }
  98. }
  99. </style>