方 案
|
實現方式
|
優點
|
缺點
|
vm vh
|
1.按照設計稿的尺寸,將px按比例計算轉為vw和vh
|
1.可以動態計算圖表的寬高,字體等,靈活性較高 2.當屏幕比例跟 ui 稿不一致時,不會出現兩邊留白情況
|
1.每個圖表都需要單獨做字體、間距、位移的適配,比較麻煩
|
scale
|
1.通過 scale 屬性,根據屏幕大小,對圖表進行整體的等比縮放
|
1.代碼量少,適配簡單2.一次處理后不需要在各個圖表中再去單獨適配 3.文字,圖片等大小均能自動適配
|
1.因為是根據 ui 稿等比縮放,當大屏跟 ui 稿的比例不一樣時,會出現周邊留白情況2.當縮放比例過大時候,字體會有一點點模糊,就一點點3.當縮放比例過大時候,事件熱區會偏移。
|
插件v-scale-screen
|
是使用 css 屬性 transform 實現縮放效果的一個大屏自適應組件,通過 scale 進行等比例計算,達到等比例縮放的效果
|
可以通過api調整原稿的寬高
|
|
方案一:vw vh
1.當屏幕正好為16:9的時候
2.當屏幕的尺寸比例大于 16:9 (左右拉長)

3.當屏幕的尺寸比例小于 16:9 時(左右變窄或者上下拉高)

實現方法:
css 方案 - sass
utils.scss
@use "sass:math"; $designWidth: 1920; $designHeight: 1080; @function vw($px) { @return math.div($px, $designWidth) * 100vw; } @function vh($px) { @return math.div($px, $designHeight) * 100vh; } 復制代碼
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
路徑配置只需在vue.config.js里配置一下utils.scss的路徑,就可以全局使用了
vue.config.js
module.exports = { css: { loaderOptions: { sass: { prependData: `@import "@/assets/css/utils.scss";` } } }, } 在 .vue 中使用 <template> <div class="box"> </div> </template> <script> export default{ name: "Box", } </script> <style lang="scss" scoped="scoped"> .box{ width: vw(300); height: vh(100); font-size: vh(16); background-color: black; margin-left: vw(10); margin-top: vh(10); border: vh(2) solid red; } </style>
-
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
css 方案 - less
utils.less
@charset "utf-8"; @designWidth: 1920; @designHeight: 1080; .px2vw(@name, @px) { @{name}: (@px / @designWidth) * 100vw; } .px2vh(@name, @px) { @{name}: (@px / @designHeight) * 100vh; } .px2font(@px) { font-size: (@px / @designWidth) * 100vw; } 路徑配置在vue.config.js里配置一下utils.less <style lang="less" scoped="scoped"> .box{ .px2vw(width, 300); .px2vh(height, 100); .px2font(16); .px2vw(margin-left, 300); .px2vh(margin-top, 100); background-color: black; } </style> 定義 js 樣式處理函數 const designWidth = 1920; const designHeight = 1080; export const px2vw = (_px) => { return (_px * 100.0) / designWidth + 'vw'; }; export const px2vh = (_px) => { return (_px * 100.0) / designHeight + 'vh'; }; export const px2font = (_px) => { return (_px * 100.0) / designWidth + 'vw'; };
-
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
屏幕變化后,圖表自動調整
這種使用方式有個弊端,就是屏幕尺寸發生變化后,需要手動刷新一下才能完成自適應調整
為了解決這個問題,你需要在各個圖表中監聽頁面尺寸變化,重新調整圖表,在 vue 項目中,也可以借助element-resize-detector,最好封裝個 resize 的指令,在各圖表中就只要使用該指令就可以了,畢竟作為程序員,能偷懶就偷懶
解決方案一
-
安裝 element-resize-detector
npm install element-resize-detector --save
-
封裝成自定義指令使用
import * as ECharts from "echarts"; import elementResizeDetectorMaker from "element-resize-detector"; import Vue from "vue"; const HANDLER = "_vue_resize_handler"; function bind(el, binding) { el[HANDLER] = binding.value ? binding.value : () => { let chart = ECharts.getInstanceByDom(el); if (!chart) { return; } chart.resize(); }; elementResizeDetectorMaker().listenTo(el, el[HANDLER]); } function unbind(el) { elementResizeDetectorMaker().removeListener(el, el[HANDLER]); delete el[HANDLER]; } Vue.directive("chart-resize", { bind, unbind });
-
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
-
main.js 中引入
import '@/directive/directive';
-
html 代碼中使用
<template> <div class="linechart"> <div ref="chart" v-chart-resize class="chart"></div> </div> </template>
這里要注意的是,圖表中如果需要 tab 切換動態更新圖表數據,在更新數據時一定不要用 echarts 的 dispose 方法先將圖表移除,再重新繪制,因為 resize 指令中掛載到的圖表實例還是舊的,就監聽不到新的 chart 元素的 resize 了,更新數據只需要用 chart 的 setOption 方法重新設置配置項即可。
解決方案二
1.在echarts中可以echarts.init(chatDom).resize()來解決寬高的自適應問題
let chatDom = document.getElementById('main'); let myChart = this.$echarts.init(chatDom); setTimeout(() => { window.addEventListener('resize', () => { this.$echarts.init(chatDom).resize(); }); }, 20);
2.在DataV中可以添加key來解決
<dv-water-level-pond :config="config2" :key="key" ref="pie2" /> data(){ return { key: 1 } }, mounted() { this.pieOutlineFunc(); }, methods: { pieOutlineFunc() { var _this = this; window.addEventListener('resize', function (e) { _this.$nextTick(() => { console.log(_this.$refs.pie2); _this.key++; }); }); } }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
方案二:csale
通過 css 的 scale 屬性,根據屏幕大小,用js監測屏幕的大小變化對圖表進行整體的等比縮放,從而達到自適應效果
當屏幕的尺寸比例剛好是 16:9 時,頁面能剛好全屏展示,內容占滿顯示器

當屏幕尺寸比例大于 16:9 時,上下左右留白,左右占滿并居中,顯示比例保持 16:9

當屏幕尺寸比例大于 16:9 時,頁面左右留白,上下占滿并居中,顯示比例保持 16:9

上代碼
html
<template> <div class="screen-root"> <div class="screen" id="screen"> <div class="div1"> <h1>11111111111</h1> </div> <div class="div2"> <h1>2222222222</h1> </div> <div class="div3"> <h1>3333333333</h1> </div> </div> </div> </template>
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
js
<script> export default { mounted() { this.handleScreenAuto(); window.onresize = () => this.handleScreenAuto(); }, deleted() { window.onresize = null; }, methods: { handleScreenAuto() { const designDraftWidth = 1920; const designDraftHeight = 1080; const scale = document.documentElement.clientWidth / document.documentElement.clientHeight < designDraftWidth / designDraftHeight ? document.documentElement.clientWidth / designDraftWidth : document.documentElement.clientHeight / designDraftHeight; document.querySelector('#screen').style.transform = `scale(${scale}) translate(-50%,-50%)`; return 1; } } }; </script>
-
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
css
<style lang="scss" scoped> h1 { color: red; font-size: 50px; } .screen-root { height: 100vh; width: 100vw; .screen { display: inline-block; width: 1920px; height: 1080px; transform-origin: 0 0; position: absolute; left: 50%; top: 50%; border: 2px solid rgb(31, 210, 145); box-sizing: border-box; display: flex; .div1 { background-color: #fff; height: 100%; text-align: center; flex: 0 1 30%; } .div2 { background-color: rgb(133, 14, 14); height: 100%; text-align: center; flex: 0 1 40%; } .div3 { background-color: rgb(61, 6, 188); height: 100%; text-align: center; flex: 0 1 30%; } } } </style>
-
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
方案三:插件v-scale-screen
它其實也是通過 scale 進行等比例計算放大和縮小的,和方案二的原理是一樣的,還可以通過api調整樣式,源碼地址和對應的API

使用方法:
1.vue2請使用v-scale-screen@1.0.0版本,vue3請使用v-scale-screen@2.0.0版本
npm install v-scale-screen@1.0.0 -save
# or
yarn add v-scale-screen
2.使用-vue2中使用插件導入,vue3以組件導入
vue2 import VScaleScreen from 'v-scale-screen' Vue.use(VScaleScreen) 組件內使用 <v-scale-screen width="1920" height="1080" :boxStyle="boxStyle"> <div> <v-chart>....</v-chart> <v-chart>....</v-chart> <v-chart>....</v-chart> <v-chart>....</v-chart> <v-chart>....</v-chart> </div> </v-scale-screen> data() { return { boxStyle: { backgroundColor: 'green' }, }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
vue3 <v-scale-screen width="1920" height="1080"> <div> <v-chart>....</v-chart> <v-chart>....</v-chart> <v-chart>....</v-chart> <v-chart>....</v-chart> <v-chart>....</v-chart> </div> </v-scale-screen> <script> import VScaleScreen from 'v-scale-screen' export default { components:{ VScaleScreen } } </script>
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
vue2演示地址
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
藍藍設計建立了UI設計分享群,每天會分享國內外的一些優秀設計,如果有興趣的話,可以進入一起成長學習,請加藍小助,微信號:ben_lanlan,報下信息,藍小助會請您入群。歡迎您加入噢~~希望得到建議咨詢、商務合作,也請與我們聯系01063334945。
分享此文一切功德,皆悉回向給文章原作者及眾讀者.
免責聲明:藍藍設計尊重原作者,文章的版權歸原作者。如涉及版權問題,請及時與我們取得聯系,我們立即更正或刪除。
藍藍設計( www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網站建設 、平面設計服務、UI設計公司、界面設計公司、UI設計服務公司、數據可視化設計公司、UI交互設計公司、高端網站設計公司、UI咨詢、用戶體驗公司、軟件界面設計公司