Vue 监听 window.resize
【摘要】
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<ti...
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Vue中绑定全局事件onresize</title>
<meta name="viewport" content="width=device-width"/>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
</head>
<body>
<table id="app">
<tr><td>document.body.clientXXX</td> <td>屏幕可见区域</td> <td>{{ clientSize }}</td></tr>
<tr><td>document.body.offsetXXX</td> <td>屏幕可见区域</td> <td>{{ offsetSize }}</td></tr>
<tr><td>document.body.scrollXXX</td> <td>网页正文全文</td> <td>{{ scrollSize }}</td></tr>
<tr><td>window.innerXXX</td> <td>内边界(视口)</td> <td>{{ innerSize }}</td></tr>
<tr><td>window.outerXXX</td> <td>外边框</td> <td>{{ outerSize }}</td></tr>
<tr><td>window.screen.availXXX</td> <td>屏幕可用工作区</td> <td>{{ availSize }}</td></tr>
<tr><td>window.screen.clientXXX</td> <td>屏幕物理分辨率</td> <td>{{ screenSize }}</td></td></tr>
<tr><td>window.screen.devicePixelRatio</td> <td>屏幕缩放因子</td> <td>{{ devicePixelRatio }}</td></tr>
<tr><td>window.screen.deviceXDPI</td> <td>DPI</td> <td>{{ deviceXDPI }}</td></tr>
</table>
<script>
function getDPI() {
var arrDPI = new Array;
var tip;
if (window.screen.deviceXDPI) {
tip = "support window.screen.deviceXDPI";
arrDPI[0] = window.screen.deviceXDPI;
arrDPI[1] = window.screen.deviceYDPI;
}
else {
tip = "not support window.screen.deviceXDPI";
var tmpNode = document.createElement("DIV");
tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
document.body.appendChild(tmpNode);
arrDPI[0] = parseInt(tmpNode.offsetWidth);
arrDPI[1] = parseInt(tmpNode.offsetHeight);
tmpNode.parentNode.removeChild(tmpNode);
}
return arrDPI + ' (' + tip + ')';
}
new Vue({
el: '#app',
data: {
clientSize: document.body.clientWidth + ' * ' + document.body.clientHeight,
offsetSize: document.body.offsetWidth + ' * ' + document.body.offsetWidth,
scrollSize: document.body.scrollWidth + ' * ' + document.body.scrollHeight,
that.innerSize = window.innerWidth + ' * ' + window.innerHeight;
that.outerSize = window.outerWidth + ' * ' + window.outerHeight;
availSize: window.screen.availWidth + ' * ' + window.screen.availHeight,
screenSize: window.screen.width + ' * ' + window.screen.height,
devicePixelRatio: window.devicePixelRatio + 'X',
deviceXDPI: getDPI(),
},
created() {
const that = this;
window.onresize = ()=> {
that.clientSize = document.body.clientWidth + ' * ' + document.body.clientHeight;
offsetSize = document.body.offsetWidth + ' * ' + document.body.offsetWidth;
scrollSize = document.body.scrollWidth + ' * ' + document.body.scrollHeight;
that.innerSize = window.innerWidth + ' * ' + window.innerHeight;
that.outerSize = window.outerWidth + ' * ' + window.outerHeight;
that.availSize = window.screen.availWidth + ' * ' + window.screen.availHeight;
that.screenSize = window.screen.width + ' * ' + window.screen.height;
that.devicePixelRatio = window.devicePixelRatio + 'X';
that.deviceXDPI = getDPI();
}
}
})
</script>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
Vue 中的 created,也可以用 mounted 来代替。
它们的共同点是仅在Vue初始化时执行一次。
created:在实例创建完成后执行的钩子
mounted:编译好的HTML挂载到页面完成后执行的事件钩子
另外,Vue中可以直接调用JS的方法。但如果要调用 Vue中的 methods 中定义的方法就会绕一点,需要使用 this.$options.methods.MethodName();
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/79852649
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)