我在尝试实现Vuejs中项的样式条件语句时遇到了一些奇怪的行为。
我已经看到了一些关于如何通过插值字符串或计算样式对象来实现三元操作符的StackOverflow帖子。但是我两种方法都尝试过,但都没有正常工作。
考虑到这个div:
<div
:class="{'radar__container':true,'inactive':inactive}"
:style= "[inactive ? {getStyleRadarContainerInactive} : {getStyleRadarContainer}]"
>
我将采用这种样式:
computed: {
getStyleRadarContainer: function(){
let styleRadarContainer = {
left: this.radarItem.posX*100 + '%',
top: this.radarItem.posY*100 + '%',
transform: 'translate(-50%,-50%) scale(' + this.radarItem.scale + ')',
opacity: this.radarItem.opacity,
}
return styleRadarContainer;
},
getStyleRadarContainerInactive: function(){
let styleRadarContainerInactive= {
left: this.radarItem.posX*100 + '%',
top: this.radarItem.posY*100 + '%',
transform: 'translate(-50%,-50%) scale(0)',
opacity: this.radarItem.opacity,
}
return styleRadarContainerInactive;
},
}
这应该使得每个项目都缩小(因为opacity属性中有scale(0)),但实际上style属性根本没有被渲染。我还尝试在style属性上使用行内三元运算符(因为scale是两种属性中唯一变化的东西:
transform: 'translate(-50%,-50%) ' + inactive ? 'scale(' + radarItem.scale + ')' : 'scale(0)',
我错过了什么?