Created
November 1, 2018 04:35
-
-
Save peterkimzz/cbd390c82d6a62c776cacfe2dd34a247 to your computer and use it in GitHub Desktop.
Vuejs 자동으로 높이를 조절하는 Textarea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <textarea | |
| id="textarea" | |
| :placeholder="placeholder" | |
| :value="value" | |
| name="textarea" | |
| @input="input($event.target.value)"/> | |
| </template> | |
| <script> | |
| export default { | |
| props: { | |
| value: { | |
| type: String, | |
| default: '' | |
| }, | |
| placeholder: { | |
| type: String, | |
| default: '' | |
| }, | |
| autoresize: { | |
| type: [Boolean, String], | |
| default: false | |
| } | |
| }, | |
| computed: { | |
| $textarea() { | |
| return document.getElementById('textarea') | |
| } | |
| }, | |
| mounted() { | |
| this.init() | |
| }, | |
| methods: { | |
| init() { | |
| this.resize() | |
| }, | |
| input(value) { | |
| this.$emit('input', value) | |
| this.resize() | |
| }, | |
| resize($event) { | |
| if (!this.autoresize) return | |
| this.$textarea.style.height = '1px' | |
| this.$textarea.style.height = `${this.$textarea.scrollHeight + 12}px` | |
| } | |
| } | |
| } | |
| </script> | |
| <style lang="scss" scoped> | |
| @import '~assets/scss/index'; | |
| #textarea { | |
| min-height: 50px; | |
| } | |
| </style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment