vue를 사용하여 간단한 키보드 작업 구현
이 글에서는 vue를 사용하여 간단한 키보드를 구현한 사례를 주로 소개합니다(모바일, PC 지원). 이제 공유해 드리며 참고용으로 올려드립니다.
다양한 앱에서 사용되는 맞춤형 키보드를 자주 볼 수 있습니다. 이 예에서는 모바일과 PC에서 사용할 수 있는 간단한 키보드를 구현하는 데 vue2가 사용됩니다.
효과 달성:
Keyboard.vue
<template> <p class="keyboard" v-show="showKeyboard" v-clickoutside="closeModal"> <p v-for="keys in keyList"> <template v-for="key in keys"> <i v-if="key === 'top'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-zhiding tab-top"></i> <i v-else-if="key === '123'" @click.stop="clickKey" @touchend.stop="clickKey" class="tab-num">123</i> <i v-else-if="key === 'del'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-delete key-delete"></i> <i v-else-if="key === 'blank'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-konggejian-jianpanyong tab-blank"></i> <i v-else-if="key === 'symbol'" @click.stop="clickKey" @touchend.stop="clickKey" class="tab-symbol">符</i> <i v-else-if="key === 'point'" @click.stop="clickKey" @touchend.stop="clickKey" class="tab-point">·</i> <i v-else-if="key === 'enter'" @click.stop="clickKey" @touchend.stop="clickKey" class="iconfont icon-huiche tab-enter"></i> <i v-else @click.stop="clickKey" @touchend.stop="clickKey">{{key}}</i> </template> </p> </p> </template> <script> import clickoutside from '../directives/clickoutside' export default { directives: { clickoutside }, data() { return { keyList: [], status: 0,//0 小写 1 大写 2 数字 3 符号 lowercase: [ ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'], ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'], ['top', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'del'], ['123', 'point', 'blank', 'symbol', 'enter'] ], uppercase: [ ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'], ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'], ['top', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 'del'], ['123', 'point', 'blank', 'symbol', 'enter'] ], equip:!!navigator.userAgent.toLocaleLowerCase().match(/ipad|mobile/i)//是否是移动设备 } }, props: { option: { type: Object } }, computed: { showKeyboard(){ return this.option.show } }, mounted() { this.keyList = this.lowercase }, methods: { tabHandle({ value = '' }) { if(value.indexOf('tab-num') > -1){ this.status = 2 //数字键盘数据 }else if(value.indexOf('key-delete') > -1){ this.emitValue('delete') }else if(value.indexOf('tab-blank') > -1){ this.emitValue(' ') }else if(value.indexOf('tab-enter') > -1){ this.emitValue('\n') }else if(value.indexOf('tab-point') > -1){ this.emitValue('.') }else if(value.indexOf('tab-symbol') > -1){ this.status = 3 }else if(value.indexOf('tab-top') > -1){ if(this.status === 0){ this.status = 1 this.keyList = this.uppercase }else{ this.status = 0 this.keyList = this.lowercase } }else{ } }, clickKey(event) { if(event.type === 'click' && this.equip) return let value = event.srcElement.innerText value && value !== '符' && value !== '·' && value !== '123'? this.emitValue(value) : this.tabHandle(event.srcElement.classList) }, emitValue(key) { this.$emit('keyVal', key) }, closeModal(e) { if (e.target !== this.option.sourceDom) { // this.showKeyboard = false this.$emit('close', false) } } } } </script> <style scoped lang="less"> .keyboard { width: 100%; margin: 0 auto; font-size: 18px; border-radius: 2px; padding-top: 0.5em; background-color: #e5e6e8; user-select: none; position: fixed; bottom: 0; left: 0; right: 0; z-index: 999; pointer-events: auto; p { width: 95%; margin: 0 auto; height: 45px; margin-bottom: 0.5em; display: flex; display: -webkit-box; flex-direction: row; flex-wrap: nowrap; justify-content: center; i { display: block; margin: 0 1%; height: 45px; line-height: 45px; font-style: normal; font-size: 24px; border-radius: 3px; width: 44px; background-color: #fff; text-align: center; flex-grow: 1; flex-shrink: 1; flex-basis: 0; -webkit-box-flex: 1; &:active { background-color: darken(#ccc, 10%); } } .tab-top { width: 50px; margin: 0 1%; background: #cccdd0; color: #fff; font-size: 24px; } .key-delete { width: 50px; margin: 0 1%; color: #827f7f; background: #d7d7d8; } .tab-num { font-size: 18px; background: #dedede; color: #5a5959; } .tab-point { width: 20px; } .tab-blank { width: 80px; font-size: 12px; padding: 0 15px; color: #5a5959; line-height: 60px; } .tab-symbol { width: 20px; font-size: 18px; } .tab-enter { font-size: 30px; line-height: 54px; } &:nth-child(2) { width: 88%; } } } </style>
KeyInput.vue
<template> <p> <input type="text" ref="keyboard" v-model="inputValue" @focus="onFocus"> <Keyboard :option="option" @keyVal="getInputValue" @close="option.show = false"></Keyboard> </p> </template> <script> import Keyboard from '../components/Keyboard' export default { components: { Keyboard }, data() { return { option: { show: false, sourceDom: '' }, inputValue: '' } }, props: {}, created() {}, methods: { getInputValue(val) { if(val === 'delete'){ this.inputValue = this.inputValue.slice(0,this.inputValue.length -1) }else{ this.inputValue += val } }, onFocus() { this.$set(this.option, 'show', true) this.$set(this.option, 'sourceDom', this.$refs['keyboard']) }, //获取光标位置 getCursorPosition() { let doc = this.$refs['keyboard'] if (doc.selectionStart) return doc.selectionStart return -1 }, //设置光标位置 暂未实现 setCursorPosition(pos) { let doc = this.$refs['keyboard'] console.log(doc.setSelectionRange) doc.focus() doc.setSelectionRange(1,3) } } } </script> <style lang="less" scoped> </style>
Demo 사용
<template> <p> <key-input class="demo-class"></key-input> </p> </template> <script> import KeyInput from '../components/KeyInput' export default { components: { KeyInput }, data() { return { } }, created() {}, methods: { } } </script> <style lang="less"> body{ background: #efefef; } .demo-class{ input{ border:1px solid #ccc; outline: none; height: 30px; font-size: 16px; letter-spacing: 2px; padding: 0 5px; } } </style>
위 내용은 제가 모든 사람을 위해 편집한 내용입니다. 앞으로 모든 사람에게 도움이 되기를 바랍니다.
관련 기사:
js와 jQuery를 사용하여 지정된 할당 방법을 구현하는 방법
Vue를 사용하여 인터셉터를 구현하는 방법과 토큰 처리 방법은 무엇입니까?
React 및 Webpack을 사용하여 패키징을 최적화하는 방법은 무엇입니까?
vue를 사용하여 todolist 구성 요소를 작성하는 방법은 무엇입니까?
openlayers4를 사용하여 점 확산을 달성하는 방법은 무엇입니까?
webpack에서 eslint 구성 사용(자세한 튜토리얼)
위 내용은 vue를 사용하여 간단한 키보드 작업 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











vue.js에서 bootstrap 사용은 5 단계로 나뉩니다 : Bootstrap 설치. main.js.의 부트 스트랩 가져 오기 부트 스트랩 구성 요소를 템플릿에서 직접 사용하십시오. 선택 사항 : 사용자 정의 스타일. 선택 사항 : 플러그인을 사용하십시오.

HTML 템플릿의 버튼을 메소드에 바인딩하여 VUE 버튼에 함수를 추가 할 수 있습니다. 메소드를 정의하고 VUE 인스턴스에서 기능 로직을 작성하십시오.

vue.js에서 JS 파일을 참조하는 세 가지 방법이 있습니다. & lt; script & gt; 꼬리표;; mounted () 라이프 사이클 후크를 사용한 동적 가져 오기; Vuex State Management Library를 통해 수입.

vue.js의 시계 옵션을 사용하면 개발자가 특정 데이터의 변경 사항을들을 수 있습니다. 데이터가 변경되면 콜백 기능을 트리거하여 업데이트보기 또는 기타 작업을 수행합니다. 구성 옵션에는 즉시 콜백을 실행할지 여부와 DEEP를 지정하는 즉시 포함되며, 이는 객체 또는 어레이에 대한 변경 사항을 재귀 적으로 듣는 지 여부를 지정합니다.

VUE 멀티 페이지 개발은 vue.js 프레임 워크를 사용하여 응용 프로그램을 구축하는 방법입니다. 여기서 응용 프로그램은 별도의 페이지로 나뉩니다. 코드 유지 보수 : 응용 프로그램을 여러 페이지로 분할하면 코드를보다 쉽게 관리하고 유지 관리 할 수 있습니다. 모듈 식 : 각 페이지는 쉬운 재사용 및 교체를 위해 별도의 모듈로 사용할 수 있습니다. 간단한 라우팅 : 페이지 간의 탐색은 간단한 라우팅 구성을 통해 관리 할 수 있습니다. SEO 최적화 : 각 페이지에는 자체 URL이있어 SEO가 도움이됩니다.

vue.js는 이전 페이지로 돌아갈 수있는 네 가지 방법이 있습니다. $ router.go (-1) $ router.back () 사용 & lt; router-link to = & quot;/quot; Component Window.history.back () 및 메소드 선택은 장면에 따라 다릅니다.

vue.js가 트래버스 어레이 및 객체에 대한 세 가지 일반적인 방법이 있습니다. V- 결합 지시문은 V-FOR와 함께 사용하여 각 요소의 속성 값을 동적으로 설정할 수 있습니다. .MAP 메소드는 배열 요소를 새 배열로 변환 할 수 있습니다.

VUE에서 태그의 점프를 구현하는 방법에는 다음이 포함됩니다. HTML 템플릿의 A 태그를 사용하여 HREF 속성을 지정합니다. VUE 라우팅의 라우터 링크 구성 요소를 사용하십시오. javaScript 에서이. $ router.push () 메소드를 사용하십시오. 매개 변수는 쿼리 매개 변수를 통해 전달 될 수 있으며 동적 점프를 위해 라우터 옵션에서 경로가 구성됩니다.
