vue+vant实现自定义键盘样式

本文介绍了一种虚拟键盘的设计方案,包括直接显示和弹框加倒计时两种效果的实现方式。通过Vue.js实现了英文数字键盘,并提供了输入验证及确认功能。

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

一、第一种效果如下面所示(直接显示)

代码部分

<!-- 英文 数字 键盘 -->
<div class="allBoard">
  <van-popup
       v-model="show_allBoard"
       position="bottom"
       :overlay="false"
        overlay-class="displayNone"
  >
    <div class="plate_number_box">
       <!-- 点击对应的字母或数字,进行输入 -->
       <van-button
          size="small"
          v-for="(item, index) in English_Number"
          :key="item.id"
          @click="checkEnglish_num(index)"
          >{{ item.name }}</van-button
        >
          <!-- <div class="close-box" @click.stop="close_keyboard">
            <div>╳</div>
            <li></li>
          </div> -->
     </div>
   </van-popup>
</div>                               
data() {
  return {
      loginForm: {
        code: "",
      },
      show_allBoard: true, //是否显示英文数字键盘
      English_Number: [
        { name: "1", id: 28 },
        { name: "2", id: 29 },
        { name: "3", id: 30 },
        { name: "4", id: 31 },
        { name: "5", id: 32 },
        { name: "6", id: 33 },
        { name: "7", id: 34 },
        { name: "8", id: 35 },
        { name: "9", id: 36 },
        { name: "0", id: 37 },
        { name: "删除", id: 99 },
        { name: "Q", id: 38 },
        { name: "W", id: 39 },
        { name: "E", id: 40 },
        { name: "R", id: 41 },
        { name: "T", id: 42 },
        { name: "Y", id: 43 },
        { name: "U", id: 44 },
        { name: "I", id: 45 },
        { name: "O", id: 46 },
        { name: "P", id: 47 },
        { name: "A", id: 48 },
        { name: "S", id: 49 },
        { name: "D", id: 50 },
        { name: "F", id: 51 },
        { name: "G", id: 52 },
        { name: "H", id: 53 },
        { name: "J", id: 54 },
        { name: "K", id: 55 },
        { name: "L", id: 56 },
        { name: "Z", id: 57 },
        { name: "X", id: 58 },
        { name: "C", id: 59 },
        { name: "V", id: 60 },
        { name: "B", id: 61 },
        { name: "N", id: 62 },
        { name: "M", id: 63 },
        { name: "确认", id: 100 },
      ],
      numArr: [],
    };
  },
methods:{
    // 虚拟键盘
    checkEnglish_num(index) {
      // 如果点击删除键,删除 numArr 的最后一个值
      if (this.English_Number[index].id == 99) {
        //删除
        this.numArr.pop();
        this.loginForm.code = this.numArr.toString().replace(/,/g, "");
        // // 如果 numArr 里面被删的没有值了,切换键盘
        // if (this.numArr.length == 0) {
        //   this.show_allBoard = false;
        // }
      } else if (this.English_Number[index].id == 100) {
        //确定
        //this.submit();
      } else {
        // 把选中的值 push 到 numArr 内
        this.numArr.push(this.English_Number[index].name);
        // // 如果 numArr 中的值超过 7 个(车牌号的最大位数),删除最后一个
        // if (this.numArr.length > 7) {
        //   this.numArr.pop();
        // }
        this.loginForm.code = this.numArr.toString().replace(/,/g, "");
      }
    },
    // 关闭虚拟键盘
    close_keyboard() {
      this.show_allBoard = false;
    },
    submit() {
      // 键盘最后输入值
      if (this.loginForm.code == "") {
        handleMessage("输入内容不能为空!");
        return;
      }
      console.log(this.loginForm.code)
    },
}
.allBoard {
  .van-popup {
    @extend .overflow-y;
  }
  .plate_number_box {
    width: 100%;
    @extend .class-plate-box;
    .close-box {
      @extend .class-close-box;
      div {
        @extend .class-close-box-div;
      }
      li {
        @extend .class-close-box-li;
      }
    }
    .van-button--small {
      width: 8.8%;
      height: 2rem;
      @extend .class-van-button-small;
    }
    .van-button--small:nth-child(1) {
      margin-bottom: 5px;
    }
    .van-button--small:nth-child(12) {
      margin-left: 5%;
    }
    .van-button--small:nth-child(22) {
      margin-left: 9%;
    }
    .van-button--small:nth-child(31) {
      margin-left: 11.5%;
    }
    .van-button--small:last-child {
      width: 13%;
    }
  }
}
.displayNone {
  display: none !important;
}

 二、第二种效果如下图所示(弹框+倒计时功能)

 代码部分

<section class="messageMask" v-if="ifWritePopUp" @click="clickFocus">
      <div class="messageMaskContent" style="height: 52em">
        <div class="message-title" style="height: 25%">提示</div>
        <div class="message-title-close" @click="onClose">
          关闭({{ countDownVal }}S)
        </div>
        <div class="message-content">
          <div class="screen-sign-mid">
            <div class="screen-sign-mid-inner">
              <input
                class="self-el-input"
                type="text"
                v-model="cardNo"
                ref="cardNoInput"
              />
              <button
                class="self-el-button"
                type="button"
                @click.stop="checkIn()"
              >
                确认
              </button>
            </div>
          </div>
          <div class="screen-sign-footer">
            <div class="keyboard-wrap">
              <div
                class="key-group-item"
                v-for="(keyItem, index) in keyList"
                :key="index"
              >
                <div
                  class="key-item"
                  :style="item.type == 'letter' ? '' : 'width:155px;'"
                  v-for="(item, index) in keyItem"
                  :key="index"
                  :data-type="item.type"
                  @click.stop="keyboardClick"
                >
                  <span class="vertical-center">{{ item.text }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

 

data() {
  return {
      ifWritePopUp: false,
      countDownVal: 30,
      showTimer: null,
      cardNo: "",//键盘输入内容
      keyList: [
        // 键盘布局
        [
          { text: "1", type: "letter" },
          { text: "2", type: "letter" },
          { text: "3", type: "letter" },
          { text: "4", type: "letter" },
          { text: "5", type: "letter" },
          { text: "6", type: "letter" },
          { text: "7", type: "letter" },
          { text: "8", type: "letter" },
          { text: "9", type: "letter" },
          { text: "0", type: "letter" },
        ],
        [
          { text: "Q", type: "letter" },
          { text: "W", type: "letter" },
          { text: "E", type: "letter" },
          { text: "R", type: "letter" },
          { text: "T", type: "letter" },
          { text: "Y", type: "letter" },
          { text: "U", type: "letter" },
          { text: "I", type: "letter" },
          { text: "O", type: "letter" },
          { text: "P", type: "letter" },
        ],
        [
          { text: "A", type: "letter" },
          { text: "S", type: "letter" },
          { text: "D", type: "letter" },
          { text: "F", type: "letter" },
          { text: "G", type: "letter" },
          { text: "H", type: "letter" },
          { text: "J", type: "letter" },
          { text: "K", type: "letter" },
          { text: "L", type: "letter" },
        ],
        [
          { text: "Z", type: "letter" },
          { text: "X", type: "letter" },
          { text: "C", type: "letter" },
          { text: "V", type: "letter" },
          { text: "B", type: "letter" },
          { text: "N", type: "letter" },
          { text: "M", type: "letter" },
        ],
        [
          { text: "回删", type: "delete" },
          { text: "重置", type: "reset" },
        ],
      ],
    };
},
created() {
    this.beginCountDown();
    this.ifWritePopUp = true;
    this.$nextTick(function () {
      this.$refs["cardNoInput"].focus();
    });
},
methods:{
    // 处理数字键盘事件
    keyboardClick(event) {
      let text = event.currentTarget.innerText;
      let type = event.currentTarget.getAttribute("data-type");
      switch (type) {
        case "letter":
          this.cardNo += text;
          break;
        case "delete":
          this.cardNo = this.cardNo.substr(0, this.cardNo.length - 1);
          break;
        case "reset":
          this.cardNo = "";
          break;
      }
      this.$refs.cardNoInput.focus();
    },
    checkIn() {
      //手动输入
      if (this.cardNo == "") {
        this.$refs["cardNoInput"].focus();
        return;
      }
      //执行输入后确定操作
      this.cardNo = "";
    },
    beginCountDown(time = 30) {
      //开启提示窗倒计时
      //直接倒计时30s
      if (this.showTimer) {
        clearInterval(this.showTimer);
      }
      this.countDownVal = time;
      this.showTimer = setInterval(() => {
        if (this.countDownVal > 1) {
          this.countDownVal--;
        } else {
          clearInterval(this.showTimer);
          this.showTimer = null;
          this.onClose();
        }
      }, 1000);
    },
    onClose() {
      if (this.ifWritePopUp) {
        this.ifWritePopUp = false;
        this.cardNo = "";
      }
    },
    clickFocus() {
      //点击屏幕任何地方都聚焦
      if (this.ifWritePopUp) {
        this.$refs.cardNoInput.focus();
      }
    },
}
.messageMask {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 9;
}

.messageMaskContent {
  position: absolute;
  top: 25%;
  left: 30%;
  width: 40%;
  height: 55em;
  border: 1px solid #eee;
  background: #fff;
  border-radius: 20px;
  box-shadow: 3px 8px 5px rgba(0, 0, 0, 0.3);
}

.message-title {
  position: relative;
  width: 100%;
  height: 25%;
  background-color: #1989fa;
  text-align: center;
  font-weight: bold;
  font-size: 50px;
  color: #fff;
  overflow: hidden;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.message-title-close {
  position: absolute;
  font-size: 40px;
  right: 15px;
  top: 15px;
  color: #fff;
}

.message-content {
  height: 70%;
  padding: 10px 20px 0;
}

.screen-sign-mid {
  position: relative;
  width: 100%;
  height: 80px;
  padding: 3px;
  box-sizing: border-box;
  background-color: #fff;
  color: #34592d;
}

.screen-sign-mid .screen-sign-mid-inner {
  width: 100%;
  height: 100%;
  position: relative;
  box-sizing: border-box;
}

.self-el-input {
  display: inline-block;
  width: 100%;
  height: 100%;
  padding: 0 100px 0 15px;
  font-size: 50px;
  color: #000;
  border: 2px solid #1989fa;
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
  transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
  outline: 0;
}

.self-el-button {
  display: inline-block;
  position: absolute;
  top: 2px;
  right: 2px;
  width: 120px;
  height: 70px;
  margin: 0;
  font-weight: bold;
  font-size: 30px;
  letter-spacing: 5px;
  line-height: 1;
  border: 1px solid #ccc;
  color: #000;
  background-color: #ccc;
  white-space: nowrap;
  -webkit-appearance: none;
  text-align: center;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-transition: .1s;
  transition: .1s;
  outline: 0;
  cursor: pointer;
}

.screen-sign-main .screen-sign-footer {
  position: relative;
  width: 100%;
  height: auto;
  background-color: #fff;
  text-align: center;
  color: #333;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
}

.keyboard-wrap {
  width: 100%;
  margin-top: 10px;
  box-sizing: border-box;
}

.keyboard-wrap .key-group-item {
  width: 100%;
  height: auto;
  text-align: center;
}

.key-group-item .key-item {
  display: inline-block;
  position: relative;
  width: 60px;
  height: 65px;
  line-height: 65px;
  margin: 0 2px 8px 2px;
  color: #000;
  font-size: 40px;
  box-sizing: border-box;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 15px;
  background-color: #bbb;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  cursor: pointer;
}

.key-item:hover {
  background-color: #ccc;
}

.key-item:active {
  background-color: #666;
}

<think>好的,我现在需要帮用户解决在Vue3中使用Vant组件库实现车牌号软键盘的问题。首先,我得理解用户的需求。用户希望有一个示例教程,展示如何结合Vue3和Vant实现这个功能。根据提供的引用内容,特别是引用[2]提到的移动端VUE车牌键盘,虽然简单但适合小白,这可能是一个起点。 首先,我需要考虑Vant组件库中的哪些组件适合用来构建软键盘Vant提供了NumberKeyboard数字键盘组件,这可能是一个基础。但车牌号不仅有数字,还有省份简称的字母,所以可能需要自定义部分。用户提到的支持不同车辆类型,比如新能源、警车等,可能需要切换不同的键盘布局。 接下来,步骤应该是: 1. 安装Vant:确保用户已经正确安装VantVue3项目中,可能需要使用Vant4版本,因为Vue3和Vant4兼容。 2. 创建车牌输入组件:使用Vant的Field组件作为输入框,显示车牌号,并触发键盘弹出。 3. 自定义键盘布局:结合Vant的NumberKeyboard,但需要添加省份选择(可能是弹出式Picker)和字母部分。可能需要自己布局按钮,比如使用Grid栅格布局。 4. 处理输入逻辑:包括省份选择、字母和数字的输入,以及删除功能。同时需要正则验证,比如引用[1]提到的正则验证规则。 5. 切换键盘类型:比如新能源车牌比普通车牌多一位,可能需要切换不同的键盘模式,或者动态调整输入长度限制。 6. 样式调整:确保键盘和输入框的样式适配移动端,可能需要自定义样式覆盖Vant默认样式。 可能的难点在于省份选择和字母键盘的切换。例如,当用户点击输入框时,首先弹出省份选择,选择省份后切换到字母和数字键盘。或者,将省份作为键盘的一部分,比如第一行显示常用省份简称,然后下面是字母和数字。 另外,正则验证部分需要根据不同的车牌类型来调整。例如,普通车牌的格式是省份+字母+5位字母或数字,而新能源可能是省份+字母+更多位。需要参考引用[1]中的正则规则,但用户提供的引用中没有详细的正则表达式,可能需要自行补充或建议用户查找相关正则。 在代码结构方面,可能需要一个Vue组件,包含输入框、键盘、省份选择器。数据部分包括输入的车牌号、当前键盘类型(省份选择或字母数字)、以及车牌类型(普通、新能源等)。方法包括处理按键输入、删除、切换键盘类型等。 测试时需要注意不同车牌类型的输入限制,以及正确触发验证。同时,用户体验方面,输入流畅,键盘弹出和隐藏的时机要合适,比如点击输入框弹出,输入完成后自动隐藏或点击确认按钮隐藏。 最后,示例代码需要简洁明了,注释清楚,方便用户理解每一步的作用。同时,提醒用户可能需要根据实际需求调整样式和验证规则,比如引用[2]提到的简单实现,可能不需要处理所有边缘情况,但至少提供一个可工作的示例。</think>在Vue3中实现车牌号软键盘功能,可以通过Vant组件库的`NumberKeyboard`结合自定义布局完成。以下是分步骤实现方案: --- ### 一、基础环境搭建 1. 安装依赖 ```bash npm install vant@next vue@3 ``` 2. 按需引入组件(在main.js中) ```javascript import { createApp } from 'vue' import { Field, NumberKeyboard, Picker } from 'vant' const app = createApp() app.use(Field) app.use(NumberKeyboard) app.use(Picker) ``` --- ### 二、核心实现代码 ```vue <template> <div class="license-plate-input"> <!-- 输入框 --> <van-field v-model="plateNumber" readonly clickable placeholder="点击输入车牌" @click="showKeyboard = true" /> <!-- 车牌键盘 --> <van-number-keyboard v-model:show="showKeyboard" :extra-key="provinceKey" theme="custom" close-button-text="完成" @blur="showKeyboard = false" > <!-- 自定义省份行 --> <div class="province-row"> <button v-for="(prov, index) in provinces" :key="index" @click="handleProvince(prov)" > {{ prov }} </button> </div> <!-- 自定义字母数字行 --> <div class="char-row"> <button v-for="(char, idx) in currentChars" :key="idx" @click="handleInput(char)" > {{ char }} </button> <button @click="handleDelete">⌫</button> </div> </van-number-keyboard> </div> </template> <script setup> import { ref, computed } from 'vue' const provinces = ['京', '沪', '粤', '浙', '苏', '鲁', '川'] const chars = [ '1','2','3','4','5','6','7','8','9','0', 'Q','W','E','R','T','Y','U','I','O','P', 'A','S','D','F','G','H','J','K','L', 'Z','X','C','V','B','N','M' ] const plateNumber = ref('') const showKeyboard = ref(false) const isProvinceSelected = ref(false) // 动态切换输入模式 const currentChars = computed(() => { return isProvinceSelected.value ? chars : provinces }) const handleProvince = (prov) => { plateNumber.value = prov isProvinceSelected.value = true } const handleInput = (char) => { if (plateNumber.value.length < 8) { // 新能源车牌最多8位 plateNumber.value += char } } const handleDelete = () => { plateNumber.value = plateNumber.value.slice(0, -1) if (plateNumber.value.length === 0) { isProvinceSelected.value = false } } </script> <style scoped> .province-row, .char-row { display: flex; flex-wrap: wrap; gap: 4px; padding: 8px; } button { flex: 1; min-width: 12%; height: 48px; border-radius: 8px; background: #f5f5f5; border: none; } </style> ``` --- ### 三、关键功能解析 1. **双模式切换**:通过`isProvinceSelected`控制省份选择与字输入两种模式 2. **输入长度限制**:新能源车牌最长支持8位(普通车牌7位) 3. **正则验证**(可结合引用[1]的验证逻辑): ```javascript // 在handleInput后添加验证 const validatePlate = () => { const pattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/ return pattern.test(plateNumber.value) } ``` --- ### 四、高级优化建议 1. **类型切换**:通过`<van-picker>`增加车牌类型选择器(普通/新能源/警用等) 2. **本地存储**:使用`localStorage`记忆用户常用省份 3. **动效优化**:添加键盘弹出/收起动画 4. **语音输入**:集成Web Speech API实现语音识别输入[^2] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值