Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use window size in Vue? (How do I detect the soft keyboard?)

Tags:

vue.js

In my mobile web app with Vue, I want to hide my footer when the soft keyboard pops. So I have a little function to test the ratio of window height to window width...

showFooter(){     return h / w > 1.2 || h > 560; } 

...and I declare window.innerHeight/window.innerWidth in my data.

    data: { h: window.innerHeight, w: window.innerWidth } 

Trouble is, when window.innerHeight changes, my h property doesn't get the new value. How can I watch window.innerHeight ?

like image 776
bbsimonbb Avatar asked Nov 10 '17 09:11

bbsimonbb


People also ask

How do you measure window width?

Use window. innerWidth and window. innerHeight to get the current screen size of a page.

What is VUE window?

Vue Window UI Component The Window component for Vue is a floating window which has two main parts - title and content. In the title you can have any html content and close button. In the content you can put any html content.

What is VUE use?

Vue. js is a progressive framework for JavaScript used to build web interfaces and one-page applications. Not just for web interfaces, Vue. js is also used both for desktop and mobile app development with Electron framework.


2 Answers

I am sure there are better ways to do this, but this one will work for you until I come up with something:

Basically you will need just one data prop and one watcher to do this.

new Vue({     el: '#app',     data() {         return {             windowHeight: window.innerHeight,             txt: ''         }     },      watch: {         windowHeight(newHeight, oldHeight) {             this.txt = `it changed to ${newHeight} from ${oldHeight}`;         }     },      mounted() {         this.$nextTick(() => {             window.addEventListener('resize', this.onResize);         })     },      beforeDestroy() {          window.removeEventListener('resize', this.onResize);      },      methods: {           onResize() {             this.windowHeight = window.innerHeight         }     } }); 

And this would output the changes

<div id="app">     <br> Window height: {{ windowHeight }} <br/>     {{ txt }} </div> 
like image 59
samayo Avatar answered Oct 06 '22 23:10

samayo


VUE 3

In Vue 3, you can create a function that can return a reactive width and breakpoint name values. You can easily reuse the function across multiple components.

import { computed, onMounted, onUnmounted, ref } from "vue"  export default function () {   let windowWidth = ref(window.innerWidth)    const onWidthChange = () => windowWidth.value = window.innerWidth   onMounted(() => window.addEventListener('resize', onWidthChange))   onUnmounted(() => window.removeEventListener('resize', onWidthChange))      const type = computed(() => {     if (windowWidth.value < 550) return 'xs'     if (windowWidth.value >= 550 && windowWidth.value < 1200) return 'md'     if (windowWidth.value >= 1200) return 'lg'     return null; // This is an unreachable line, simply to keep eslint happy.   })    const width = computed(() => windowWidth.value)    return { width, type } } 

You can use this in the setup method of your vue 3 component as follows.

const { width, type } = useBreakpoints() 

Quick tip: Even though document event listeners are the most optimized things in the planet, this is best used only once in an app for performance reasons. Make a tiny plugin and add the value to the Vue instance, just like Vuetify does. Or to be simpler, commit them to vuex and read from there.

like image 25
kshksdrt Avatar answered Oct 07 '22 00:10

kshksdrt