当前位置: 首页 > 编程笔记 >

基于vue的验证码组件的示例代码

洪知
2023-03-14
本文向大家介绍基于vue的验证码组件的示例代码,包括了基于vue的验证码组件的示例代码的使用技巧和注意事项,需要的朋友参考一下

最近在自己写页面,模仿思否论坛,然后写登录注册UI的时候需要一个验证码组件. 去搜一下没找到什么合适的,而且大多都是基于后端的,于是自己手写一个。

演示

分析验证码组件

分析验证码功能

  • 随机出现的数字大小写字母 (基础功能)
  • 不同的数字或者字母有不同的颜色 (功能优化)
  • 不同的数字或者字母有不同的字体大写 (功能优化)
  • 不同的数字或者字母有不同的边距 (功能优化)
  • 不同的数字或者字母有不同的倾斜角度 (功能优化)
  • 更多功能优化...

分析组件功能

  • 可以设置生成验证码的长度 (基本功能)
  • 可以设置验证码组件的宽高 (基本功能)

编写验证码组件

template

最外层div绑定点击事件,点击后刷新验证码。
span是单个验证码的载体,样式动态绑定

<template>
 <div class="ValidCode disabled-select" :style="`width:${width}; height:${height}`" @click="refreshCode">
  <span v-for="(item, index) in codeList" :key="index" :style="getStyle(item)">{{item.code}}</span>
 </div>
</template>

methods

refreshCode 刷新验证码的方法
createdCode 生成验证码的方法
getStyle 为每个元素生成动态的样式

 methods: {
  refreshCode () {
   this.createdCode()
  },
  createdCode () {
   let len = this.length,
    codeList = [],
    chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789',
    charsLen = chars.length
   // 生成
   for (let i = 0; i < len; i++) {
    let rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
    codeList.push({
     code: chars.charAt(Math.floor(Math.random() * charsLen)), // 随机码
     color: `rgb(${rgb})`, // 随机颜色
     fontSize: `1${[Math.floor(Math.random() * 10)]}px`, // 随机字号
     padding: `${[Math.floor(Math.random() * 10)]}px`, // 随机内边距
     transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)` // 随机旋转角度
    })
   }
   // 指向
   this.codeList = codeList
   // 将当前数据派发出去
   this.$emit('update:value', codeList.map(item => item.code).join(''))
  },
  // 动态绑定样式
  getStyle (data) {
   return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
  }
 }

完整代码

使用

<valid-code :value.sync="validCode"></valid-code>

组件

<template>
 <div class="ValidCode disabled-select" :style="`width:${width}; height:${height}`" @click="refreshCode">
  <span v-for="(item, index) in codeList" :key="index" :style="getStyle(item)">{{item.code}}</span>
 </div>
</template>

<script>
export default {
 name: 'validCode',
 props: {
  width: {
   type: String,
   default: '100px'
  },
  height: {
   type: String,
   default: '40px'
  },
  length: {
   type: Number,
   default: 4
  }
 },
 data () {
  return {
   codeList: []
  }
 },
 mounted () {
  this.createdCode()
 },
 methods: {
  refreshCode () {
   this.createdCode()
  },
  createdCode () {
   let len = this.length,
    codeList = [],
    chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789',
    charsLen = chars.length
   // 生成
   for (let i = 0; i < len; i++) {
    let rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
    codeList.push({
     code: chars.charAt(Math.floor(Math.random() * charsLen)),
     color: `rgb(${rgb})`,
     fontSize: `1${[Math.floor(Math.random() * 10)]}px`,
     padding: `${[Math.floor(Math.random() * 10)]}px`,
     transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)`
    })
   }
   // 指向
   this.codeList = codeList
   // 将当前数据派发出去
   this.$emit('update:value', codeList.map(item => item.code).join(''))
  },
  getStyle (data) {
   return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
  }
 }
}
</script>

<style scoped lang="scss">
 .ValidCode{
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  span{
   display: inline-block;
  }
 }
</style>

源码地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍基于 Vue 的树形选择组件的示例代码,包括了基于 Vue 的树形选择组件的示例代码的使用技巧和注意事项,需要的朋友参考一下 本文介绍了基于 Vue 的树形选择组件。分享给大家,具体如下: 系统要求:Vue 2 基本特性  完美的多级联动效果  支持无限多的分级  有 全选、半选、不选 三种状态  截图展示 代码如下: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多

  • 本文向大家介绍vue生成随机验证码的示例代码,包括了vue生成随机验证码的示例代码的使用技巧和注意事项,需要的朋友参考一下 本文介绍了vue生成随机验证码的示例代码,分享给大家,具体如下: 样式自调,最终效果如图: 实现效果: 点击右边input框会自动切换,如果输入的值与字不同,则清空换一串随机数 HTML JS 友情提示:本文直接从项目拿来供大家思路参考,验证提示那块大家可根据自己情况做更改。

  • 本文向大家介绍Vue from-validate 表单验证的示例代码,包括了Vue from-validate 表单验证的示例代码的使用技巧和注意事项,需要的朋友参考一下 前言 需要进行表单数据验证,原先才用html5来完成验证,但是效果很差,也不够灵活,所以需要进行自定义的表单验证,网上的插件都太过庞大,项目并没有这么多的需求。 那让我们自己来写一个吧! 知识准备 vue的自定义指令 具体可以看

  • 本文向大家介绍Vue组件之Tooltip的示例代码,包括了Vue组件之Tooltip的示例代码的使用技巧和注意事项,需要的朋友参考一下 前言 本文主要Alert 组件的大致框架, 提供少量可配置选项。 旨在大致提供思路 tooltip 常用于展示鼠标 hover 时的提示信息。 模板结构 大致结构DOM结构 一个div 包含 箭头 及 气泡内容。 v-bind中可选tooltip位置,是否禁用,及

  • 本文向大家介绍vue验证码组件应用实例,包括了vue验证码组件应用实例的使用技巧和注意事项,需要的朋友参考一下 代码如下: 总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对呐喊教程的支持。如果你想了解更多相关内容请查看下面相关链接

  • 本文向大家介绍开发Vue树形组件的示例代码,包括了开发Vue树形组件的示例代码的使用技巧和注意事项,需要的朋友参考一下 本文介绍了Vue树形组件的示例代码,分享给大家,具体如下: 使用SemanticUI和vue做一个menubar组件,实现方法大概是这样的: 使用时,假如父组件app使用到了menubar组件,那么data中需要定义一下items数据,例 : 里面的click事件是定义了,当在工