vue使用element-resize-detector監(jiān)聽(tīng)元素寬度變化方式
使用element-resize-detector監(jiān)聽(tīng)元素寬度變化
如圖,當(dāng)我們切換左側(cè)菜單展示效果的時(shí)候,右側(cè)內(nèi)容會(huì)對(duì)應(yīng)變寬,但此時(shí)的echarts并不能執(zhí)行自適應(yīng)效果,這是因?yàn)榍袚Q菜單展示效果并沒(méi)有觸發(fā)window.onresize,所以為解決類(lèi)似此問(wèn)題,我們可使用element-resize-detector

1、引入element-resize-detector,npm install element-resize-detector --save
2、在對(duì)應(yīng)位置上引入即可

let elementResizeDetectorMaker = require("element-resize-detector");
//監(jiān)聽(tīng)元素變化
let erd = elementResizeDetectorMaker();
let that = this;
erd.listenTo(document.getElementById("bar"), function (element) {
that.$nextTick(function () {
//使echarts尺寸重置
that.myEcharts.resize();
})
})
//監(jiān)聽(tīng)元素變化PS:如果在改變寬度過(guò)程中存在動(dòng)畫(huà)效果,此時(shí)我們可以使用防抖,使在動(dòng)畫(huà)結(jié)束后再resize,這樣做的好處是避免在動(dòng)畫(huà)過(guò)程中不斷進(jìn)行resize,造成界面卡頓,影響性能
節(jié)流與防抖代碼見(jiàn):http://m.fzitv.net/article/269597.htm
<template>
<div class="page">
<div id="bar" class="echarts"></div>
</div>
</template>
<script>
let elementResizeDetectorMaker = require("element-resize-detector");
import {debounce} from 'utils.js';
export default {
name:'page',
mounted(){
let erd = elementResizeDetectorMaker();
let that = this;
erd.listenTo(document.getElementById("bar"), debounce(this.resizeFunc))
},
methods:{
resizeFunc(element){
console.log(element);//element元素信息
that.$nextTick(function () {
//使echarts尺寸重置
that.myEcharts.resize();
})
}
}
}
</script>
<style lang="scss" scoped>
.page{
width:100%;
height:100%;
.echarts{
width:100%;
height:100%;
}
}
</style>全局element-resize-detector監(jiān)聽(tīng)DOM元素
解決方案
第一步:通過(guò)npm install element-resize-detector獲取elementResizeDetectorMaker
npm install element-resize-detector
第二步:將依賴(lài)引入import elementResizeDetectorMaker from ‘element-resize-detector’
import ElementResizeDetectorMaker from "element-resize-detector" Vue.prototype.$erd = ElementResizeDetectorMaker()
第三步:使用
? ? ? ? ? this.$erd.listenTo(document.getElementById("chinaMapChart"), (element)=>{
? ? ? ? ? ? this.resize()
? ? ? ? ? })如果不使用Lambda表達(dá)式作為監(jiān)聽(tīng)器,會(huì)出現(xiàn)不能獲取data和methods的情況,具體原因參考JavaScript高級(jí)教程
解決方案:
let that = this;
this.$erd.listenTo(document.getElementById("bar"), function (element) {
? ? that.$nextTick(function () {
? ? ? ? //使echarts尺寸重置
? ? ? ? that.myEcharts.resize();
? ? })
})
? ? ? ? ? ? //監(jiān)聽(tīng)元素變化總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue API中setup ref reactive函數(shù)使用教程
setup是用來(lái)寫(xiě)組合式api,內(nèi)部的數(shù)據(jù)和方法需要通過(guò)return之后,模板才能使用。在之前vue2中,data返回的數(shù)據(jù),可以直接進(jìn)行雙向綁定使用,如果我們把setup中數(shù)據(jù)類(lèi)型直接雙向綁定,發(fā)現(xiàn)變量并不能實(shí)時(shí)響應(yīng)。接下來(lái)就詳細(xì)看看它們的使用2022-12-12
Vue3中引入Pinia存儲(chǔ)庫(kù)使用示例詳解
這篇文章主要介紹了Vue3中引入Pinia存儲(chǔ)庫(kù)使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
解決vue頁(yè)面刷新或者后退參數(shù)丟失的問(wèn)題
下面小編就為大家分享一篇解決vue頁(yè)面刷新或者后退參數(shù)丟失的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Vue+element自定義指令如何實(shí)現(xiàn)表格橫向拖拽
這篇文章主要介紹了Vue+element自定義指令如何實(shí)現(xiàn)表格橫向拖拽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vuejs+element UI點(diǎn)擊編輯表格某一行時(shí)獲取內(nèi)容填入表單的示例
這篇文章主要介紹了vuejs+element UI點(diǎn)擊編輯表格某一行時(shí)獲取內(nèi)容填入表單的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
vue編寫(xiě)的功能強(qiáng)大的swagger-ui頁(yè)面及使用方式
swagger是一種標(biāo)準(zhǔn)的數(shù)據(jù)格式的定義,對(duì)于不同語(yǔ)言進(jìn)行實(shí)現(xiàn)一些注解API式的東西,能快速生成這種描述restful格式的api信息的json串,本文給大家詳細(xì)介紹vue編寫(xiě)的功能強(qiáng)大的swagger-ui頁(yè)面,感興趣的朋友跟隨小編一起看看吧2022-02-02

