【vue】el-select选择器实现宽度自适应

        

选择器的宽度根据内容长度进行变化

<div class="Space_content">
    <el-select
            v-model="value"
            :placeholder="$t('bot.roommessage')"
            class="select"
            size="small"
            style="margin-right: 10px"
            @change="selectchange"
          >
            <template slot="prefix">
              <span style="margin-right: 10px; font-weight: 600">
                {{ $t("bot.state") }}:</span
              >
              <span style="visibility: hidden">
                {{ (options.find((s) => s.value === value) || {}).label }}</span
              >
            </template>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
</div>
<style lang="scss">
  .Space_content {
  .select {
    min-width: 100px;
    .el-input__inner {
      text-align: right;
      border: none;
      background-color: transparent;
      font-weight: 600;
      padding-left: 15px;
      padding-right: 25px;
    }
    &:hover {
      background-color: rgba(46, 46, 56, 0.08);
      border-radius: 5px;
      cursor: pointer;
    }
    .el-icon-arrow-up:before {
      content: "\e78f";
      color: #000;
    }
    .el-input__suffix {
      top: -2px;
      right: 0;
    }
    .el-input__prefix {
      position: relative;
      left: 0px;
      box-sizing: border-box;
      // border: 1px solid #ffffff00;
      width: auto;
      padding: 0 30px 0 10px;
      height: 32px;
      line-height: 35px;
      // visibility: hidden;
    }
    input {
      position: absolute;
    }
  }
  .el-input {
    width: auto !important;
  }
}
</style>

其中css中的.el-input { width: auto !important; },是因为选择器后面还有一个搜索框,如果不写这个,会出现问题。

其中$t("bot.state")是用国际化的方式呈现的,固定的内容,如果你没有就把他去掉就行,下面是去掉的样子

<div class="Space_content">
    <el-select
            v-model="value"
            :placeholder="$t('bot.roommessage')"
            class="select"
            size="small"
            style="margin-right: 10px"
            @change="selectchange"
          >
            <template slot="prefix">
                {{ (options.find((s) => s.value === value) || {}).label }}
            </template>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
</div>
<style lang="scss">
  .Space_content {
  .select {
    min-width: 100px;
    .el-input__inner {
      border: none;
      background-color: transparent;
      font-weight: 600;
      padding-left: 15px;
      padding-right: 20px;
    }
    &:hover {
      background-color: rgba(46, 46, 56, 0.08);
      border-radius: 5px;
      cursor: pointer;
    }
    .el-icon-arrow-up:before {
      content: "\e78f";
      color: #000;
    }
    .el-input__suffix {
      top: -2px;
      right: 0;
    }
    .el-input__prefix {
      position: relative;
      left: 0px;
      box-sizing: border-box;
      padding: 0 30px;
      height: 32px;
      line-height: 35px;
      visibility: hidden;
    }
    input {
      position: absolute;
    }
  }
  .el-input {
    width: auto !important;
  }
}
</style>

相关推荐

  1. uniapp app 实现适应宽度 input

    2024-05-26 01:36:41       55 阅读
  2. el-dialog宽度适应

    2024-05-26 01:36:41       44 阅读
  3. Unity InputField宽度适应内容

    2024-05-26 01:36:41       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-26 01:36:41       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-26 01:36:41       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-26 01:36:41       82 阅读
  4. Python语言-面向对象

    2024-05-26 01:36:41       91 阅读

热门阅读

  1. 169. 多数元素

    2024-05-26 01:36:41       35 阅读
  2. 15、Go Gin常见响应返回详解

    2024-05-26 01:36:41       37 阅读
  3. 掌握C++回调:按值捕获、按引用捕获与弱引用

    2024-05-26 01:36:41       34 阅读
  4. 【数据结构与算法 | 基础篇】数组模拟栈

    2024-05-26 01:36:41       36 阅读
  5. 银发经济:老龄化社会中的机遇与挑战

    2024-05-26 01:36:41       36 阅读
  6. 基于Amazon Cognito的安全登录与资源访问

    2024-05-26 01:36:41       28 阅读
  7. ORACLE 6节点组成的ACFS文件系统异常的分析思路

    2024-05-26 01:36:41       27 阅读
  8. Nginx 从入门到精通-Nginx-Web服务器的瑞士军刀

    2024-05-26 01:36:41       32 阅读
  9. PostgreSQL入门教程

    2024-05-26 01:36:41       29 阅读
  10. 系统分析师-案例分析-数据库

    2024-05-26 01:36:41       38 阅读