Lorine
Lorine

Reputation: 129

Use a computed switch in CSS to define top property of v-img

I want to define the top property of a v-img depending on the current breakpoint of the window.

I wanted to define it like that:

<v-img contain id="logo-transparent" :top="logoTop" :width="logoWidth" :src="logoTransparent" class="hidden-xs-only"></v-img>

with the computed property looking like that:

logoTop(){
            switch (this.$vuetify.breakpoint.name) {
                case 'xl': return "-4%"
                case 'lg': return "-6%"
                case 'md': return "-8%"
                case 'sm': return "-8%"
                case 'xs': return 0
                default:
                    return "-4%"
            }
        },

and the CSS like this:

#logo-transparent{
  z-index: 1;
  width: 400px;
  height: 300px;
  position: absolute;
  right: -1%;
}

but the problem is that v-img doesn't have the top property.

I wanted to use my computed function to define the CSS of the image like this:

logoTop(){
            return {
                "--top-property" : switch (this.$vuetify.breakpoint.name) {
                                    case 'xl': return 400
                                    case 'lg': return 300
                                    case 'md': return 300
                                    case 'sm': return 200
                                    case 'xs': return 0
                                    default:
                                        return 400
                                }
            }
            
        },

to be able to use it like this in the css:

top : var(--top-property)

but it seems that I can't use a switch in that case.

How could I do it?

Upvotes: 0

Views: 273

Answers (2)

tony19
tony19

Reputation: 138436

Your original logoTop computed property would work to set the v-img's top position in a style binding:

<template>
  <v-img :style="{ top: logoTop }" ... />
</template>

<script>
export default {
  computed: {
    logoTop() {
      switch (this.$vuetify.breakpoint.name) {
        case 'xl': return "-4%"
        case 'lg': return "-6%"
        case 'md': return "-8%"
        case 'sm': return "-8%"
        case 'xs': return 0
        default: return "-4%"
      }
    },
  }
}
</script>

demo

Upvotes: 0

Cedric Cholley
Cedric Cholley

Reputation: 2043

switch doesn't return anything. You should use a variable like so

logoTop() {
    let topProperty;
    switch (this.$vuetify.breakpoint.name) {
        case 'xl':
            topProperty =  400;
            break;
        case 'lg':
        case 'md':
            topProperty =  300;
            break;
        case 'sm':
            topProperty =  200;
            break;
        case 'xs':
            topProperty =  0;
            break;
        default:
            topProperty = 400;
    }
    return {
        "--top-property" : topProperty
    }
},

Upvotes: 1

Related Questions