Nativescript Vue:有没有办法在非按钮元素上使用高亮显示的或活动的伪选择器?
我有一排由Nativescript Vue:有没有办法在非按钮元素上使用高亮显示的或活动的伪选择器?,nativescript,nativescript-vue,Nativescript,Nativescript Vue,我有一排由StackLayout制作的盒子,我希望在手指按盒子时改变背景色。我尝试在CSS中使用:突出显示的,但它似乎只适用于按钮元素 编辑: 实际上我有这个,但它适用于我所有的盒子,因为它在v-for中: <StackLayout v-for="item in items" orientation="horizontal"> <StackLayout v-bind:class="{ 'color': bgColor }" @touch="hoverOver($e
StackLayout
制作的盒子,我希望在手指按盒子时改变背景色。我尝试在CSS中使用:突出显示的,但它似乎只适用于按钮元素
编辑:
实际上我有这个,但它适用于我所有的盒子,因为它在v-for中:
<StackLayout v-for="item in items" orientation="horizontal">
<StackLayout v-bind:class="{ 'color': bgColor }"
@touch="hoverOver($event)">
</StackLayout>
</StackLayout>
////
hoverOver: function(e) {
if (e.action == "down") {
this.bgColor = true;
} else if (e.action == "up") {
this.bgColor = false;
}
}
////
悬停:函数(e){
如果(如动作==“向下”){
this.bgColor=true;
}否则,如果(如行动==“向上”){
this.bgColor=false;
}
}
一个简单的方法是在每个项目中都有一个标志
示例
<template>
<Page class="page">
<ActionBar title="Home" class="action-bar" />
<ScrollView>
<StackLayout orientation="horizontal">
<Label v-for="item in items" :text="item.title" :class="[item.selected ? 'selected' : '', 'h1 p-15']"
@touch="hoverOver($event, item)">
</Label>
</StackLayout>
</ScrollView>
</Page>
</template>
<script>
export default {
methods: {
hoverOver: function(e, item) {
item.selected = e.action !== "up" && e.action !==
"cancel";
}
},
data() {
return {
items: [{
title: "A",
selected: false
},
{
title: "B",
selected: false
},
{
title: "C",
selected: false
},
{
title: "D",
selected: false
}
]
};
}
};
</script>
<style scoped>
.selected {
color: red;
}
</style>
导出默认值{
方法:{
悬停:功能(e,项目){
item.selected=e.action!=“向上”和&e.action==
“取消”;
}
},
数据(){
返回{
项目:[{
标题:“A”,
所选:false
},
{
标题:“B”,
所选:false
},
{
标题:“C”,
所选:false
},
{
标题:“D”,
所选:false
}
]
};
}
};
.选定{
颜色:红色;
}
您可以在触摸时使用背景色应用一个类,然后在触摸端将其删除。您好@Manoj--我一开始确实尝试过,但它将颜色应用到我的所有框中,因为它处于v-for循环中。我在上面做了一个编辑来展示我的尝试。那么它一定是一个代码错误,你能发布它吗。你应该在每个项目上设置bgColor属性,而不是将color类绑定到item.bgColor。这是你的逻辑缺陷。每个元素都有一个变量绑定,当您更改它时,所有元素都会受到影响。有趣!感谢您在这个社区的持续帮助@Manoj