常看到各种app应用中使用自定义的键盘,本例子中使用vue2实现个简单的键盘,支持在移动端和PC端使用,欢迎点赞,h5 ios输入框与键盘 兼容性优化

实现效果:

Keyboard.vue


  1. <template>
  2. <div class="keyboard" v-show="showKeyboard" v-clickoutside="closeModal">
  3. <p v-for="keys in keyList">
  4. <template v-for="key in keys">
  5. <i v-if="key === 'top'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-zhiding tab-top"></i>
  6. <i v-else-if="key === '123'" @click.stop="clickKey" @touchend.stop="clickKey" class="tab-num">123</i>
  7. <i v-else-if="key === 'del'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-delete key-delete"></i>
  8. <i v-else-if="key === 'blank'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-konggejian-jianpanyong tab-blank"></i>
  9. <i v-else-if="key === 'symbol'" @click.stop="clickKey" @touchend.stop="clickKey" class="tab-symbol">符</i>
  10. <i v-else-if="key === 'point'" @click.stop="clickKey" @touchend.stop="clickKey" class="tab-point">·</i>
  11. <i v-else-if="key === 'enter'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-huiche tab-enter"></i>
  12. <i v-else @click.stop="clickKey" @touchend.stop="clickKey">{{key}}</i>
  13. </template>
  14. </p>
  15. </div>
  16. </template>
  17. <script>
  18. import clickoutside from '../directives/clickoutside'
  19. export default {
  20. directives: { clickoutside },
  21. data() {
  22. return {
  23. keyList: [],
  24. status: 0,//0 小写 1 大写 2 数字 3 符号
  25. lowercase: [
  26. ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
  27. ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
  28. ['top', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'del'],
  29. ['123', 'point', 'blank', 'symbol', 'enter']
  30. ],
  31. uppercase: [
  32. ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
  33. ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'],
  34. ['top', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 'del'],
  35. ['123', 'point', 'blank', 'symbol', 'enter']
  36. ],
  37. equip:!!navigator.userAgent.toLocaleLowerCase().match(/ipad|mobile/i)//是否是移动设备
  38. }
  39. },
  40. props: {
  41. option: {
  42. type: Object
  43. }
  44. },
  45. computed: {
  46. showKeyboard(){
  47. return this.option.show
  48. }
  49. },
  50. mounted() {
  51. this.keyList = this.lowercase
  52. },
  53. methods: {
  54. tabHandle({ value = '' }) {
  55. if(value.indexOf('tab-num') > -1){
  56. this.status = 2
  57. //数字键盘数据
  58. }else if(value.indexOf('key-delete') > -1){
  59. this.emitValue('delete')
  60. }else if(value.indexOf('tab-blank') > -1){
  61. this.emitValue(' ')
  62. }else if(value.indexOf('tab-enter') > -1){
  63. this.emitValue('\n')
  64. }else if(value.indexOf('tab-point') > -1){
  65. this.emitValue('.')
  66. }else if(value.indexOf('tab-symbol') > -1){
  67. this.status = 3
  68. }else if(value.indexOf('tab-top') > -1){
  69. if(this.status === 0){
  70. this.status = 1
  71. this.keyList = this.uppercase
  72. }else{
  73. this.status = 0
  74. this.keyList = this.lowercase
  75. }
  76. }else{
  77. }
  78. },
  79. clickKey(event) {
  80. if(event.type === 'click' && this.equip) return
  81. let value = event.srcElement.innerText
  82. value && value !== '符' && value !== '·' && value !== '123'? this.emitValue(value) : this.tabHandle(event.srcElement.classList)
  83. },
  84. emitValue(key) {
  85. this.$emit('keyVal', key)
  86. },
  87. closeModal(e) {
  88. if (e.target !== this.option.sourceDom) {
  89. // this.showKeyboard = false
  90. this.$emit('close', false)
  91. }
  92. }
  93. }
  94. }
  95. </script>
  96. <style scoped lang="less">
  97. .keyboard {
  98. width: 100%;
  99. margin: 0 auto;
  100. font-size: 18px;
  101. border-radius: 2px;
  102. padding-top: 0.5em;
  103. background-color: #e5e6e8;
  104. user-select: none;
  105. position: fixed;
  106. bottom: 0;
  107. left: 0;
  108. right: 0;
  109. z-index: 999;
  110. pointer-events: auto;
  111. p {
  112. width: 95%;
  113. margin: 0 auto;
  114. height: 45px;
  115. margin-bottom: 0.5em;
  116. display: flex;
  117. display: -webkit-box;
  118. flex-direction: row;
  119. flex-wrap: nowrap;
  120. justify-content: center;
  121. i {
  122. display: block;
  123. margin: 0 1%;
  124. height: 45px;
  125. line-height: 45px;
  126. font-style: normal;
  127. font-size: 24px;
  128. border-radius: 3px;
  129. width: 44px;
  130. background-color: #fff;
  131. text-align: center;
  132. flex-grow: 1;
  133. flex-shrink: 1;
  134. flex-basis: 0;
  135. -webkit-box-flex: 1;
  136. &:active {
  137. background-color: darken(#ccc, 10%);
  138. }
  139. }
  140. .tab-top {
  141. width: 50px;
  142. margin: 0 1%;
  143. background: #cccdd0;
  144. color: #fff;
  145. font-size: 24px;
  146. }
  147. .key-delete {
  148. width: 50px;
  149. margin: 0 1%;
  150. color: #827f7f;
  151. background: #d7d7d8;
  152. }
  153. .tab-num {
  154. font-size: 18px;
  155. background: #dedede;
  156. color: #5a5959;
  157. }
  158. .tab-point {
  159. width: 20px;
  160. }
  161. .tab-blank {
  162. width: 80px;
  163. font-size: 12px;
  164. padding: 0 15px;
  165. color: #5a5959;
  166. line-height: 60px;
  167. }
  168. .tab-symbol {
  169. width: 20px;
  170. font-size: 18px;
  171. }
  172. .tab-enter {
  173. font-size: 30px;
  174. line-height: 54px;
  175. }
  176. &:nth-child(2) {
  177. width: 88%;
  178. }
  179. }
  180. }
  181. </style>

KeyInput.vue


  1. <template>
  2. <div>
  3. <input type="text" ref="keyboard" v-model="inputValue" @focus="onFocus">
  4. <Keyboard :option="option" @keyVal="getInputValue" @close="option.show = false"></Keyboard>
  5. </div>
  6. </template>
  7. <script>
  8. import Keyboard from '../components/Keyboard'
  9. export default {
  10. components: {
  11. Keyboard
  12. },
  13. data() {
  14. return {
  15. option: {
  16. show: false,
  17. sourceDom: ''
  18. },
  19. inputValue: ''
  20. }
  21. },
  22. props: {},
  23. created() {},
  24. methods: {
  25. getInputValue(val) {
  26. if(val === 'delete'){
  27. this.inputValue = this.inputValue.slice(0,this.inputValue.length -1)
  28. }else{
  29. this.inputValue += val
  30. }
  31. },
  32. onFocus() {
  33. this.$set(this.option, 'show', true)
  34. this.$set(this.option, 'sourceDom', this.$refs['keyboard'])
  35. },
  36. //获取光标位置
  37. getCursorPosition() {
  38. let doc = this.$refs['keyboard']
  39. if (doc.selectionStart) return doc.selectionStart
  40. return -1
  41. },
  42. //设置光标位置 暂未实现
  43. setCursorPosition(pos) {
  44. let doc = this.$refs['keyboard']
  45. console.log(doc.setSelectionRange)
  46. doc.focus()
  47. doc.setSelectionRange(1,3)
  48. }
  49. }
  50. }
  51. </script>
  52. <style lang="less" scoped>
  53. </style>

使用demo


  1. <template>
  2. <div>
  3. <key-input class="demo-class"></key-input>
  4. </div>
  5. </template>
  6. <script>
  7. import KeyInput from '../components/KeyInput'
  8. export default {
  9. components: {
  10. KeyInput
  11. },
  12. data() {
  13. return {
  14. }
  15. },
  16. created() {},
  17. methods: {
  18. }
  19. }
  20. </script>
  21. <style lang="less">
  22. body{
  23. background: #efefef;
  24. }
  25. .demo-class{
  26. input{
  27. border:1px solid #ccc;
  28. outline: none;
  29. height: 30px;
  30. font-size: 16px;
  31. letter-spacing: 2px;
  32. padding: 0 5px;
  33. }
  34. }
  35. </style>

完整代码:https://github.com/dawnyu/vue...

原文地址:

使用vue实现简单键盘,支持移动端和pc端的更多相关文章

  1. C# 移动端与PC端的数据交互

    小记:针对目前功能越来越强大的智能手机来说,在PC端支持对手机中的用户数据作同步.备份以及恢复等保护措施的应用已经急需完善.不仅要对数据作保护,而且用户更希望自己的手机跟PC能够一体化,以及和远程服务 ...

  2. 前端:移动端和PC端的区别

    在阿里的几次面试中,总是被问到移动端和PC端有什么区别,当时回答的时候主要是回答了在兼容性.网速.适配.页面布局等方面的不同,但是还是很不系统,所以这里做一个总结. 第一: PC考虑的是浏览器的兼容性 ...

  3. 简述移动端与PC端的区别

    1.移动端与PC端的区别 PC考虑的是浏览器的兼容性,而移动端开发考虑的更多的是手机兼容性,因为目前不管是android手机还是ios手机,一般浏览器使用的都是webkit内核,所以说做移动端开发,更 ...

  4. JavaScript判断移动端及pc端访问不同的网站

    JavaScript判断移动端及pc端访问不同的网站 现在很多网站都是分为两个版本,一个pc端的一个移动端的(响应式除外),针对这两个版本,就需要对访问的设备进行判断,如果是pc,就直接访问pc网站, ...

  5. 关于移动端和PC端的交互的区别

    对于现在的移动端设备的普及,移动端上的用户体验成了一个重要的关注点. 看了一些网上的关于移动端的交互和用户体验的知识,这里总结了一些.若有不足的地方,希望大家能够积极补充. PC端和移动端的产品的设计 ...

  6. html与css的移动端与pc端需要注意的事项

    一个移动端与pc端之间最主要的也就是尺寸问题,苹果与安卓的机型尺寸大小相差甚多,一个尺寸都会影响用户的体验.那么我们来了解一下一些常用的解决方法. 一般在网页中都会在头部有一些这样的代码 <me ...

  7. 移动端和PC端弹出遮罩层后,页面禁止滚动的解决方法及探究

    PC端解决方案 pc端的解决思路就是在弹出遮罩层的时候取消已经存在的滚动条,达到无法滚动的效果. 也就是说给body添加overflow:hidden属性即可,IE6.7下不会生效,需要给html增加 ...

  8. 移动端和PC端页面常用的弹出层

    我们在页面的时候,很多时候用到了弹出层,消息提醒,确认框等等,统一样式的弹出框可以使页面更加优美.在此,我整理一下我们项目的移动端和PC端页面常用的弹出层. 一.移动端 我们需在页面引入弹出框的样式和 ...

  9. iis 如何搭建url 重定向,实现无线端和pc端不同的跳转

    第一步,下载安装ARR(Application Request Routing), http://www.iis.net/downloads/microsoft/application-request ...

随机推荐

  1. [USACO18FEB] Snow Boots G (离线+并查集)

    题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...

  2. python_函数传递列表

    '''#创建一个空列表,包含一些要打印的设计unprinted_designs = ['iphone','robot','books']completed_modles = [] #模拟打印每个设计, ...

  3. Tire树总结(模板+例题)

    题目来自<算法竞赛设计指南> Tire树是一种可以快速查找字符串的数据结构 模板 #include<cstdio> #include<algorithm> #inc ...

  4. SpringBoot中使用过滤器Filter

    场景:API的参数都是经过加密的,于是在过滤器中,将获取到的请求的参数先解密再去进行处理 一.实现Filter接口 public class TestFilter implements Filter ...

  5. android 使用讯飞人脸识别api报错:java.lang.UnsatisfiedLinkError

    1.在做一个人脸识别的项目,使用的是讯飞的api,编辑器为AS2.0,运行时报如下错误: FATAL EXCEPTION: main                  Process: com.adm ...

  6. CF16A Flag

    CF16A Flag 题意翻译 题目描述 根据一项新的ISO标准,每一个国家的国旗应该是一个n×m的格子场,其中每个格子最多有10种不同的颜色.并且国旗应该有条纹:旗帜的每一行应包含相同颜色的方块,相 ...

  7. pcapy-0.10.8 安装

    (1)下载 http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name= ...

  8. HDU 4331 Contest 4

    一个很直观的想法是,求出每个点上下左右能到达的最大长度.然后枚举其斜边...没想到过了.... 当然,题解有一个很巧妙的优化,利用树状数组,那个太巧妙了. #include<iostream&g ...

  9. bzoj-1492 货币兑换Cash (1)——平衡树维护凸包

    题意: 有n天和m的初始金钱,用来购买AB两种纪念券: n天里每天都有AB的价格.每天能够进行这种操作. 1.卖出手中x%的纪念券(AB分别都卖出x%). 2.用x的金钱买入纪念券.买入AB券的比例在 ...

  10. 从头认识java-16.4 nio的读与写(ByteBuffer的使用)

    这一章节我们来讨论一下nio的读与写. 1.nio的读 package com.ray.ch16; import java.io.IOException; import java.io.RandomA ...