当前位置:   article > 正文

可视化大屏适配方案_大屏可视化适配方案

大屏可视化适配方案

创作背景

在做可视化大屏项目的时候,适配不同分辨率屏幕的电脑是最头疼的问题,那么,有没有一种方案,可以解决大屏适配问题呢。

在不懈的努力查找以及总结后,得到了一套,个人认为比较科学的适配方案。

线上demo例子

方案一

设计图宽高比例固定,并且跟随页面变化,等比例缩放

效果图

前端只需要按照设计图(px)单位设计开发即可,效果如图:

 无论窗口缩放,屏幕放大缩小,我们的可视化界面都可以按照设计图比例正常展示,不会出现字体模块爆出,或者拉伸问题。这就是我想要的适配方案。

实现代码

html

给最外层的盒子(body也行)加上标识(id, 或ref)

  1. <div id="appRef">
  2. xxx(反正就是整个可视化页面内容)
  3. </div>

css

这里仅展示id标识符的css样式

  1. #appRef {
  2. width: 1920px; /* 设计图宽 */
  3. height: 1080px; /* 设计图高 */
  4. position: absolute;
  5. top: 50%;
  6. left: 50%;
  7. transform: translate(-50%, -50%);
  8. transform-origin: left top;
  9. overflow: hidden;
  10. }

draw.js(html写法)

为了方便copy一点,这里就把普通html开发的适配js写法直接奉上

  1. // * 默认缩放值
  2. const scale = {
  3. width: '1',
  4. height: '1',
  5. }
  6. // * 设计稿尺寸(px)
  7. const baseWidth = 1920
  8. const baseHeight = 1080
  9. // * 需保持的比例
  10. const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
  11. let drawTiming = null
  12. calcRate()
  13. window.addEventListener('resize', resize)
  14. // * 初始化页面比例
  15. function calcRate () {
  16. const appRef = document.getElementById("appRef")
  17. if (!appRef) return
  18. // 当前宽高比
  19. const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
  20. if (appRef) {
  21. if (currentRate > baseProportion) {
  22. // 表示更宽
  23. scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
  24. scale.height = (window.innerHeight / baseHeight).toFixed(5)
  25. appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
  26. } else {
  27. // 表示更高
  28. scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
  29. scale.width = (window.innerWidth / baseWidth).toFixed(5)
  30. appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
  31. }
  32. }
  33. }
  34. // * 窗口大小变化
  35. function resize () {
  36. clearTimeout(drawTiming)
  37. // 加延迟是为了更好的看到变化的过程,不需要的可以不用
  38. drawTiming = setTimeout(() => {
  39. calcRate()
  40. }, 200)
  41. }

 draw.js(vue写法)

vue开发的同理,为了方便copy,代码直接奉上

  1. // * 默认缩放值
  2. const scale = {
  3. width: '1',
  4. height: '1',
  5. }
  6. // * 设计稿尺寸(px)
  7. const baseWidth = 1920
  8. const baseHeight = 1080
  9. // * 需保持的比例
  10. const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
  11. export default {
  12. data() {
  13. return {
  14. drawTiming: null // 定时函数
  15. }
  16. },
  17. mounted () {
  18. this.calcRate()
  19. window.addEventListener('resize', this.resize)
  20. },
  21. methods: {
  22. // 初始化页面比例
  23. calcRate () {
  24. const appRef = this.$refs["appRef"]
  25. if (!appRef) return
  26. // 当前宽高比
  27. const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
  28. if (appRef) {
  29. if (currentRate > baseProportion) {
  30. // 表示更宽
  31. scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
  32. scale.height = (window.innerHeight / baseHeight).toFixed(5)
  33. appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
  34. } else {
  35. // 表示更高
  36. scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
  37. scale.width = (window.innerWidth / baseWidth).toFixed(5)
  38. appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
  39. }
  40. }
  41. },
  42. // 窗口大小变化
  43. resize () {
  44. clearTimeout(this.drawTiming)
  45. this.drawTiming = setTimeout(() => {
  46. this.calcRate()
  47. }, 200)
  48. }
  49. },
  50. beforeDestroy () {
  51. window.removeEventListener('resize', this.resize)
  52. },
  53. }

使用(html开发)

在所有script的最后面引入就行,如图:

 使用(vue开发)

使用mixin混入引入(或者其他方式也行,个人习惯)

  1. <template>
  2. <div id="appRef" ref="appRef">xxx</div>
  3. </template>
  4. <script>
  5. import draw from "@/utils/draw"
  6. export default {
  7. mixins: [draw]
  8. }
  9. </script>

方案二

自适应全屏,不管屏幕宽高比如何,都以铺满全屏的形式进行展示。(适用于页面布局为百分比、或者flex自适应布局)

效果图

 

实现代码

draw.js(方案2)

  1. const appRef = document.getElementById("appRef")
  2. // 初始化页面
  3. calcRate()
  4. window.addEventListener('resize', calcRate)
  5. function calcRate () {
  6. if (!appRef) return
  7. // 获取浏览器缩放比例
  8. const browserRoom = getZoom()
  9. /**
  10. * 1. 先将宽高乘上浏览器缩放倍数x
  11. * 2. 再将整个页面用scale缩放 1/x 倍
  12. * 在视觉上,就感觉页面没有缩放
  13. */
  14. // 宽高
  15. const w = window.innerWidth * browserRoom
  16. const h = window.innerHeight * browserRoom
  17. // scale缩放比例
  18. const scl = parseFloat((1 / browserRoom).toFixed(5))
  19. // 页面重绘处理
  20. appRef.style.width = `${w}px`
  21. appRef.style.height = `${h}px`
  22. appRef.style.transform = `scale(${scl}, ${scl}) translate(-50%, -50%)`
  23. // 页面重绘完成后,再进行echarts重绘,保证echarts图表的自适应性
  24. myChart.resize()
  25. myChart2.resize()
  26. }
  27. // 获取浏览器缩放比例
  28. function getZoom() {
  29. let ratio = 0,
  30. screen = window.screen,
  31. ua = navigator.userAgent.toLowerCase();
  32. if (window.devicePixelRatio !== undefined) {
  33. ratio = window.devicePixelRatio;
  34. } else if (~ua.indexOf('msie')) {
  35. if (screen.deviceXDPI && screen.logicalXDPI) {
  36. ratio = screen.deviceXDPI / screen.logicalXDPI;
  37. }
  38. } else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
  39. ratio = window.outerWidth / window.innerWidth;
  40. }
  41. if (ratio){
  42. ratio = Math.round(ratio * 100);
  43. }
  44. return parseFloat(ratio/100).toFixed(2);
  45. }

draw.js(方案2-vue写法)

  1. // * 默认缩放值
  2. const scale = {
  3. width: '1',
  4. height: '1',
  5. }
  6. export default {
  7. data() {
  8. return {
  9. drawTiming: null // 定时函数
  10. }
  11. },
  12. mounted () {
  13. this.calcRate()
  14. window.addEventListener('resize', this.resize)
  15. },
  16. methods: {
  17. // 初始化页面比例
  18. calcRate () {
  19. const appRef = this.$refs["appRef"]
  20. if (!appRef) return
  21. // 获取浏览器缩放比例
  22. const browserRoom = this.getZoom()
  23. // 当前宽高比
  24. /**
  25. * 1. 先将宽高乘上浏览器缩放倍数x
  26. * 2. 再将整个页面用scale缩放 1/x 倍
  27. * 在视觉上,就感觉页面没有缩放
  28. */
  29. // 宽高
  30. const w = window.innerWidth * browserRoom
  31. const h = window.innerHeight * browserRoom
  32. // scale缩放比例
  33. const scl = parseFloat((1 / browserRoom).toFixed(5))
  34. // 页面重绘处理
  35. appRef.style.width = `${w}px`
  36. appRef.style.height = `${h}px`
  37. appRef.style.transform = `scale(${scl}, ${scl}) translate(-50%, -50%)`
  38. // 页面重绘完成后,再进行echarts重绘,保证echarts图表的自适应性
  39. // myChart.resize()
  40. // myChart2.resize()
  41. },
  42. // 窗口大小变化
  43. resize () {
  44. clearTimeout(this.drawTiming)
  45. this.drawTiming = setTimeout(() => {
  46. this.calcRate()
  47. }, 200)
  48. },
  49. // 获取浏览器缩放比例
  50. getZoom() {
  51. let ratio = 0,
  52. screen = window.screen,
  53. ua = navigator.userAgent.toLowerCase();
  54. if (window.devicePixelRatio !== undefined) {
  55. ratio = window.devicePixelRatio;
  56. } else if (~ua.indexOf('msie')) {
  57. if (screen.deviceXDPI && screen.logicalXDPI) {
  58. ratio = screen.deviceXDPI / screen.logicalXDPI;
  59. }
  60. } else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
  61. ratio = window.outerWidth / window.innerWidth;
  62. }
  63. if (ratio){
  64. ratio = Math.round(ratio * 100);
  65. }
  66. return parseFloat(ratio/100).toFixed(2);
  67. }
  68. },
  69. beforeDestroy () {
  70. window.removeEventListener('resize', this.resize)
  71. },
  72. }

总结:只要是按照设计图,正常开发的大屏项目,此方案基本都能很好的完成适配,切记别在项目中用rem单位就行,这玩意儿不行,拜拜~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/566513
推荐阅读
相关标签
  

闽ICP备14008679号