zoukankan      html  css  js  c++  java
  • Vue resize监听窗口变化

    https://blog.csdn.net/xuaner8786/article/details/81565219

    一、在Vue单个页面运用

    <template>
        <div id="divId">
    
        </div>
    </template>
    <script>
    
        export default {
            data() {
                return {
                    screenWidth: document.documentElement.clientWidth,//屏幕宽度
                    screenHeight: document.documentElement.clientHeight,//屏幕高度
                }
            },
            watch:{
                'screenWidth':function(val){ //监听屏幕宽度变化
                    var oIframe = document.getElementById(divId);
                    oIframe.style.width = (Number(val)-120) + 'px'; //'120'是页面布局调整,可去除
    
                },
                'screenHeight':function(){ //监听屏幕高度变化
                    var oIframe = document.getElementById(divId);
                    oIframe.style.height = (Number(val)-120) + 'px'; //'120'是页面布局调整,可去除
                }
            },
            mounted() {
                var _this = this;
                window.onresize = function(){ // 定义窗口大小变更通知事件
                    _this.screenWidth = document.documentElement.clientWidth; //窗口宽度
                    _this.screenHeight = document.documentElement.clientHeight; //窗口高度
                };
            }
        }
    </script>
    

     

    二、在项目中全局运用

    1、在store.js里定义

    let state = {
        screenWidth:document.documentElement.clientWidth, //屏幕宽度
        screenHeight:document.documentElement.clientHeight, //屏幕高度
    }
    

      2、在main.vue里挂载window.onresize

    mounted() {
      var _this = this;
      window.onresize = function(){ // 定义窗口大小变更通知事件
        _this.$store.state.screenWidth = document.documentElement.clientWidth; //窗口宽度
        _this.$store.state.screenHeight = document.documentElement.clientHeight; //窗口高度
      };
    }
    

      3、在Vue页面中监听

    <template>
        <div id="divId">
    
        </div>
    </template>
    <script>
    
        export default {
            data() {
                return {
                    screenWidth: document.documentElement.clientWidth,//屏幕宽度
                    screenHeight: document.documentElement.clientHeight,//屏幕高度
                }
            },
            watch:{
                '$store.state.screenWidth':function(val){ //监听屏幕宽度变化
                    var oIframe = document.getElementById(divId);
                    oIframe.style.width = (Number(val)-120) + 'px'; //'120'是页面布局调整,可去除
    
                },
                '$store.state.screenHeight':function(){ //监听屏幕高度变化
                    var oIframe = document.getElementById(divId);
                    oIframe.style.height = (Number(val)-120) + 'px'; //'120'是页面布局调整,可去除
                }
            }
        }
    </script>
    

      

    注意事项

    1、在项目中 window.onresize只能挂载一次,在多个页面中同时挂载 window.onresize时,只有其中一个 window.onresize会起作用
    2、避免 window.onresize频繁挂载(待续)

    其他

     watch: {
                screenWidth (val) {
                    if (!this.timer) {
                        this.screenWidth = val
                        this.timer = true
                        let that = this
                        setTimeout(function () {
                            // that.screenWidth = that.$store.state.canvasWidth
                            console.log(that.screenWidth)
                            that.init()
                            that.timer = false
                        }, 400)
                    }
                }
            }
    

      

  • 相关阅读:
    浅谈REST[转]
    linuxyum
    XAMPPAccess denied for user 'root'@'localhost' (using password:YES)....& can't connect to localhost...
    ps aux详细解释【转】
    TOP 10:值得关注的十家云计算公司【转】
    isnull在数据库查询中的应用
    正则匹配代码
    推荐一款生成SQL插入语句的软件
    网页自动刷新
    执行SQL脚本语句判断是否已经存在
  • 原文地址:https://www.cnblogs.com/dhjy123/p/12744579.html
Copyright © 2011-2022 走看看