用div+css实现聊天输入框,可输入、展示图片

复制代码

 

 


<div class="dzm-textarea" id="dzm-textarea" contenteditable="true" @keypress="onKeypress" placeholder="输入文本" style="overflow: auto;"></div>
复制代码
复制代码
const onKeypress = (e)=> {
  if (e.key === 'Enter') {
    if (e.shiftKey) {
      // Shift+Enter 按下,执行换行
      initRange()
    } else {
      // 单纯的 Enter 按下,执行发送
      handleSend()
    }
    e.preventDefault()
  }
}
const initRange = ()=> {
  const inputDiv = document.querySelector("#dzm-textarea");
  inputDiv.innerText += "\n";
  const lastChild = inputDiv.lastChild;
  lastChild.innerText += "\n";
  const selection = window.getSelection();
  // 创建range对象
  const range = document.createRange();
  // 设置range的起始位置为div内容的第3个字符之后
  range.setStart(lastChild, lastChild.length);
  // 将range设置为选区
  selection.removeAllRanges();
  selection.addRange(range);
}
复制代码
复制代码
/* 输入框 */
.dzm-textarea {
flex: 1;
padding: 5px;
font-size: 14px;
max-height: 62px;
//border: 1px solid red;
}
/* 输入框为空时显示 placeholder */
.dzm-textarea:empty:before {
content: attr(placeholder);
color: rgb(200,200,200);
}
/* 输入框获取焦点时移除 placeholder */
.dzm-textarea:focus:before {
content: none;
}
复制代码
posted @   尹言覃少  阅读(422)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示