江西景德鎮(zhèn)建設廳網(wǎng)站seo行業(yè)崗位
在UniApp中,可以使用CSS偽類選擇器和動態(tài)樣式綁定來實現(xiàn)點擊某個元素時改變其顏色的效果。假設有四個元素分別為A、B、C和D。
首先,為這四個元素添加一個共同的類名,例如"item"。
然后,在頁面的樣式中定義兩種顏色,一種是原始顏色,另一種是點擊后的變色。
<style>.item {background-color: 原始顏色;}.item.active {background-color: 點擊后的變色;}
</style>
接下來,在Vue組件中,使用v-for指令遍歷渲染這四個元素,并為每個元素添加一個點擊事件
<template><div><div v-for="(item, index) in itemList":key="index":class="{ 'item': true, 'active': activeIndex === index }"@click="changeColor(index)">{{ item }}</div></div>
</template><script>
export default {data() {return {itemList: ['A', 'B', 'C', 'D'],activeIndex: null};},methods: {changeColor(index) {this.activeIndex = index;}}
};
</script>