当前位置: 首页 > ds >正文

【前端】使用grid布局封装断点式进度条

业务需求要求展示一个动态进度条,不同于第三方插件的进度条,这个ui设计的是断点式进度条,效果当然是美观大方,但是因为没有现成的第三方插件可以实现,这当然难不倒一个成熟的前端开发工程师。
根据ui设计可以看到进度条是由一定数目的小矩形拼成,并且占比情况下的矩形是一个比较亮的颜色,没有占比的颜色比较暗,这个感觉可以用动态背景色来实现,现在要考虑的是那些矩形的颜色是亮色,哪些是暗色,不难发现这就是个数学问题,当索引值小于某个值的时候之间展示活跃色,接下来直接代码实现吧

<template><div class="progress-wrapper"><div class="total" :style="gridStyle"><divclass="item"v-for="i in splitNumber":key="i":style="{background: i <= currentIndex ? activeColor : inactiveColor}"></div></div></div>
</template><script lang="ts" setup>
import { computed } from 'vue'const props = defineProps({// 当前数值(不是百分比)current: {type: Number,default: 50},// 进度条总数total: {type: Number,default: 100},// 分割的小矩形数量splitNumber: {type: Number,default: 40},// 占比颜色activeColor: {type: String,default: '#00fff5'},// 未占比颜色inactiveColor: {type: String,default: '#09545f'},// 小矩形间的间距gap: {type: Number,default: 4}
})const currentIndex = computed(() => {return Math.floor((props.current / props.total) * props.splitNumber)
})// 使用CSS变量避免强制重排
const gridStyle = computed(() => ({'--columns': props.splitNumber,'--gap': `${props.gap}px`
}))
</script><style scoped lang="scss">
.progress-wrapper {width: 100%;background: transparent;height: 10px;contain: layout paint;.total {display: grid;width: 100%;height: 100%;background: transparent;grid-auto-flow: column;grid-template-columns: repeat(var(--columns), minmax(0, 1fr));gap: var(--gap);.item {min-width: 0;min-height: 0;width: 100%;height: 100%;box-sizing: border-box;transition: background-color 0.3s ease; /* 添加过渡效果 */}}
}
</style>

代码实现非常简单,比较有意思的是思考这一实现的过程,现在贴上效果图吧
在这里插入图片描述
如果你也觉得不错的话 留个脚印点个赞+关注吧

http://www.xdnf.cn/news/9966.html

相关文章:

  • Flannel 支持的后端
  • 交集、差集、反选
  • 蓝牙和wifi相关的杂项内容总结
  • Executors面试题
  • apptrace 的优势以及对 App 的价值
  • 【Stable Diffusion 1.5 】在 Unet 中每个 Cross Attention 块中的张量变化过程
  • 磁盘管理无法删除卷,虚拟磁盘管理器:不支持该请求
  • Attention Is All You Need论文阅读笔记
  • Wirtinger Flow算法的matlab实现和python实现
  • 【前端】Twemoji(Twitter Emoji)
  • RV1126-OPENCV Mat理解
  • 某东 h5st第8个参数 指纹加密纯算解析
  • 模型微调之对齐微调KTO
  • MySQL的binlog有有几种录入格式分别有什么区别 ?
  • VSCode的下载与安装(2025亲测有效)
  • LLaMaFactory 微调QwenCoder模型
  • Windows 中禁止在桌面放置文件以保持桌面整洁
  • 深入详解编译与链接:翻译环境和运行环境,翻译环境:预编译+编译+汇编+链接,运行环境
  • does not provide an export named ‘getActiveHead‘
  • 集成均衡功能电池保护芯片在大功率移动电源的应用,创芯微CM1341-DAT、杰华特JW3312、赛微微电CW1244、中颖SH366006
  • 从Homebrew找到openssl.cnf文件并拷贝到Go项目下使用
  • vr中风--数据处理模型搭建与训练
  • NW907NW918美光固态闪存NW920NW930
  • mongodb删除字段
  • springboot拦截器的基本配置
  • lost connection to mysql server at ‘reading initial communication packet‘
  • 怒更一波免费声音克隆和AI配音功能
  • MediaMtx开源项目学习
  • window 显示驱动开发-转换 Direct3D 固定函数状态(二)
  • 《深度搜索-R1-0528》