vue+element項目實時監(jiān)聽div寬度的變化
背景:
vue項目中用到echarts圖表,頁面上有側(cè)邊欄,側(cè)邊欄收縮圖表不能自適應(yīng),想通過監(jiān)聽內(nèi)容部分的寬度讓圖表resize,試過window帶的resize,只能監(jiān)聽瀏覽器窗口大小變化,為了監(jiān)聽某元素區(qū)域的變化而使echarts的尺寸重置。

本次解決采用 element-resize-detector 可以完美的解決
思路:因為收縮側(cè)邊欄的時候右側(cè)的區(qū)域會自動適應(yīng),但是echarts不會隨之改變
element提供的 element-resize-detector 可以輕松解決問題的存在
第一步:在項目中安裝 element-resize-detector
npm install element-resize-detector
第二步:在項目中使用
html
<div id="test">
<div id="eChart">
</div>(1)script引入
<script src="node_modules/element-resize-detector/dist/element-resize-detector.min.js"></script>
// With default options (will use the object-based approach).
var erd = elementResizeDetectorMaker();
// With the ultra fast scroll-based approach.
// This is the recommended strategy.
var erdUltraFast = elementResizeDetectorMaker({
strategy: "scroll" //<- For ultra performance.
});
//監(jiān)聽元素size變化,觸發(fā)響應(yīng)事件
erd.listenTo(document.getElementById("test"), function(element) {
var width = element.offsetWidth;
var height = element.offsetHeight;
console.log("Size: " + width + "x" + height);
});(2)require 引入使用,在cli項目中使用,需要用到的頁面 ***.vue 引入
var elementResizeDetectorMaker = require("element-resize-detector")在mounted中啟用
var erd = elementResizeDetectorMaker()
erd.listenTo(document.getElementById("test"), function (element) {
var width = element.offsetWidth
var height = element.offsetHeight
that.$nextTick(function () {
console.log("Size: " + width + "x" + height)
//使echarts尺寸重置
that.$echarts.init(document.getElementById("eChart")).resize()
})
})因為gif圖為錄屏所以導(dǎo)航欄比較卡頓,勉強看一下哦

附大GIF圖壓縮工具地址:https://ezgif.com/resize/ezgif-1-d76f5cf7b36f.gif
基本解決問題,有更好的方案,歡迎留言指導(dǎo),謝謝
更新 自定義指令方法
directives: { // 使用局部注冊指令的方式
resize: { // 指令的名稱
bind(el, binding) { // el為綁定的元素,binding為綁定給指令的對象
let width = '', height = '';
function isReize() {
const style = document.defaultView.getComputedStyle(el);
if (width !== style.width || height !== style.height) {
binding.value(); // 關(guān)鍵
}
width = style.width;
height = style.height;
}
el.__vueSetInterval__ = setInterval(isReize, 300);
},
unbind(el) {
clearInterval(el.__vueSetInterval__);
}
}
}
//然后在html中
<div v-resize="resize"></div> // 綁定的resize是一個函數(shù)
//在methods中
resize() { // 當寬高變化時就會執(zhí)行
//執(zhí)行某些操作
}到此這篇關(guān)于vue+element項目里實時監(jiān)聽某個div寬度的變化,然后執(zhí)行相應(yīng)的事件的文章就介紹到這了,更多相關(guān)vue element監(jiān)聽div寬度變化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于在vue2中使用weixin-js-sdk的詳細步驟
公司最近有微信公眾號的需求,那么微信登錄授權(quán)和如何使用WX-JSSDk實現(xiàn)分享等等肯定是最頭疼的問題,這篇文章主要給大家介紹了關(guān)于在vue2中使用weixin-js-sdk的詳細步驟,需要的朋友可以參考下2024-07-07
Vue3+Vite環(huán)境變量與多環(huán)境配置詳解
本文介紹了在Vue3+Vite項目中配置和使用環(huán)境變量的完整方案,該方案實現(xiàn)了安全、靈活的環(huán)境變量管理,支持多環(huán)境開發(fā)和部署需求,感興趣的可以了解一下2026-05-05
vue2.0+vue-router構(gòu)建一個簡單的列表頁的示例代碼
這篇文章主要介紹了vue2.0+vue-router構(gòu)建一個簡單的列表頁的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
vue-cli3 配置開發(fā)與測試環(huán)境詳解
這篇文章主要介紹了vue-cli3 配置開發(fā)與測試環(huán)境詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
Vue——解決報錯 Computed property "****" was assigned to but it ha
這篇文章主要介紹了Vue——解決報錯 Computed property "****" was assigned to but it has no setter.的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12

