• 【uniapp】短信验证码输入框


    需求是短信验证码需要格子输入框 如图

    在这里插入图片描述

    网上找了一个案例改吧改吧 直接上代码

    结构

    在这里插入图片描述

    
    <template>
    	<view class="verify-code">
    		<!-- 输入框 -->
    		<input id="input" :value="code" class="input" :focus="isFocus" :type="inputType" :maxlength="itemSize"
    			@input="onInput" @focus="inputFocus" @blur="inputBlur" />
    
    		<!-- 光标 -->
    		<view id="cursor" v-if="cursorVisible && type !== 'middle'" class="cursor"
    			:style="{ left: codeCursorLeft[code.length] + 'px', height: cursorHeight + 'px', backgroundColor: cursorColor }">
    		</view>
    
    		<!-- 输入框 --->
    		<view id="input-ground" class="input-ground">
    			<view v-for="(item, index) in itemSize" :key="index"
    				:style="{ borderColor: code.length === index && cursorVisible ? boxActiveColor : boxNormalColor }"
    				:class="['box', `box-${type + ''}`, `box::after`]">
    				<view :style="{ borderColor: boxActiveColor }" class="middle-line"
    					v-if="type === 'middle' && !code[index]"></view>
    
    				<text class="code-text">{{ code[index] | codeFormat(isPassword) }}</text>
    			</view>
    		</view>
    	</view>
    </template>
    
    <script>
    	/**
    	 * @description 输入验证码组件
    	 * @property {string} type = [box|middle|bottom] - 显示类型 默认:box -eg:bottom
    	 * @property {string} inputType = [text|number] - 输入框类型 默认:number -eg:number
    	 * @property {number} size - 验证码输入框数量 默认:6 -eg:6
    	 * @property {boolean} isFocus - 是否立即聚焦 默认:true
    	 * @property {boolean} isPassword - 是否以密码形式显示 默认false -eg: false
    	 * @property {string} cursorColor - 光标颜色 默认:#cccccc
    	 * @property {string} boxNormalColor - 光标未聚焦到的框的颜色 默认:#cccccc
    	 * @property {string} boxActiveColor - 光标聚焦到的框的颜色 默认:#000000
    	 * @event {Function(data)} confirm - 输入完成回调函数
    	 */
    	import {
    		getElementRect
    	} from './util.js';
    	export default {
    		name: 'verify-code',
    		props: {
    			value: {
    				type: String,
    				default: () => ''
    			},
    			type: {
    				type: String,
    				default: () => 'box'
    			},
    			inputType: {
    				type: String,
    				default: () => 'number'
    			},
    			size: {
    				type: Number,
    				default: () => 6
    			},
    			isFocus: {
    				type: Boolean,
    				default: () => true
    			},
    			isPassword: {
    				type: Boolean,
    				default: () => false
    			},
    			cursorColor: {
    				type: String,
    				default: () => '#cccccc'
    			},
    			boxNormalColor: {
    				type: String,
    				default: () => '#cccccc'
    			},
    			boxActiveColor: {
    				type: String,
    				default: () => '#000000'
    			}
    		},
    		model: {
    			prop: 'value',
    			event: 'input'
    		},
    		data() {
    			return {
    				cursorVisible: false,
    				cursorHeight: 35,
    				code: '', // 输入的验证码
    				codeCursorLeft: [], // 向左移动的距离数组,
    				itemSize: 6,
    				getElement: getElementRect(this),
    				isPatch: false
    			};
    		},
    		created() {
    			this.cursorVisible = this.isFocus;
    			this.validatorSize();
    		},
    		mounted() {
    			this.init();
    		},
    		methods: {
    			/**
    			 * 设置验证码框数量
    			 */
    			validatorSize() {
    				if (this.size > 0) {
    					this.itemSize = Math.floor(this.size);
    				} else {
    					throw "methods of 'size' is integer";
    				}
    			},
    			/**
    			 * @description 初始化
    			 */
    			init() {
    				this.getCodeCursorLeft();
    				this.setCursorHeight();
    			},
    			/**
    			 * @description 计算光标的高度
    			 */
    			setCursorHeight() {
    				this.getElement('.box', 'single', boxElm => {
    					this.cursorHeight = boxElm.height * 0.6;
    				});
    			},
    			/**
    			 * @description 获取光标在每一个box的left位置
    			 */
    			getCodeCursorLeft() {
    				// 获取父级框的位置信息
    				this.getElement('#input-ground', 'single', parentElm => {
    					const parentLeft = parentElm.left;
    					// 获取各个box信息
    					this.getElement('.box', 'array', elms => {
    						this.codeCursorLeft = [];
    						elms.forEach(elm => {
    							this.codeCursorLeft.push(elm.left - parentLeft + elm.width / 2);
    						});
    					});
    				});
    			},
    
    			// 输入框输入变化的回调
    			onInput(e) {
    				let {
    					value,
    					keyCode
    				} = e.detail;
    				this.cursorVisible = value.length < this.itemSize;
    				this.code = value;
    				this.$emit('input', value);
    				this.inputSuccess(value);
    			},
    
    			// 输入完成回调
    			inputSuccess(value) {
    				if (value.length === this.itemSize && !this.isPatch) {
    					this.$emit('confirm', value);
    				} else {
    					this.isPatch = false;
    				}
    			},
    			// 输入聚焦
    			inputFocus() {
    				this.cursorVisible = this.code.length < this.itemSize;
    			},
    			// 输入失去焦点
    			inputBlur() {
    				this.cursorVisible = false;
    			}
    		},
    		watch: {
    			value(val) {
    				if (val !== this.code) {
    					this.code = val;
    				}
    			}
    		},
    		filters: {
    			codeFormat(val, isPassword) {
    				return val ? (isPassword ? '*' : val) : '';
    			}
    		}
    	};
    </script>
    <style scoped>
    	.verify-code {
    		position: relative;
    		width: 100%;
    		box-sizing: border-box;
    	}
    
    	.verify-code .input {
    		height: 100%;
    		width: 200%;
    		position: absolute;
    		left: -100%;
    		z-index: 1;
    		color: transparent;
    		caret-color: transparent;
    		background-color: rgba(0, 0, 0, 0);
    	}
    
    	.verify-code .cursor {
    		position: absolute;
    		top: 50%;
    		transform: translateY(-50%);
    		display: inline-block;
    		width: 2px;
    		animation-name: cursor;
    		animation-duration: 0.8s;
    		animation-iteration-count: infinite;
    	}
    
    	.verify-code .input-ground {
    		display: flex;
    		justify-content: space-between;
    		align-items: center;
    		width: 100%;
    		box-sizing: border-box;
    	}
    
    	.verify-code .input-ground .box {
    		position: relative;
    		display: inline-block;
    		width: 100rpx;
    		height: 140rpx;
    	}
    
    	.verify-code .input-ground .box-bottom {
    		border-bottom-width: 2px;
    		border-bottom-style: solid;
    	}
    
    	.verify-code .input-ground .box-box {
    		border-width: 2px;
    		border-style: solid;
    	}
    
    	.verify-code .input-ground .box-middle {
    		border: none;
    	}
    
    	.input-ground .box .middle-line {
    		position: absolute;
    		top: 50%;
    		left: 50%;
    		width: 50%;
    		transform: translate(-50%, -50%);
    		border-bottom-width: 2px;
    		border-bottom-style: solid;
    	}
    
    	.input-ground .box .code-text {
    		position: absolute;
    		top: 50%;
    		left: 50%;
    		font-size: 80rpx;
    		transform: translate(-50%, -50%);
    	}
    
    	@keyframes cursor {
    		0% {
    			opacity: 1;
    		}
    
    		100% {
    			opacity: 0;
    		}
    	}
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276

    util.js

    /**
     * @description 获取元素节点 - 大小等信息
     * @param {string} elm - 节点的id、class 相当于 document.querySelect的参数 -eg: #id
     * @param {string} type = [single|array] - 单个元素获取多个元素 默认是单个元素
     * @param {Function} callback - 回调函数
     * @param {object} that - 上下文环境 vue2:this ,vue3: getCurrentInstance();
     */
    export const getElementRect = (that) => (elm, type = 'single', callback) => {
    	// #ifndef H5
    	uni
    		.createSelectorQuery()
    		.in(that)[type === 'array' ? 'selectAll' : 'select'](elm)
    		.boundingClientRect()
    		.exec(data => {
    			callback(data[0]);
    		});
    	// #endif
    
    	// #ifdef H5
    	let elmArr = [];
    	const result = [];
    	if (type === 'array') {
    		elmArr = document.querySelectorAll(elm);
    	} else {
    		elmArr.push(document.querySelector(elm));
    	}
    
    	for (let elm of elmArr) {
    		result.push(elm.getBoundingClientRect());
    	}
    	console.log('result', result)
    	callback(type === 'array' ? result : result[0]);
    	// #endif
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    作为一名软件测试开发岗的面试官,我都是怎么选人的?
    Quartus 实例应用(2)——创建设计工程
    2022年高薪测试必备核心技术
    linux安装CUDA、cuDNN以及ytorch-gpu
    Biome-BGC生态系统模型与Python融合技术实践应用
    SpringCloud微服务集成Dubbo
    supervisord: ImportError: No module named web
    Docker18:Docker- compose容器编排
    【Linux通信】Linux中IPC(跨进程通信)、RPC(远程过程调用)、LPC(本地过程调用)的区别
    界面组件DevExpress WPF v23.1 - 进一步升级数据处理能力
  • 原文地址:https://blog.csdn.net/c347087870/article/details/134062632