vue监听浏览器窗口大小变化

时间:2021-10-29 06:03:07

  首先,页面初始化mounted的时候,通过 document.body.clientWidth 和 document.body.clientHeight 来获取到浏览器的宽和高,然后通过 window.onresize 来监听浏览器窗口的变化,在这里来改变我们的变量宽和高即可。

(created()的时候不行,因为此时document还没有生成)

<template>
<section class="p-10">
<h1> {{ screenWidth }} × {{ screenHeight }} </h1>
</section>
</template>
<script>
export default {
data() {
return {
screenWidth: '',
screenHeight: ''
};
},
mounted() {
this.screenWidth = document.body.clientWidth;
this.screenHeight = document.body.clientHeight;
window.onresize = () => {
return (() => {
this.screenWidth = document.body.clientWidth;
this.screenHeight = document.body.clientHeight;
})();
};
}
}
</script> <style lang="scss">
</style>

嗯。就酱~~

vue监听浏览器窗口大小变化的更多相关文章

  1. vue 监听页面宽度变化 和 键盘事件

    vue 监听页面窗口大小 export default { name: 'Full', components: { Header, Siderbar }, data () { return { scr ...

  2. vue监听页面大小变化重新刷新布局

    在项目中由于某些div元素在布局的时候需要初始化宽高,因为当浏览器缩小屏幕的时候需要重新刷新页面视图. 分析思路: 1.在store中创建state,用于保存当前浏览器的宽.高值. 2.在mounte ...

  3. Vue监听属性的变化

    监听属性的变化watch: { counter: function (nval, oval) { alert('计数器值的变化 :' + oval + ' 变为 ' + nval + '!') }}

  4. vue监听浏览器窗口的变化,随着窗口变化调整里面table的宽高

    1.在data中设置: tableHeight:"500", screenHeight:window.innerHeight, 2.在mounted中设置: mounted() { ...

  5. vue监听路由的变化,跳转到同一个页面时,Url改变但视图未重新加载问题

    引入:https://q.cnblogs.com/q/88214/ 解决方法: 添加路由监听,路由改变时执行监听方法 methods:{ fetchData(){ console.log('路由发送变 ...

  6. vue 监听state 任意值变化、监听mutations actions

    // store.watch((state) => state.count + 1, (newCount) => { // console.log(' 监听') // }) // stor ...

  7. js实现监听浏览器窗口大小改变事件

    window.onresize = function(){   }

  8. vue同一个路由&comma;但参数发生变化,页面不刷新的问题(vue监听路由参数变化重新渲染页面)

    watch: { $route: function(newVal, oldVal) { console.log(oldVal); //oldVa 上一次url console.log(newVal); ...

  9. jquery如何监听浏览器窗口大小并根据不同的大小输出不同的值

    $(window).bind("load resize",function(){ document.documentElement.clientWidth >= 600 ? ...

随机推荐

  1. vs win32 &amp&semi; MFC 指针默认位置

    一开始win32指针所在的位置是与debug文件夹同级的.即打开打开改程序的第一个文件夹这一级. MFC指针是在第二个debug下头,就是打开第二个project名词的文件夹下头,e.g., &quo ...

  2. spring dataSourceRouter自动切换数据源

    spring多数据源的切换,主要用到的是AbstractRoutingDataSource这个路由类,当我们的自定义的一个路由分发类继承AbstractRoutingDataSource类后,重写de ...

  3. sqlserver 自定义字符串分割函数&period;

    --SQL Server Split函数 --Author: sq --说明:被分割后的字段为:short_str --支持分割符多字节 --使用方法 --Select * FROM splits(' ...

  4. logic&colon;present 和 logic&colon;empty的用法 &lpar;转&rpar;

    logic:empty和logic:notEmpty logic:empty标签判断脚本变量是否为null,是否是一个空的字符串(长度为0),是否是一个空的collection或map(调用isEmp ...

  5. CSS的优先级

    样式的优先级: (内联样式表[嵌入式样式])>(内部样式表)>(外部样式表) 经过测试动手测试发现有个(唯一的)例外 情况:当引用外部样式在内部样式表(非嵌入式样式)的后面时,外部样式会覆 ...

  6. sprintboot 中占位符及多环境配置

    (原) 关于springboot中多环境配置问题 1.在application.properties文件中通过 spring.profiles.active=... 选择系统所要加载的配置文件,这里的 ...

  7. leetcode每日刷题计划-简单篇day13

    Num 169 先码,回头再说,摩尔算法... tle了 class Solution { public: int majorityElement(vector<int>& num ...

  8. python3编写网络爬虫13-Ajax数据爬取

    一.Ajax数据爬取 1. 简介:Ajax 全称Asynchronous JavaScript and XML 异步的Javascript和XML. 它不是一门编程语言,而是利用JavaScript在 ...

  9. pta7-7旅游规划(dijkstra算法)

    题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101314114762387456 题意:给n给城市,m条公路,公 ...

  10. java结合使用Jsonp的例子

    更多:js跨域问题解释 解决方案值使用jsonp或jQuery Jsonp和java操作例子 介绍JSONP之前,先简单的介绍一些JSON.JSON是JavaScript Object Notatio ...