Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change vue.js data value when screen size changes?

<div id="app">
    <ul>
        <!-- On mobile devices use short heading -->
        <template v-if="mobile == 1">
            <li><a href="#">Heading</a></li>
        </template>
        <!-- Else use long heading -->
        <template v-else-if="mobile == 0">
            <li><a href="#">Heading Long</a></li>
        </template>
    </ul>
</div>

<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script>
    var app = new Vue({
            el: '#app',
            data: {
                mobile: 0
            }
});

I'm looking for a way to change the value of 'mobile' when the screen breakpoint of (max-width: 547px) becomes active. And to change it back when this mobile breakpoint becomes inactive (screen goes over 547px). I normally use skel (https://github.com/ajlkn/skel) to deal with screen breakpoints, but I cannot access skel from inside Vue, or vice-versa. I would forego using Vue for this particular task, but display: none, and display: block throws off my presentation--turning my element into a block.

like image 665
Will Moore Avatar asked Mar 21 '18 19:03

Will Moore


People also ask

How do I watch data changes on Vue instance?

A Watcher in Vue. js is a special feature that allows one to watch a component and perform specified actions when the value of the component changes. It is a more generic way to observe and react to data changes in the Vue instance. Watchers are the most useful when used to perform asynchronous operations.

How do I get my screen height on Vuetify?

Use fluid to get the full width of viewport, and fill-height to get the full height of the viewport.

Is Vue JS good for large projects?

Being a frontend newcomer compared to other big players in the world of JavaScript such as React or Angular, Vue. js is increasingly used in large-scale projects. The growing maturity of the framework and its considerable ecosystem make Vue capable of meeting even the most demanding project requirements.


2 Answers

If you are using Vuetify, you can programmatically adjust the data value based on the built in breakpoints of xs, sm, md, lg, xl (as specified in Material Design) as follows:

computed: {
  mobile() {
    return this.$vuetify.breakpoint.sm
  },
}

mobile will change to true as soon as the screen width is less than 600px.

Your code would then be something like this (I also moved the if statement to be directly on the <li> element):

<div id="app">
    <ul>
        <!-- On mobile devices use short heading -->
        <li v-if="mobile"><a href="#">Heading</a></li>
        <!-- Else use long heading -->
        <li v-else><a href="#">Heading Long</a></li>
    </ul>
</div>
like image 86
user11809641 Avatar answered Oct 05 '22 08:10

user11809641


You can use onorientationchange event like the following:

methods: {
   detectOrientationChange() {
      switch(window.orientation) {  
         case -90 || 90:
            // landscape
            this.mobile = false;
            break; 
         default:
            // portrait
            this.mobile = true;
            break; 
      }
   }
},
mounted() {
   this.$nextTick(() => {
      window.addEventListener('onorientationchange', this.detectOrientationChange)
   }
},
created() {
   this.detectOrientationChange(); // when instance is created
}

Note: As the event has been deprecated, it can only be used with mobile browsers as of writing this.


To detect screen orientation on current browsers check this post.

like image 30
Bhojendra Rauniyar Avatar answered Oct 05 '22 06:10

Bhojendra Rauniyar