当前位置:   article > 正文

Vue大屏自适应_vue页面自适应屏幕大小

vue页面自适应屏幕大小
  1. <div class="scale-box" :style="{ transform: `scale(${scaleX},${scaleY}) translate(-50%, -50%)` }" ></div>
  2. // 这是包裹元素 最外层的 利用 css3 动画属性 transform 整体页面进行 缩放缩小
  3. <style>
  4. .scale-box {
  5. width: 1920px;
  6. height: 1080px;
  7. transform-origin: 0 0;
  8. position: absolute;
  9. left: 50%;
  10. top: 50%;
  11. transition: 0.3s;
  12. overflow: hidden;
  13. }
  14. </style>
  15. mounted() {
  16. this.styleScale()
  17. },
  18. methods: {
  19. styleScale(){
  20. let that = this;
  21. console.log(111111)
  22. window.addEventListener('resize', this.setScale);
  23. window.addEventListener('keydown', function(e) {
  24. var e = event || window.event || arguments.callee.caller.arguments[0];
  25. if (e && e.keyCode == 122) {
  26. //捕捉F11键盘动作
  27. e.preventDefault(); //阻止F11默认动作
  28. var el = document.documentElement;
  29. var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen; //定义不同浏览器的全屏API //执行全屏
  30. if (typeof rfs != 'undefined' && rfs) {
  31. rfs.call(el);
  32. } else if (typeof window.ActiveXObject != 'undefined') {
  33. var wscript = new ActiveXObject('WScript.Shell');
  34. if (wscript != null) {
  35. wscript.SendKeys('{F11}');
  36. }
  37. }
  38. const weListenScreen = function() {
  39. if (document.webkitIsFullScreen) {
  40. // console.log(1)
  41. that.flag = true; // 全屏
  42. console.log(that.flag, '全屏');
  43. that.setScale();
  44. } else {
  45. that.flag = false; // 不全屏
  46. console.log(that.flag, '不全屏');
  47. that.setScale();
  48. // console.log(2)
  49. document.addEventListener('webkitfullscreenchange', weListenScreen);
  50. }
  51. };
  52. document.addEventListener('webkitfullscreenchange', weListenScreen, false);
  53. }
  54. });
  55. this.flag = false;
  56. this.setScale();
  57. },
  58. getScale() {
  59. // 固定好16:9的宽高比,计算出最合适的缩放比,宽高比可根据需要自行更改
  60. let ww = window.innerWidth / 1920;
  61. let wh = window.innerHeight / 1080;
  62. return ww < wh ? ww : wh;
  63. },
  64. setScale() {
  65. var ratio = 0,
  66. screen = window.screen,
  67. ua = navigator.userAgent.toLowerCase();
  68. if (window.devicePixelRatio !== undefined) {
  69. ratio = window.devicePixelRatio;
  70. } else if (~ua.indexOf('msie')) {
  71. if (screen.deviceXDPI && screen.logicalXDPI) {
  72. ratio = screen.deviceXDPI / screen.logicalXDPI;
  73. }
  74. } else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
  75. ratio = window.outerWidth / window.innerWidth;
  76. }
  77. if (ratio) {
  78. ratio = Math.round(ratio * 100); // 计算浏览器百分比缩放
  79. }
  80. console.log(ratio)
  81. if (this.flag) {
  82. this.scaleX = this.getScale();
  83. this.scaleY = this.getScale();
  84. console.log(this.flag, 1);
  85. } else { // 根据屏幕大小自适应 适应任何屏幕 但是会留黑边 只有在F11 全屏下 才能铺满屏幕
  86. console.log(this.flag, 2);
  87. this.scaleX = this.getScale();
  88. this.scaleY = this.getScale();
  89. return
  90. // 下面这个根据 屏幕大小自定缩放比例 能占满全屏 不是F11 全屏状态下 会有压缩 圆形会变形
  91. switch (window.screen.availWidth) {
  92. case 1920:
  93. switch (ratio) {
  94. case 175:
  95. this.scaleX = 0.575;
  96. this.scaleY = 0.49537;
  97. break;
  98. case 150:
  99. this.scaleX = 0.67;
  100. this.scaleY = 0.578704;
  101. break;
  102. case 125:
  103. this.scaleX = 0.799999;
  104. this.scaleY = 0.694444;
  105. break;
  106. case 110:
  107. this.scaleX = 0.91;
  108. this.scaleY = 0.788889;
  109. break;
  110. case 100:
  111. this.scaleX = 1;
  112. this.scaleY = 0.867593;
  113. break;
  114. case 90:
  115. this.scaleX = 1.1111;
  116. this.scaleY = 0.963889;
  117. break;
  118. case 80:
  119. this.scaleX = 1.24999;
  120. this.scaleY = 1.08426;
  121. break;
  122. case 75:
  123. this.scaleX = 1.33333;
  124. this.scaleY = 1.15648;
  125. break;
  126. default:
  127. this.scaleX = this.getScale();
  128. this.scaleY = this.getScale();
  129. }
  130. break;
  131. case 1366:
  132. switch (ratio) {
  133. case 175:
  134. this.scaleX = 0.575;
  135. this.scaleY = 0.49537;
  136. break;
  137. case 150:
  138. this.scaleX = 0.4744;
  139. this.scaleY = 0.386111;
  140. break;
  141. case 125:
  142. this.scaleX = 0.57;
  143. this.scaleY = 0.462963;
  144. break;
  145. case 110:
  146. this.scaleX = 0.648;
  147. this.scaleY = 0.525926;
  148. break;
  149. case 100:
  150. this.scaleX = 0.712;
  151. this.scaleY = 0.5898704;
  152. break;
  153. case 90:
  154. this.scaleX = 0.791;
  155. this.scaleY = 0.642593;
  156. break;
  157. case 80:
  158. this.scaleX = 0.89;
  159. this.scaleY = 0.723148;
  160. break;
  161. case 75:
  162. this.scaleX = 0.949;
  163. this.scaleY = 0.771296;
  164. break;
  165. default:
  166. this.scaleX = this.getScale();
  167. this.scaleY = this.getScale();
  168. }
  169. break;
  170. case 1360:
  171. switch (ratio) {
  172. case 175:
  173. this.scaleX = 0.575;
  174. this.scaleY = 0.49537;
  175. break;
  176. case 150:
  177. this.scaleX = 0.4744;
  178. this.scaleY = 0.386111;
  179. break;
  180. case 125:
  181. this.scaleX = 0.57;
  182. this.scaleY = 0.462963;
  183. break;
  184. case 110:
  185. this.scaleX = 0.648;
  186. this.scaleY = 0.525926;
  187. break;
  188. case 100:
  189. this.scaleX = 0.712;
  190. this.scaleY = 0.5898704;
  191. break;
  192. case 90:
  193. this.scaleX = 0.791;
  194. this.scaleY = 0.642593;
  195. break;
  196. case 80:
  197. this.scaleX = 0.89;
  198. this.scaleY = 0.723148;
  199. break;
  200. case 75:
  201. this.scaleX = 0.949;
  202. this.scaleY = 0.771296;
  203. break;
  204. default:
  205. this.scaleX = this.getScale();
  206. this.scaleY = this.getScale();
  207. }
  208. break;
  209. default: // 按着 1680 分辨率调的
  210. switch (ratio) {
  211. case 175:
  212. this.scaleX = 0.50;
  213. this.scaleY = 0.47537;
  214. break;
  215. case 150:
  216. this.scaleX = 0.58144;
  217. this.scaleY = 0.5386111;
  218. break;
  219. case 125:
  220. this.scaleX = 0.6948;
  221. this.scaleY = 0.6648;
  222. break;
  223. case 110:
  224. this.scaleX = 0.7948;
  225. this.scaleY = 0.7725926;
  226. break;
  227. case 100:
  228. this.scaleX = 0.872;
  229. this.scaleY = 0.81987;
  230. break;
  231. case 90:
  232. this.scaleX = 0.9691;
  233. this.scaleY = 0.942593;
  234. break;
  235. case 80:
  236. this.scaleX = 1.0911;
  237. this.scaleY = 1.02923148;
  238. break;
  239. case 75:
  240. this.scaleX = 1.162911;
  241. this.scaleY = 1.0911;
  242. break;
  243. default:
  244. this.scaleX = this.getScale();
  245. this.scaleY = this.getScale();
  246. }
  247. }
  248. }
  249. // console.log(ratio);
  250. }
  251. },
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/436187
推荐阅读
相关标签
  

闽ICP备14008679号