使用 CSS 中的计算开关来定义 v-img 的顶部 属性
Use a computed switch in CSS to define top property of v-img
我想根据 window 的当前断点定义 v-img 的顶部 属性。
我想这样定义它:
<v-img contain id="logo-transparent" :top="logoTop" :width="logoWidth" :src="logoTransparent" class="hidden-xs-only"></v-img>
计算出的 属性 看起来像这样:
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%"
}
},
和 CSS 这样的:
#logo-transparent{
z-index: 1;
width: 400px;
height: 300px;
position: absolute;
right: -1%;
}
但问题是v-img没有top 属性.
我想使用我的计算函数来定义图像的 CSS,如下所示:
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
}
}
},
能够在css中像这样使用它:
top : var(--top-property)
但在那种情况下我似乎不能使用开关。
我该怎么做?
switch
没有 return 任何东西。你应该使用像这样的变量
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
}
},
您最初的 logoTop
计算 属性 可以将 v-img
的 top
位置设置为 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>
我想根据 window 的当前断点定义 v-img 的顶部 属性。
我想这样定义它:
<v-img contain id="logo-transparent" :top="logoTop" :width="logoWidth" :src="logoTransparent" class="hidden-xs-only"></v-img>
计算出的 属性 看起来像这样:
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%"
}
},
和 CSS 这样的:
#logo-transparent{
z-index: 1;
width: 400px;
height: 300px;
position: absolute;
right: -1%;
}
但问题是v-img没有top 属性.
我想使用我的计算函数来定义图像的 CSS,如下所示:
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
}
}
},
能够在css中像这样使用它:
top : var(--top-property)
但在那种情况下我似乎不能使用开关。
我该怎么做?
switch
没有 return 任何东西。你应该使用像这样的变量
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
}
},
您最初的 logoTop
计算 属性 可以将 v-img
的 top
位置设置为 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>