vue.js - How can I react to whether the browser is in fullscreen mode?
问题描述
I'm creating a web application with Vue.js for which users may want to put the application in fullscreen mode. I want to show a "maximize" icon () in the top-right of the application when the application is not in fullscreen mode, and I want to show a "restore down" icon (︎) when the application is in fullscreen mode. I want to handle the switching of these icons with Vue.js.
I've searched Google for how to do this, even just using vanilla JavaScript, but I'm not finding something that works.
解决方案
I developed a solution using a combination of this answer and this answer.
In your mounted
function, add an event listener:
mounted: function () {
const vm = this
window.addEventListener('resize', () => {
vm.windowInnerHeight = window.innerHeight
})
},
Add the necessary windowInnerHeight
variable in your data
section:
data () {
return {
windowInnerHeight: undefined,
}
},
Add a computed
property:
computed: {
applicationIsInFullscreenMode () {
return this.windowInnerHeight === screen.availHeight
}
},
And then you can use that computed property in your template like this:
{{ applicationIsInFullscreenMode ? '🗗︎' : '' }}
推荐阅读
- docker - docker dynamodb -sharedDb 选项
- java - 替换前 4 个字符的正则表达式
- cassandra - Cassandra - 在不停机的情况下更改 Keyspace
- excel - Filtering two columns: keep all the rows associated to one ID if exists a value in the second column
- python - 在python中动态比较两个csv文件
- node.js - SyntaxError:尝试在 Google Cloud 上运行 ssh 时出现意外令牌
- python - 我想知道它是什么意思 (height,width)=img.shape[:2]
- logging - Kubernetes 后端 Pod 不断崩溃并进入 CrashLoopBackOff
- django - 将 Django 自动增量主键设置为特定值
- .htaccess - .htaccess 重定向 301 奇怪的行为