Reputation: 1
I can't get attribute css of class in Vuejs/nuxtjs in my project.
My class: 'only-small' has attribute: 'display:none' if mediaquery <768.
If this way is impossible, can someone suggest another method to resolve my problem without using window object (window.innerWidth always error if refesh page - window is not defined) and vue-screen plugin
size.vue :
in viewport < 768
<button class="select-btn select-btn--size only-small">
<span class="select-btn__label"></span>
<svg class="icon icon--down">
<use xlink:href="/global.svg#down"></use>
</svg>
</button>
in viewport >= 768
<div class="select size-select small-font only-large size-select--small">
<div class="select__headline"></div>
<div class="select__items no-scrollbar">
<div>
<button class="select__label" ></button>
</div>
</div>
</div>
info.vue
<template>
<div>
<Size></Size>
<AddToCart></AddToCart>
</div>
</template
scss:
@media screen and (min-width: 768px) {
.only-small {
display: none !important;
}
}
When I resize the viewport, no class, element,... is added or removed. Only attribute of css applies.
Upvotes: 0
Views: 368
Reputation: 9
Actually, i didn't understand exactly what you need. But, if you want to show your elements according to visitor mediaquery you can use v-show directive with a computed property.
<button v-show="mediaQuery" class="select-btn select-btn--size only-small">
<span class="select-btn__label"></span>
<svg class="icon icon--down">
<use xlink:href="/global.svg#down"></use>
</svg>
<div v-show="!mediaQuery" class="select size-select small-font only-large size-select--small">
<div class="select__headline"></div>
<div class="select__items no-scrollbar">
<div>
<button class="select__label" ></button>
</div>
</div>
data() {
return {
mediaQuery: true
}
},
computed: {
checkMedia() {
if (window.matchMedia('(min-width:768px')) {
this.mediaQuery = false
}
},
Upvotes: 1