首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在keyDown事件上调整Vue.js文本区域的大小,如何获取样式属性?

在keyDown事件上调整Vue.js文本区域的大小,如何获取样式属性?
EN

Stack Overflow用户
提问于 2018-06-06 23:22:17
回答 1查看 983关注 0票数 0

我想复制Vue.js Textarea组件的autosize library。我发现我无法获得以下几个属性的值:boxSizingpaddingToppaddingBottomborderTopWidthborderBottomWidth to calculate heightOffset

代码语言:javascript
复制
const style = window.getComputedStyle(this.$refs.textarea, null);
if (style.boxSizing === 'content-box') {
  heightOffset = -(parseFloat(style.paddingTop + parseFloat(style.paddingBottom));
} else {
  heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
}

模板:

代码语言:javascript
复制
<textarea 
  class="answer" 
  ref="textarea"
  v-bind:placeholder="placeholder" 
  v-on:keydown="setHeight"
  v-bind:style="{ height: areaHeight + 'px' }"
>
</textarea>

在组件中:

代码语言:javascript
复制
data: function () {
  return {
    placeholder: "Your answer",
      areaHeight: 24
  }
},
methods: {
  setHeight: function(e) {
    var ta = this.$refs.textarea;

    console.log("ta.paddingTop: " + ta.paddingTop); //undefined
    console.log("ta.style.paddingTop: " + ta.style.paddingTop); //""
    console.log("ta.paddingBottom: " + ta.paddingBottom); //undefined
    console.log("ta.style.paddingBottom: " + ta.style.paddingBottom); //""

    console.log("ta.borderTopWidth: " + ta.borderTopWidth); //undefined
    console.log("ta.style.borderTopWidth: " + ta.style.borderTopWidth); //""
    console.log("ta.borderBottomWidth: " + ta.borderBottomWidth); //undefined
    console.log("ta.style.borderBottomWidth: " + ta.style.borderBottomWidth); //""

    this.areaHeight = this.$refs.textarea.scrollHeight;

    if (e.keyCode === 13) {
      this.areaHeight += 24;
    }
    if (e.keyCode === 8) {
      this.areaHeight -= 24;
      if(this.areaHeight <= 0) this.areaHeight = 24;
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50724053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档