vue如何監(jiān)聽某個(gè)元素的大小變化
vue監(jiān)聽某個(gè)元素的大小變化
<div class="dialog_wrap"></div>
console.log(box1.offsetWidth, box1.offsetHeight); this.width = box1.offsetWidth; //offsetWidth屬性可以返回對象的padding+border+width屬性值之和 this.height = box1.offsetHeight; const resizeObserver = new ResizeObserver((entries) => { for (let entry of entries) { if (entry.target.offsetWidth != this.width) { // 打印出entry.target 可以看到此處可以進(jìn)行元素的各種變化的監(jiān)聽 //此處直接寫監(jiān)聽變化后要處理的邏輯即可 //防抖處理(這里做一些監(jiān)聽變化后的邏輯處理) if (this.konvatimer !== null) { clearTimeout(this.konvatimer); } this.konvatimer = setTimeout(() => { console.log("變化", entry.target.offsetWidth); this.width = entry.target.offsetWidth; this.height = entry.target.offsetHeight; this.layer = Konva.Node.create(this.json, "screenTopo").findOne( "Layer" ); this.stage = new Konva.Stage({ container: "screenTopo", width: this.width, height: this.height, }); this.stage.add(this.layer); this.handleInitKonva(); }, 100); } } }); resizeObserver.observe(document.querySelector(".dialog_wrap")); //observe方法 用于監(jiān)聽指定的 Element 或SVGElement
使用element-resize-detector監(jiān)聽元素大小變化
1、應(yīng)用場景
底部固定按鈕欄使用 position: fixed 固定定位,寬度等于右側(cè)內(nèi)容欄的寬度,當(dāng)我們左側(cè)菜單欄收起的時(shí)候,其寬度也能夠自適應(yīng)變化。
也就是說底部欄的寬度需要監(jiān)聽其父元素右側(cè)內(nèi)容的寬度從而實(shí)現(xiàn)自適應(yīng)變化。
2、 解決方法
使用 element-resize-detector
(1)下載
npm i element-resize-detector --save
(2)導(dǎo)入
const elementResizeDetectorMaker = require('element-resize-detector')
(3)使用
// 監(jiān)聽右側(cè)page元素寬度變化,更新底部固定按鈕欄寬度 let erd = elementResizeDetectorMaker(); erd.listenTo(document.getElementById('id元素'), (ele) => { console.log(ele.clientWidth); })
3、源碼
<template> <div class="page" ref="page"> <div class="page-footer" :style="{'width': footerWidth }"> <el-button type="primary" @click="handleSubmit">保 存</el-button> </div> </div> </template>
<script> const elementResizeDetectorMaker = require('element-resize-detector') export default { name: "home", data() { return { footerWidth: "500px" // 底部固定按鈕欄寬度 } }, mounted() { // 監(jiān)聽右側(cè)page元素寬度變化,更新底部固定按鈕欄寬度 let erd = elementResizeDetectorMaker(); erd.listenTo(this.$refs.page, (ele) => { this.footerWidth = ele.clientWidth - 32 + "px"; }) } } </script>
<style lang="less" scoped> .page-footer { height: 52px; margin: 0 16px; border-top: 1px solid #e0e0e0; display: flex; align-items: center; position: fixed; bottom: 0; z-index: 99; background-color: rgba(255, 255, 255, 0.9); .el-button { height: 32px; width: 96px; line-height: 32px; padding: 0; border-radius: 2px; } } </style>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題
今天小編就為大家分享一篇解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼詳解
在使用elementUI el-form 中,對于業(yè)務(wù)不同的時(shí)候可能會(huì)產(chǎn)生不同表單結(jié)構(gòu),但是都是存在同一個(gè)表單控件el-form中。這篇文章主要介紹了vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼,需要的朋友可以參考下2019-05-05使用vue cli4.x搭建vue項(xiàng)目的過程詳解
這篇文章主要介紹了使用vue cli4.x搭建vue項(xiàng)目的過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Vue項(xiàng)目中Element UI組件未注冊的問題原因及解決方法
在 Vue 項(xiàng)目中使用 Element UI 組件庫時(shí),開發(fā)者可能會(huì)遇到一些常見問題,例如組件未正確注冊導(dǎo)致的警告或錯(cuò)誤,本文將詳細(xì)探討這些問題的原因、解決方法,以及如何在需要時(shí)撤銷相關(guān)操作,需要的朋友可以參考下2025-01-01