Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue binding value based on media query

Tags:

css

vue.js

carousel-3d(:display="3", :width="150", :height="150")

I want to set the attribute bindings based on a media query

e.g.

display should become 5 when screen width > 960px

like image 614
Chris Avatar asked Nov 06 '17 18:11

Chris


1 Answers

You could try binding the display value to a component property:

<carousel-3d :display="display">

...and then update that property on window resize:

...

data() {
  return {
    display: 3
  }
},

methods: {
  onResize() {
    if (window.innerWidth > 960) {
      this.display = 5
    } else {
      this.display = 3
    }
  }
},

created() {
  window.addEventListener('resize', this.onResize)
},

beforeDestroy() {
  window.removeEventListener('resize', this.onResize)
},

...
like image 82
Steve Holgado Avatar answered Sep 21 '22 06:09

Steve Holgado