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

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

相关推荐
我命由我1234523 分钟前
微信小程序 - 自定义实现分页功能
前端·微信小程序·小程序·前端框架·html·html5·js
程序员黄同学1 小时前
请谈谈 Vue 中的 key 属性的重要性,如何确保列表项的唯一标识?
前端·javascript·vue.js
繁依Fanyi1 小时前
巧妙实现右键菜单功能,提升用户操作体验
开发语言·前端·javascript·vue.js·uni-app·harmonyos
前端御书房1 小时前
前端防重复请求终极方案:从Loading地狱到精准拦截的架构升级
前端·javascript
web182854825121 小时前
nginx 部署前端vue项目
前端·vue.js·nginx
zy0101011 小时前
HTML标签
前端·css·html
程序员黄同学1 小时前
解释 Vue 中的虚拟 DOM,如何通过 Diff 算法最小化真实 DOM 更新次数?
开发语言·前端·javascript
蓝谷芮济2 小时前
二:前端发送POST请求,后端获取数据
前端
果粒chenl2 小时前
css+js提问
前端·javascript·css
memorycx2 小时前
Vue02
前端·javascript·vue.js