近期做一个微信小程序 使用的是uniapp+uview 框架
做到验证码输入框u-code-input这个,发现无法进行粘贴选项,普通的输入框是可以粘贴的。
经过翻找代码最后做了出来
// 首先验证码输入框的父级盒子
<view class="form-view-class" :class="input_val?'code_login_ipt':''">
<u-code-input
v-model="input_val"
maxlength="6"
hairline
color="#333333"
borderColor="#9b9ba5"
space="16"
fontSize="21"
size="40"
mode="line"
:focus="true"
></u-code-input>
</view>
<script>
// js
export default {
data() {
return {
input_val: '', // 验证码的值
};
},
}
</script>
//css
<style lang="scss">
.form-view-class {
padding: 0 40rpx;
box-sizing: border-box;
position: relative;
.u-code-input__input { // 通过查看css得到的验证码输入框组件里的input类名
height: 100%;
width: 670rpx !important;
// 验证码输入框宽度为整屏750rpx-我的父级加的padding
left: 20px !important;
// 组件已经内置的有定位,直接在这里设置left给它原先的冲掉,
// 因为验证码输入框的每个框宽度是40px
// 所以为了让光标重叠,给input一半的left距离,也就是20px,
// 可以自行在开发者工具上看验证码输入框的css
}
.u-code-input {
.u-code-input__item:first-child { // 隐藏第一个框的光标
.u-code-input__item__cursor {
width: 0px !important;
}
}
}
}
.code_login_ipt { //该类名作用为在验证码输入框有值时将验证码输入框的input挪出屏幕。
.u-code-input__input {
height: 100%;
// padding: 0 30rpx !important;
width: 999rpx !important;
left: -249rpx !important;
// z-index: 2 !important;
}
}
</style>
// 按照原先组件自带的css,是把验证码输入框中的input设置为两个屏幕的宽度,然后通过定位将他挪出了屏幕,这样因为选不中input框所以无法使用粘贴功能。
设置的css把input设置成正常宽度,并改变他的定位,这样出现了新的问题就是光标无法隐藏。
上边的代码就是把验证码输入框的第一个框的光标效果隐藏了,然后通过css定位把input的光标位置刚好和第一个框进行重叠,这样既能选中input用他的粘贴,又可以不出现多个光标
最后是将已经有内容的input直接挪出屏幕外就行。