2024-4-11 关于金额输入框的样式实现

1.需求目标的确认

本次需求方想要实现一个金额输入的输入框样式,如下图。

其中这个icon是输入金额时显示,点击后金额消失,'万'与'元'字是固定位置

看到这样的需求图的第一反应肯定是笑呵呵,哪怕初学编程的人员都能三下五除二的做出来。 于是我们便有一下的实现解决思路。

2.需求实现方式构思

按照交互提供的UI图大致将此模块分成了2部分,上面部分的文案区域,下面部分的输入框区域。

简单实现代码如下。

以下是js代码:

js 复制代码
    <View className={style.singlePart}>
        <View className={styles.upperPart}>
            <View>申请金额</View>
            <View>交易规则</View>
        </View>
        
        <View className={styles.downPart}>
            <Input 
             type='number'
             value={inputVal}
             placeholder={`请输入金额`}
             className={styles.inputStyle}
            />
            <Image src={closeIcon} className={styles.closeIcon}/>
            <Text className={styles.unitTH}>万</Text>
            <Text className={styles.unit}>元</Text>
        </View>
    
    </View>

以下是css代码

css 复制代码
.singlePart{
    background: #fff;
    display: flex;
    flex-direction: column;
}

.upPart{
    display: flex;
    justify-content: space-between;
    padding-bottom: 13px;
}

.downPart{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

.inputStyle{
    font-family: 'PingFangSC-Regular';
    font-size: 14px;
    color: #999999;
    font-weight: 400;
    padding: 18px 0 14px 0px;
}

.closeIcon{
    width: 18px;
    height: 18px;
    right: 8px;
}

.unitTH{
    position: absolute;
    bottom: 14px;
    right: 2px;
    font-family: 'PingFangSC-Regular';
    font-size: 24px;
    color: #e03136;
}

.unit{
    position: absolute;
    bottom: 3px;
    font-family: 'PingFangSC-Regular';
    font-size: 14px;
    color: #e03136;
}

交互里的'万'和'元'我们自然而然的就想到用绝对定位来实现。运行以上代码我们就可以很容易的实现上图的交互。虽然只是静态的实现了,但是没有考虑到交互因素。

('交易规则' 后面需求方要求删去。)

当页面滑动的时候,'万元户'跑没了。

于是优化一下,改成相对定位。

css 复制代码
.unitTH{
    position: relative;
    bottom: 14px;
    right: 2px;
    font-family: 'PingFangSC-Regular';
    font-size: 24px;
    color: #e03136;
}

.unit{
    position: relative;
    bottom: 3px;
    font-family: 'PingFangSC-Regular';
    font-size: 14px;
    color: #e03136;
}

我们成功留住了'万元户'

但是本项目是在移动端设备,考虑到不同机型的屏幕问题,'万元'两个字的相对定位距离值不一定适用到所有机型,显然用相对定位也不是一个很好的实现方法。

3.最终解决方案

最终我们修改样式代码如下:

css 复制代码
.unitTH{
    display: 'block';
    font-family: 'PingFangSC-Regular';
    font-size: 24px;
    color: #f77e82;
    text-align: right;
    font-weight: 400;
}

.unit{
     display: 'block';
    font-family: 'PingFangSC-Regular';
    font-size: 14px;
    color: #e03136;
    text-align: right;
    font-weight: 400;
}

这样的方案便可以让输入框适用所有的移动设备,不用考虑距离设置问题了!

相关推荐
阿奇__38 分钟前
element 跨页选中,回显el-table选中数据
前端·vue.js·elementui
谢尔登40 分钟前
【React】SWR 和 React Query(TanStack Query)
前端·react.js·前端框架
断竿散人40 分钟前
专题一、HTML5基础教程-Viewport属性深入理解:移动端网页的魔法钥匙
前端
3Katrina41 分钟前
理解Promise:让异步编程更优雅
前端·javascript
星之金币42 分钟前
关于我用Cursor优化了一篇文章:30 分钟学会定制属于你的编程语言
前端·javascript
天外来物43 分钟前
实战分享:用CI/CD实现持续部署
前端·nginx·docker
moxiaoran57531 小时前
Spring Cloud Gateway 动态路由实现方案
运维·服务器·前端
市民中心的蟋蟀1 小时前
第十一章 这三个全局状态管理库之间的共性与差异 【上】
前端·javascript·react.js
vvilkim1 小时前
Flutter 常用组件详解:Text、Button、Image、ListView 和 GridView
前端·flutter
vvilkim1 小时前
Flutter 命名路由与参数传递完全指南
前端·flutter