<view v-for="(item, index) in data.cms_head.categories" :key="item.id" :ref="'item'+index"
:class="['pl25', 'pr25','scroll-item', {'newActive': index == newActive}]"
@click="changeNewTab(index)">
<text class="c-666 fs28">{{ item.name }}</text>
<text :class="['tabLine']"></text>
</view>
父元素动态设置newActive类名后,想通过.newActive .tabLine 这样动态控制tabLine样式,是不生效的。
艾思 (作者)
.tabLine {
position: absolute;
width: 0;
height: 6rpx;
background: #0768ac;
bottom: 0;
transition-duration: 0.3s;
transition: width .15s;
}
.newActive .tabLine {
width: 80rpx !important;
}
改成:<text :class="['tabLine', {'tabLine1': index == newActive}]"></text>,这样就可以,而且我发现你们uni组件库里面的组件这样写,也是不行的
2025-08-14 11:17
DCloud_UNI_yuhe
回复 艾思: 你说的哪个uni组件库?你这不是加了个新的类 tabLine1 吗?
2025-08-14 11:20
艾思 (作者)
回复 DCloud_UNI_yuhe: 新加的类 tabLine1,可以修改到样式,但是不加这个,通过父元素控制就不行.newActive .tabLine这样
2025-08-14 11:22
DCloud_UNI_yuhe
回复 艾思: tabLine1的内容是什么?
2025-08-14 11:29
艾思 (作者)
回复 DCloud_UNI_yuhe: width: 80rpx
2025-08-14 11:32
DCloud_UNI_yuhe
回复 艾思: text 元素是行内元素,也不能设置 width 吧,你设置生效了之后效果是什么?
2025-08-14 11:46
艾思 (作者)
回复 DCloud_UNI_yuhe: 可以的宽度有变化,之前是vue2都可以,这次升级了vue3,在做兼容,把宽度成字体也是无效的
2025-08-14 11:53
DCloud_UNI_yuhe
回复 艾思: 这个与vue2无关的,这是css样式本来就不能设置width给text,你之前设置生效是不是改成块元素之类了?
2025-08-14 11:55
艾思 (作者)
回复 DCloud_UNI_yuhe:有加绝对定位 position: absolute;变块级元素;我测试了字体大小,也是没变的,但是类是有加上去的
2025-08-14 13:44
DCloud_UNI_yuhe
回复 艾思: 你提供一下完整的可以运行代码,看一下你是咋写的
2025-08-14 14:22
艾思 (作者)
回复 DCloud_UNI_yuhe: 发你了
2025-08-14 14:49