I encountered a requirement to customize shortcut keys on the page, which required a place to set and display shortcut keys. I looked around Element UI and found that there was no component that could be used with slight modifications, so I wrote one myself. The effect is as follows: Key PointsAlthough it looks like an Input, it actually displays a label effect inside the component and also requires a delete button. This requires putting the HTML code in the input box. The browser's Input component is obviously not suitable, so you can only imitate the effect of an Input component yourself. focus, blur, selected highlight effectsNon-Input components do not have effects such as focus, blur, and selected highlight effects. Fortunately, browsers have reserved implementation methods, and netizens have already provided solutions online. By adding the tabindex="0" attribute to the div, the div can obtain these effects. The tabindex attribute specifies the order of the Tab keys. If you write 0, the components will be selected in the default order. If you write -1, they will never be selected. Because it is in the form of an Input component, it is reasonable that it can be obtained by Tab. Then add CSS to get the focus border effect, and move the mouse to display the text type pointer .shortcut-key-input { cursor: text; transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1); } .shortcut-key-input:focus { border-color: #188cff; box-shadow: 0 0 4px rgba(24, 140, 255, 0.38); } Text promptsWhen there is no content, it needs to be the same as Input, and a text prompt can be displayed by default. This also puts a div inside and controls it with Vue. If the output label variable has data, this element will not be displayed. Cursor flashing effectThis is relatively easy to handle. Put a pseudo-element in the Input class, add this pseudo-element when the focus is acquired, and then give this element a CSS3 animation, and you will have the effect of a flashing cursor. @keyframes Blink { 0% { opacity: 0; } 100% { opacity: 1; } } .shortcut-key-input.cursor::after { content: "|"; animation: Blink 1.2s ease 0s infinite; font-size: 18px; position: absolute; top: 1px; left: 8px; } Key capture Key capture mainly relies on the keydown event. The returned event will mark whether alt, ctrl (control) and other information are pressed, so key combination can be achieved by relying on this information. handleKeydown(e) { const { altKey, ctrlKey, shiftKey, key, code } = e; if (!CODE_CONTROL.includes(key)) { if (!this.keyRange.includes(code)) return; let controlKey = ""; [ { key: altKey, text: "Alt" }, { key: ctrlKey, text: "Ctrl" }, { key: shiftKey, text: "Shift" } ].forEach(curKey => { if (curKey.key) { if (controlKey) controlKey += "+"; controlKey += curKey.text; } }); if (key) { if (controlKey) controlKey += "+"; controlKey += key.toUpperCase(); } this.addHotkey({ text: controlKey, controlKey: { altKey, ctrlKey, shiftKey, key, code } }); } e.preventDefault(); }, CODE_CONTROL is another preset key code set for easy processing. Originally keyCode was used, but keyCode has been deprecated and code is recommended. addHotkey(data) { if (this.list.length && this.list.some(item => data.text === item.text)) return; if (this.list.length && this.list.length.toString() === this.max.toString()) return; if (!this.verify(data)) return; this.list.push(data); } Online Previewhttps://codesandbox.io/s/vue-hotkeyinput-90m2k The above is the details of Vue's implementation of an Input component that obtains the effect of a key display shortcut. For more information about Vue's display shortcut keys, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Vue uses vue-quill-editor rich text editor and uploads pictures to the server
>>: How are Vue components parsed and rendered?
Enter Alibaba vector icon library Alibaba Vector ...
Result:Implementation code: <!DOCTYPE html>...
The interviewer will sometimes ask you, tell me h...
This article is original by 123WORDPRESS.COM Ligh...
Table of contents Table definition auto-increment...
We all know that Jmeter provides native result vi...
There are two main reasons why it is difficult to...
1. Install Apache $ sudo apt update && su...
1. Pull the image docker pull registry.cn-hangzho...
This article shares the specific code for impleme...
This article shares the specific code of a simple...
Table of contents Preface Installation and Config...
1. Installation Package MYSQL service download ad...
question: <input type="hidden" name=...
1. Apache 2.4.41 installation and configuration T...