rt-sim-training-client/src/mixin/WindowResizeHandler.js

36 lines
692 B
JavaScript
Raw Normal View History

2019-07-04 10:59:40 +08:00
import * as throttleUtil from '@/utils/throttle';
2019-07-02 16:29:52 +08:00
export default {
data() {
return {
2019-08-29 17:16:33 +08:00
_clientWidth: 0,
_clientHeight: 0
2019-07-04 10:59:40 +08:00
};
2019-07-02 16:29:52 +08:00
},
beforeMount() {
// 调用节流函数限制执行频率
var fn = throttleUtil.createOrUpdate(
this,
'_resizeHandler',
300,
'debounce'
2019-07-04 10:59:40 +08:00
);
window.addEventListener('resize', fn);
2019-07-02 16:29:52 +08:00
},
mounted() {
2019-07-04 10:59:40 +08:00
this._resizeHandler();
2019-07-02 16:29:52 +08:00
},
methods: {
_resizeHandler() {
2019-07-04 10:59:40 +08:00
this._clientWidth = document.documentElement.clientWidth;
this._clientHeight = document.documentElement.clientHeight;
2019-07-02 16:29:52 +08:00
if (this.resizeHandler) {
2019-07-04 10:59:40 +08:00
this.resizeHandler();
2019-07-02 16:29:52 +08:00
}
}
},
beforeDestroy() {
2019-07-04 10:59:40 +08:00
window.removeEventListener('resize', this._resizeHandler);
2019-07-02 16:29:52 +08:00
}
2019-07-04 10:59:40 +08:00
};