请问各位大神怎么实现这个效果, 输入长度变长时 自动伸缩输入框,超出行宽时自动换行
vue
import Vue from "vue";
export const autoTextarea = Vue.directive("autoTextarea", {
inserted: (el, binding) => {
const change = () => {
const value = el.value;
// 内容长度不变不做处理
if (el._length === value.length) return;
el._length = value.length;
let height;
// textare单行高度
let minHeight = binding.value ? binding.value.minHeight : 22;
const style = el.style;
const padding =
parseInt(getComputedStyle(el)["paddingTop"]) +
parseInt(getComputedStyle(el)["paddingBottom"]);
style.overflowY = "hidden";
style.height = `${minHeight}px`;
if (el.scrollHeight > minHeight) {
height = el.scrollHeight - padding;
style.height = `${height}px`;
}
};
el.addEventListener("input", change);
el.addEventListener("change", change);
el.addEventListener("focus", change);
el.addEventListener("blur", change);
},
});
//main.js
import { autoTextarea } from "./utils/input";
Vue.prototype.$autoTextarea = autoTextarea;
<template>
<div class="text">
<textarea class="textarea" v-autoTextarea="{minHeight: 36}"></textarea>
</div>
</template>
<style scoped lang="scss">
.textarea {
width: 240px;
height: 36px;
line-height: 28px;
overflow: hidden;
resize: none;
outline-color: #ccc; // 鼠标聚焦边框颜色
border-color: #ccc;
border-radius: 5px;
padding-left: 6px;
}
.textarea:focus {//去除聚焦效果
outline: none;
}
</style>
//信息发送后输入框聚焦
8 回答4.7k 阅读✓ 已解决
6 回答3.5k 阅读✓ 已解决
5 回答2.8k 阅读✓ 已解决
6 回答2.4k 阅读
5 回答6.4k 阅读✓ 已解决
4 回答2.3k 阅读✓ 已解决
5 回答1.3k 阅读✓ 已解决
找到了一个方案,主要依靠 div的。contenteditable="true"