You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Vuetify框架v-timeline组件的中心线设置分段不同颜色

Vuetify v-timeline 分段修改中心线颜色实现方案

该需求完全可以实现,核心思路是将默认的单色中心线背景替换为线性渐变背景,根据目标节点的位置调整渐变的色标分割点即可。

方案1:静态节点场景

如果你的时间线节点数量、位置固定不会动态变化,直接修改CSS即可:

/* 示例:前60%为红色,剩余部分为灰色,可根据目标节点实际位置调整百分比数值 */
.theme--light.v-timeline:before {
    background: linear-gradient(to bottom, red 60%, #ccc 60%);
}

方案2:动态节点场景

如果节点会动态增减、位置不固定,可以结合JS动态计算分割位置:

  1. 给v-timeline和目标节点分别设置ref:
<v-timeline ref="myTimeline">
  <v-timeline-item>节点1</v-timeline-item>
  <v-timeline-item>节点2</v-timeline-item>
  <!-- 目标分割节点 -->
  <v-timeline-item ref="splitNode">目标节点</v-timeline-item>
  <v-timeline-item>节点4</v-timeline-item>
</v-timeline>
  1. 在生命周期中动态计算分割比例,注入CSS变量:
mounted() {
  const timeline = this.$refs.myTimeline
  const splitNode = this.$refs.splitNode.$el
  const splitPercent = `${(splitNode.offsetTop / timeline.clientHeight) * 100}%`
  timeline.style.setProperty('--timeline-split', splitPercent)
}
  1. CSS中使用变量绑定渐变分割点:
.theme--light.v-timeline:before {
    background: linear-gradient(to bottom, red var(--timeline-split), #ccc var(--timeline-split));
}

版本适配说明

如果你使用的是Vuetify 3,中心线对应的选择器替换为.v-timeline__divider即可,其他逻辑保持一致。

内容的提问来源于stack exchange,提问作者Nim173

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 18:51:00