vue实现分割验证码效果

本文实例为大家分享了vue实现分割验证码效果的具体代码,供大家参考,具体内容如下

注:该代码存在问题,因为我maxlength =4 ,input 的宽度跟屏幕同宽,所以当我input 里面的length == 4 的时候,我的光标会超出屏幕,所以我在length == 4 的时候做了一个把光标放到第三个的处理,这种虽用户体验不好,也解了length==4的时候整个布局乱了的燃眉之急

上UI 图

上代码:

vue

<template>
  <div>
    <v-main class="bgi">
      <v-container class="verify">
        <v-icon @click="verifyShow = false" color="#000">mdi-close</v-icon>
        <div>
          <p class="verifyTitle">输入验证码</p>
          <p class="verifyTitle1" v-show="verifyStatus == 1">
            验证码已发送至 <span> {{ "+" + area + phone }}</span>
          </p>
          <p class="verifyTitle2" v-show="verifyStatus == 3">
            验证码不正确,请重新输入
          </p>
          <div class="verifyInputBox">
            <div class="verifyInputBg verifyInputBg1"></div>
            <div class="verifyInputBg verifyInputBg2"></div>
            <div class="verifyInputBg verifyInputBg3"></div>
            <div class="verifyInputBg verifyInputBg4"></div>
            <input
              ref="verifyInput"
              type="text"
              class="verifyInput"
              maxlength="4"
              v-model="verifyInput"
            />
          </div>
          <v-btn
            :disabled="restTimeShow"
            outlined
            tile
            class="nextBtn"
            @click="verifyConfirm"
          >
            确定
          </v-btn>
          <p class="restTime" v-show="restTimeShow">
            重新获取(<span>{{ restTime }}</span
            >s)
          </p>
        </div>
      </v-container>
    </v-main>
  </div>
</template>

css

<style lang="scss" scoped>
.bgi {
  width: 100vw;
  height: 100vh;
  background: url("~@/assets/imgs/banner.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  p {
    margin-bottom: 0;
  }
  .verify {
    width: 100%;
    height: 100%;
    position: relative;
    color: #000;
    background: #fff;
    .verifyTitle {
      margin-left: 2rem;
      margin-top: 3rem;
      font-size: 1.4rem;
    }
    .verifyTitle1 {
      margin-left: 2rem;
      margin-top: 0.6rem;
      font-size: 0.8125rem;
      span {
        color: #bfbfbf;
      }
    }
    .verifyTitle2 {
      margin-left: 2rem;
      margin-top: 0.6rem;
      font-size: 0.8125rem;
      color: #f53f3f;
    }
    input::-webkit-input-placeholder {
      color: #000;
    }
    .verifyInputBox {
      width: 20rem;
      margin: 3rem auto 0;
      position: relative;
      height: 4rem;
      z-index: 1;
      .verifyInputBg {
        position: absolute;
        z-index: 2;
        height: 4rem;
        width: 4rem;
        border-radius: 0.2rem;
        background: rgba($color: #000, $alpha: 0.1);
      }
      .verifyInputBg2 {
        left: 5.33rem;
      }
      .verifyInputBg3 {
        left: 10.66rem;
      }
      .verifyInputBg4 {
        left: 16rem;
      }
      .verifyInput {
        height: 4rem;
        line-height: 4rem;
        width: 100%;
        letter-spacing: 4.53rem;
        padding-left: 1.5rem;
        font-size: 1.4rem;
        font-weight: 700;
        position: absolute;
        z-index: 3;
      }
    }
    .restTime {
      margin-top: 1rem;
      color: #bfbfbf;
      font-size: 0.8125rem;
      text-align: center;
    }
  }
  .nextBtn {
    margin-top: 2rem;
    margin-left: 2rem;
    width: calc(100% - 4rem);
    height: 3rem !important;
    background: #ff962b;
    border-radius: 0.3125rem;
    font-size: 1rem;
    color: #ffffff !important;
    border: none;
  }
  .nextBtn:disabled {
    background: #d2d2d2;
    color: #ffffff !important;
  }
}
</style>

script

<script>
export default {
  name: "Login",
  data() {
    return {
      area: "86",
      phone: "",
      verifyInput: "",
      verifyStatus: "1", // 1  -- 验证码发送至  ||  2 --正在验证验证码  || 3 -- 验证码错误
      restTimer: null,
      restTime: 60,
      restTimeShow: false,
    };
  },
  watch: {
    verifyInput(newVal) {
      if (newVal.length == 4) {
        this.$nextTick(() => {
          let verifyInput = this.$refs.verifyInput;
          verifyInput.setSelectionRange(3,3);
        });
        this.verifyConfirm();
      }
    },
  },
  methods: {
    verifyConfirm() {
      this.restTimeShow = true;
      this.verifyStatus = 2;
      this.restTimeFun();
    },
    restTimeFun() {
      if (!this.restTimer) {
        this.restTime = 60;
        this.restTimer = setInterval(() => {
          if (this.restTime > 0) {
            this.restTime--;
          } else {
            this.restTimeShow = false;
            clearInterval(this.restTimer);
            this.restTimer = null;
          }
        }, 1000);
      }
    },
  },
};
</script>

效果图:

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

(0)

相关推荐

  • vue生成随机验证码的示例代码

    本文介绍了vue生成随机验证码的示例代码,分享给大家,具体如下: 样式自调,最终效果如图: 实现效果: 点击右边input框会自动切换,如果输入的值与字不同,则清空换一串随机数 HTML <input type="text" placeholder="请输入验证码" class="yanzhengma_input" @blur="checkLpicma" v-model="picLyanzhengma"

  • vue+element-ui集成随机验证码+用户名+密码的form表单验证功能

    在登入页面,我们往往需要通过输入验证码才能进行登入,那我们下面就详讲一下在vue项目中如何配合element-ui实现这个功能 第一步:自定义一个生产随机验证码的组件,其本质是使用canvas绘制,详细代码如下: <template> <div class="s-canvas"> <canvas id="s-canvas" :width="contentWidth" :height="contentHeig

  • Vue 实现登录界面验证码功能

    登录界面 SIdentify 创建验证码组件,实现绘画出图片验证码 <template> <div class="s-canvas"> <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas> </div> </template> <script>

  • Vue中添加手机验证码组件功能操作方法

    什么是组件: 组件是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.在较高层面上,组件是自定义的元素,Vue.js的编译器为它添加特殊功能.在有些情况下,组件也可以是原生HTML元素的形式,以is特性扩展. 写在前面: 今天要实现的功能是在 完善个人信息页面(vue)中添加手机验证码组件,当用户点击 手机选项时,弹出获取验证码组件,完成验证手机的功能: 这里考虑到功能的复用,我把当前弹出手机验证码的操作放在了单独的组件中: <template > <div>

  • vue实现登录页面的验证码以及验证过程解析(面向新手)

    做成之后就 是这个样子 接下来上代码 创建一个组件.显示验证码图片 <template> <div class="s-canvas"> <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas> </div> </template> <script>

  • vue 实现通过手机发送短信验证码注册功能

    效果如下: 代码如下: template代码: <el-main> <el-form :model="ReginForm" ref="ReginForm" :rules="rule" class="regform" label-width="0"> <h3 class="login-text">手机注册</h3> <el-form-i

  • 简单实现vue验证码60秒倒计时功能

    本文实例为大家分享了vue验证码倒计时60秒的具体代码,供大家参考,具体内容如下 html <span v-show="show" @click="getCode">获取验证码</span> <span v-show="!show" class="count">{{count}} s</span> js data(){ return { show: true, count: ''

  • VUE实现图片验证码功能

    本文实例为大家分享了VUE实现图片验证码的具体代码,供大家参考,具体内容如下 1. 概述 1.1 说明 在开发过程中,有时候需要使用图片验证码进行增加安全强度,在点击图片时更新新的图片验证码,记录此功能,以便后期使用. 2. 示例 2.1 vue示例代码 <template> <el-form style="width: 400px;"> <el-form-item style="height: 40px;margin-bottom: 20px;

  • vue实现验证码输入框组件

    先来看波完成效果图 需求 输入4位或6位短信验证码,输入完成后收起键盘 实现步骤 第一步 布局排版 <div class="security-code-wrap"> <label for="code"> <ul class="security-code-container"> <li class="field-wrap" v-for="(item, index) in num

  • Vue实现滑动拼图验证码功能

    缘由:之前看哔哩哔哩官网登录的时候有一个拼图验证码,很好奇怎么去实现.然后就想着自己弄一个.先给大家看我的最终效果.后面再一点点拆解代码. 为什么想着写这个功能呢,主要在于拼图验证码在前端这里会比较复杂并且深入.相比文字拼写,12306的图片验证码都没有拼图验证码对前端的要求来的复杂,和难. 我总结下知识点: 1.弹窗功能 2.弹窗基于元素定位 3.元素拖动 4.canvas绘图 5.基础逻辑 一.弹窗和弹窗组件 抱歉,这里我偷懒了直接用了elementUI的el-popover组件,所以小伙伴

随机推荐