input框超出输入区域显示...和tooltip

2024-07-18  本文已影响0人  团猫咪爱吃玉米
   <el-tooltip placement="top" effect="dark" :content="inputValue" >
        <el-input 
           v-model="inputValue" 
           @mouseover="handleMouseover"
           @mouseout="handleMouseout" 
           @focus="focus"
           placeholder="Please input" 
        />
    </el-tooltip>
        
    handleMouseover(e) {
      if (this.isOverflow(e.target)) {
        this.toolTipVisible = true;
      }
    },
    handleMouseout() {
      this.toolTipVisible = false;
    },
    focus() {
      this.toolTipVisible = false;
    },
    isOverflow(target) {
      const inputWidth = target.offsetWidth;
      const spanEl = document.createElement("span");
      spanEl.style.visibility = "hidden";
      spanEl.style.whiteSpace = "nowrap";
      spanEl.style.position = "absolute";
      spanEl.style.left = "-9999px";
      spanEl.textContent = this.inputValue;
      document.body.appendChild(spanEl);
      const contentWidth = spanEl.offsetWidth;
      document.body.removeChild(spanEl);
      return inputWidth < contentWidth;
    },
:deep(.el-input__inner)[type="text"] {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    cursor: pointer;
}
上一篇 下一篇

猜你喜欢

热点阅读