当前位置:网站首页>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);
}
边栏推荐
- Gradle知识点
- Countdownlatch source code analysis
- Flink deeply understands the graph generation process (source code interpretation)
- 黑马畅购商城---8.微服务网关Gateway和Jwt令牌
- Where do the guests come from
- SQL server saves binary fields to disk file
- Why can't you Ping the website but you can access it?
- 现在网上炒股开户身份证信息安全吗?
- Why distributed IDS? What are the distributed ID generation schemes?
- 什么是Flink?Flink能用来做什么?
猜你喜欢
黑马畅购商城---8.微服务网关Gateway和Jwt令牌
Old ou, a fox friend, has had a headache all day. The VFP format is always wrong when it is converted to JSON format. It is actually caused by disordered code
Application of analytic hierarchy process in college teaching evaluation system (principle + example + tool)
Explain websocket protocol in detail
TCP如何处理三次握手和四次挥手期间的异常
flutter常用命令及问题
Sentinel integrated Nacos data source
ROS 笔记(06)— 话题消息的定义和使用
VFP uses Kodak controls to control the scanner to solve the problem that the volume of exported files is too large
ThingsPanel 发布物联网手机客户端(多图)
随机推荐
一個硬件工程師走過的彎路
Gradle知识点
Solution to the timeout scenario of Flink streaming computing (official live broadcast)
客从何处来
分享7个神仙壁纸网站,让新的壁纸,给自己小小的雀跃,不陷入年年日日的重复。
Customize to prevent repeated submission of annotations (using redis)
Quarkus SaaS dynamic data source switching implementation, simple and perfect
VFP uses Kodak controls to control the scanner to solve the problem that the volume of exported files is too large
Network related encapsulation introduced by webrtc native M96 basic base module
黑馬暢購商城---3.商品管理
Simple use of stream (II)
Tool usage summary
PD1.4转HDMI2.0转接线拆解。
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、epiDisplay包的poisgof函数对拟合的泊松回归模型进行拟合优度检验(检验模型效果)
按钮多次点击造成结果
WebRTC Native M96 基础Base模块介绍之网络相关的封装
Actual combat summary of Youpin e-commerce 3.0 micro Service Mall project
Mui scroll bar recovery
How to open an account for trading futures Shanghai nickel products online
Hook技术