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;
}

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

相关推荐
brzhang3 小时前
我操,终于有人把 AI 大佬们 PUA 程序员的套路给讲明白了!
前端·后端·架构
止观止4 小时前
React虚拟DOM的进化之路
前端·react.js·前端框架·reactjs·react
goms4 小时前
前端项目集成lint-staged
前端·vue·lint-staged
谢尔登4 小时前
【React Natve】NetworkError 和 TouchableOpacity 组件
前端·react.js·前端框架
Lin Hsüeh-ch'in4 小时前
如何彻底禁用 Chrome 自动更新
前端·chrome
augenstern4166 小时前
HTML面试题
前端·html
张可6 小时前
一个KMP/CMP项目的组织结构和集成方式
android·前端·kotlin
G等你下课6 小时前
React 路由懒加载入门:提升首屏性能的第一步
前端·react.js·前端框架
蓝婷儿7 小时前
每天一个前端小知识 Day 27 - WebGL / WebGPU 数据可视化引擎设计与实践
前端·信息可视化·webgl
然我7 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法