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

本次解決采用 element-resize-detector 可以完美的解決
思路:因?yàn)槭湛s側(cè)邊欄的時(shí)候右側(cè)的區(qū)域會(huì)自動(dòng)適應(yīng),但是echarts不會(huì)隨之改變
element提供的 element-resize-detector 可以輕松解決問(wèn)題的存在
第一步:在項(xiàng)目中安裝 element-resize-detector
npm install element-resize-detector
第二步:在項(xiàng)目中使用
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項(xiàng)目中使用,需要用到的頁(yè)面 ***.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()
})
})因?yàn)間if圖為錄屏所以導(dǎo)航欄比較卡頓,勉強(qiáng)看一下哦

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

