当前位置:网站首页>JS monitors the width and height changes of div
JS monitors the width and height changes of div
2022-06-25 12:04:00 【GIS roast lamb leg is delicious】
Preface explains
1. Monitoring will be encountered during development div The width and height of . For example, when the width and height change echarts Need to redraw .
2. Realization principle : hold div Put it in iframe in , monitor iframe Of resize event .
vue call
import {ElementResize} from '@/utils/ElementResize.js
// Create a listening instance
var eleResize = new ElementResize('#content');
eleResize.listen(function() {
console.log(' monitor ')
})
monitor ElementResize.js file
/**
* Element width height monitoring
* @param {Object} el Listen for element selectors
*/
function ElementResize(eleSelector) {
if (!(this instanceof ElementResize)) return;
if (!eleSelector) return;
this.eleSelector = eleSelector;
this.el = document.querySelector(eleSelector);
this.queue = [];
this.__init(); //globel init
}
// initialization
ElementResize.prototype.__init = function() {
let iframe = this.crateIElement();
this.el.style.position = 'relative';
this.el.appendChild(iframe)
this.bindEvent(iframe.contentWindow);
}
/**
* Set element style
* @param {HTMLObject} el
* @param {Object} styleJson
*/
ElementResize.prototype.setStyle = function(el, styleJson) {
if (!el) return;
styleJson = styleJson || {
opacity: 0,
'z-index': '-1111',
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '100%',
};
let styleText = '';
for (var key in styleJson) {
styleText += (key + ':' + styleJson[key] + ';');
}
el.style.cssText = styleText;
}
/**
* Create elements
* @param {Object} style
*/
ElementResize.prototype.crateIElement = function(style) {
let iframe = document.createElement('iframe');
this.setStyle(iframe);
return iframe;
}
/**
* The binding event
* @param {Object} el
*/
ElementResize.prototype.bindEvent = function(el) {
if (!el) return;
var _self = this;
el.addEventListener('resize', function() {
_self.runQueue();
}, false)
}
/**
* Run the queue
*/
ElementResize.prototype.runQueue = function() {
let queue = this.queue;
for (var i = 0; i < queue.length; i++) {
(typeof queue[i]) === 'function' && queue[i].apply(this);
}
}
/**
* External monitoring
* @param {Object} cb Callback function
*/
ElementResize.prototype.listen = function(cb) {
if (typeof cb !== 'function') throw new TypeError('cb is not a function!');
this.queue.push(cb);
}
边栏推荐
- 开哪家证券公司的账户是比较好,比较安全的
- Cesium draw point line surface
- 为什么要分布式 id ?分布式 id 生成方案有哪些?
- ThingsPanel 發布物聯網手機客戶端(多圖)
- Use PHP script to view the opened extensions
- WebRTC Native M96 基础Base模块介绍之实用方法的封装(MD5、Base64、时间、随机数)
- SQL server saves binary fields to disk file
- The idea of mass distribution of GIS projects
- JS judge whether a number is in the set
- Openfeign uses
猜你喜欢

Continue to cut the picture after the ArcGIS Server is disconnected

Countdownlatch source code analysis

Recommend a virtual machine software available for M1 computer

How TCP handles exceptions during three handshakes and four waves

Dark horse shopping mall ---8 Microservice gateway and JWT token

Nacos installation and use

Application of analytic hierarchy process in college teaching evaluation system (principle + example + tool)

Use PHP script to view the opened extensions

Windows11 MySQL service is missing

Idea local launch Flink task
随机推荐
Use of JSP sessionscope domain
R语言dplyr包filter函数过滤dataframe数据中指定数据列的内容不是(不等于指定向量中的其中一个)指定列表中的数据行
VFP a picture processing library, simple and easy to use, free of charge, worth recommending
devsecops与devops的理解与建设
The service layer reports an error. The XXX method invalid bound statement (not found) cannot be found
VFP calls the command line image processing program, and adding watermark is also available
Convergence by probability
Pd1.4 to hdmi2.0 adapter cable disassembly.
Caused by: org. xml. sax. SAXParseException; lineNumber: 1; columnNumber: 10; Processing matching '[xx][mm][ll]' is not allowed
JSON format processing
学习笔记 2022 综述 | 自动图机器学习,阐述 AGML 方法、库与方向
Update of complex JSON in MySQL
Is it safe to open an account and buy stocks? Who knows
黑马畅购商城---1.项目介绍-环境搭建
Startups must survive
Kotlin学习笔记
R语言caTools包进行数据划分、scale函数进行数据缩放、e1071包的naiveBayes函数构建朴素贝叶斯模型
Manually rollback abnormal data
动态代理
为什么ping不通网站 但是却可以访问该网站?