vant3.x 进度条上增加跟随提示元素

image.png

f12研究看到数字百分比是0的时候 元素就在最左边,
100的时候元素就在最右边,50的时候就在进度刻度线的中间,
还以为实现很复杂,甚至看了下源码是怎么计算百分比位置的控制
其实很简单
left 与 transform x轴的值与百分比保持一致就行了。
根本原因是没有搞懂 transform 的用法

style="left: 60%;transform: translate(-60%, 0%);"

完整代码

¥0

less

.progress-statistics-box {
  width: 278px;
  padding-left: 2px;
  margin-top: 6px;
  position: relative;
  .progress-box {
    width: 278px;
  }
  .tooltip {
    width: fit-content;
    height: 17px;
    background: #febf3f;
    border-radius: 4px 4px 4px 4px;
    box-sizing: border-box;
    padding: 0 4px;
    margin-bottom: 20px;
    font-size: 12px;
    font-family: DIN-Medium, DIN;
    font-weight: 500;
    color: #ffffff;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-bottom: 10px;
  }
  .tooltip:after {
    content: "";
    position: absolute;
    bottom: -3px;
    left: 0%;
    right: 0;
    margin: auto;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 3px 4px 0 4px;
    border-color: #febf3f transparent transparent transparent;
  }
}

https://vant-contrib.gitee.io/vant/#/zh-CN/progress
https://gitee.com/vant-contrib/vant/blob/dev/packages/vant/src/progress/Progress.tsx

手写进度条版

三角形箭头始终与刻度线对齐


image.png
{{progressNum}}
.progress_box_after {
  width: 1%;
  min-width: 0%;
  max-width: 100%;
  height: 10px;
  background: #1677FF;
  border-radius: 10px;
  display: block;
  position: relative;
  top: 0;
}

.progress_box_num {
  width: 50px;
  /* 自适应宽度 */
  /* width: fit-content; */
  box-sizing: border-box;
  padding: 0 10px;
  height: 30px;
  border-radius: 5px;
  background: #1677FF;
  text-align: center;
  font-size: 15px;
  color: #FFFFFF;
  line-height: 30px;
  position: absolute;
  right: -25px;
  /* 自适应宽度 屏蔽上方 right */
  /* right: 0;
  transform: translate(50%, 0%); */
  top: -40px;
}

.progress_box_num::before {
  content: '';
  width: 0;
  height: 0;
  border: 10px solid;
  position: absolute;
  top: 30px;
  left: 0%;
  right: 0;
  margin: 0 auto 0;
  border-color: #1677FF transparent transparent;
}

你可能感兴趣的:(vant3.x 进度条上增加跟随提示元素)