下程序input框letter-spacing失效,模拟实现

当在小程序中input的letter-spacing属性失效时,本文通过HTML、CSS和JS实现了一个模拟输入框间距效果的解决方案。利用多个view元素来分别显示输入的每个字符,并通过CSS动画实现光标闪烁效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要实现这样的效果,但是input下的letter-spacing不起作用,只能模拟实现。

参考文章https://blog.youkuaiyun.com/luoyumeiluoyumei/article/details/81509694

 

html部分:

<!--此处input要隐藏起来 -->

<input type='number' class='codeInput' placeholder-class="placeholder" maxlength='4' bindinput='inputcatch'></input>

<!--inputCon用来模拟input输入的效果 -->

<view class='inputCon'>

<view class="inputItem">

<view wx:if="{{!numList.length}}" class="cursor"></view>

<text wx:if="{{numList.length}}">{{numList[0]}}</text>

</view>

<view class="inputItem">

<view wx:if="{{numList.length===1}}" class="cursor"></view>

<text wx:if="{{numList.length>=2}}">{{numList[1]}}</text>

</view>

<view class="inputItem">

<view wx:if="{{numList.length===2}}" class="cursor"></view>

<text wx:if="{{numList.length>=3}}">{{numList[2]}}</text>

</view>

<view class="inputItem">

<view wx:if="{{numList.length===3}}" class="cursor"></view>

<text wx:if="{{numList.length>=4}}">{{numList[3]}}</text>

</view>

</view>

</view>

<view class='dashed'>

<text class='dashedItem'></text>

<text class='dashedItem'></text>

<text class='dashedItem'></text>

<text class='dashedItem'></text>

</view>

css部分

.dashed{

display: block;

width: 320rpx;

margin: 0 auto;

margin-top: -30rpx;

}

.dashedItem{

display: inline-block;

border-bottom: 2rpx solid #000;

width: 50rpx;

margin: 0rpx 15rpx;

margin-top: -10rpx;

}

.codeInput{

display: block;

/* background: red; */

width: 100%;

color: #000;

height: 100rpx;

margin-left: -20%;

}

.cursor {

width: 1px;

height:43rpx;

background-color: #000;

animation: focus 0.7s infinite;

}

.getcode.active{

background: #000;

}


 

/* 光标动画 */

 

@keyframes focus {

from {

opacity: 1;

}

 

to {

opacity: 0;

}

}

.inputCon{

width: 320rpx;

margin: 0 auto;

margin-top:-50rpx;

}

.inputItem{

width: 80rpx;

height: 50rpx;

vertical-align: top;

display: inline-block;

text-align: center;

}

.inputItem>text{

font-size: 43rpx;

}

js部分

data: {

numList:[],

confirmstatus:'',

code:''

},

inputcatch:function(e){

var that = this;

var value = e.detail.value;

var valueArray = value.split("");

console.info(valueArray);

that.setData({

numList: valueArray,

code: value

})

if (!/^\d{4}$/.test(e.detail.value)) {

that.setData({

confirmstatus: ''

})

return;

} else {

that.setData({

confirmstatus: 'active'

})

}

},

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值