Vue_为window对象添加事件处理程序

resize事件为例,要获取窗口变化时的窗口大小:在created钩子函数中为window对象添加事件处理程序

        var app = new Vue({
            el: '#app',
            data: {
                winWidth: {
                    type: Number
                },
                winHeight: {
                    type: Number
                }
            },
            methods: {
                viewWidth() {
                    return window.innerWidth || document.documentElement.clientWidth;
                },
                viewHeight() {
                    return window.innerHeight || document.documentElement.clientHeight;
                },
                updateWindow() {
                    this.winWidth = this.viewWidth();
                    this.winHeight = this.viewHeight();
                }
            },
            created() {
                this.updateWindow();
                window.onresize = () => {
                    this.updateWindow();
                }
            }
        });
posted @ 2020-12-25 15:12  Syinho  阅读(1306)  评论(0编辑  收藏  举报