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

vue3修改带小数点的价格数字:小数点的前后数字,要分别显示成不同颜色和大小!已经封装成组件了!

需求:

修改带小数点的价格数字:小数点的前后数字,要分别显示成不同颜色和大小!已经封装成组件了!

效果:

前面大,后面小

代码:

组件:

<!--修改小数点前后数字不同颜色和大小的组件-->
<template><view class="price-container"><text class="current-price"><text class="price-icon"> ¥</text><!-- 显示小数点前的价格部分,应用传入的颜色和字体大小 --><text :style="{ color: integerColor, fontSize: integerFontSize }" class="price-integer">{{ integerPart }}</text><text class="dot">.</text><!-- 显示小数点后的价格部分,应用传入的颜色和字体大小 --><text :style="{ color: decimalColor, fontSize: decimalFontSize }" class="price-decimal">{{ decimalPart }}</text></text></view>
</template><script setup>
// 导入所需的 Vue API
import { computed, defineProps } from 'vue';// 定义组件的 props
const props = defineProps({// 价格值,必填项price: {type: Number,required: true},// 整数部分字体颜色,默认黑色integerColor: {type: String,default: '#000000'},// 整数部分字体大小,默认 24pxintegerFontSize: {type: String,default: '24px'},// 小数部分字体颜色,默认黑色decimalColor: {type: String,default: '#000000'},// 小数部分字体大小,默认 16pxdecimalFontSize: {type: String,default: '16px'}
});// 确保 price 是一个有效的数字
const numericPrice = computed(() => {return Number(props.price) || 0;
});// 计算价格的整数部分
const integerPart = computed(() => {const priceStr = numericPrice.value.toFixed(2); // 保证小数点后有两位return priceStr.split('.')[0]; // 获取小数点前的部分
});// 计算价格的小数部分
const decimalPart = computed(() => {const priceStr = numericPrice.value.toFixed(2); // 保证小数点后有两位return priceStr.split('.')[1]; // 获取小数点后的部分
});</script><style lang="scss" scoped>
/* 定义价格容器的布局 */
.price-container {display: flex;align-items: baseline; /* 对齐基线,使得不同字体大小的文本对齐 */
}/* 定义当前价格的样式 */
.current-price {display: flex;align-items: baseline; /* 对齐基线,使得不同字体大小的文本对齐 */.price-icon{font-size: 24rpx;margin-right: 2rpx;}.dot{color: #333333;}
}/* 前面的整数,默认样式可以在这里定义 */
.price-integer {/* 这里可以设置默认样式 */color: #333333;}//后面的小数点
.price-decimal {/* 这里可以设置默认样式 */color: #333333;
}
</style>

使用组件:

灰色:

     <PriceDisplay:price="item.price"integerColor="#333333"integerFontSize="36rpx"decimalColor="#333333"decimalFontSize="24rpx"class="priceContainerWrapper"/>

彩色:

   <PriceDisplay:price="123.45"integerColor="#FF5733"integerFontSize="30px"decimalColor="#33CFFF"decimalFontSize="18px"/>

http://www.lryc.cn/news/413527.html

相关文章:

  • JVM(Java虚拟机) - JVM内存分配与内存管理
  • Linux中vim的基本介绍和使用
  • 宝塔面板上,安装rabbitmq
  • 【Docker系列】Docker 镜像管理:删除无标签镜像的技巧
  • 数据采集器
  • 小红书0510笔试-编程题
  • 2024年热门开放式耳机评测!悠律、韶音、声阔到底该选谁?
  • 计算机毕业设计选题推荐-智慧物业服务系统-Java/Python项目实战
  • 新手小白学习PCB设计,立创EDA专业版
  • 查物流信息用什么软件
  • (40)温度传感器
  • 【靶场实操】sql-labs通关详解----第二节:前端页面相关(Less-11-Less-17)
  • 样式与特效(2)——新闻列表
  • NICE Seminar(2023-07-16)|演化算法的理论研究到底有什么用?(南京大学钱超教授)
  • 优盘驱动器未格式化?数据恢复全攻略
  • (超全)Kubernetes 的核心组件解析
  • 前端常用的【设计模式】和使用场景
  • QT下载问题:Download from your IP address is not allowed
  • 自建数据库VS云数据库
  • 【大数据开发语言Scala的入门教程】
  • docker部署kkfileview文件在线预览服务
  • 朱锐 | 生命图像中的时间和意识
  • pytorch: cpu,cuda,tensorRt 推理对比学习
  • android 音频播放器,(一)SoundPool音频播放实例
  • AVL解析
  • 用C#和WinForms打造你的专属视频播放器:从多格式支持到全屏播放的完整指南
  • Spring security学习笔记
  • MySQL:基础增删查改
  • Apache DolphinScheduler 1.3.4升级至3.1.2版本过程中的踩坑记录
  • 最后一块石头的重量(超级妙的背包问题)