前端组件库小知识-element

60 阅读1分钟

theme: github

---

1、只读输入框添加清除按钮

/**  js **/
  <el-input v-model="formData.codePartgroup" icon="el-icon-more" size="mini" readonly clear @dblclick.native="selectPart">
       <span v-show="formData.codePartgroup" slot="suffix" @click="()=>{formData.codePartgroup = '' }">
              <i class="el-icon-close" style="margin-left: 5px;cursor: pointer;" />
       </span>
</el-input>
/**  css **/
::v-deep{
 .el-icon-close:before {
   content: "\E78D";
 }
 .el-input__suffix{
  display: none;
 }
 .el-input:hover {
    .el-input__suffix{
      display: block;
    }
 }
}

2、css实现form表单一行显示多个

/**  css **/
::v-deep{
   .el-form.demo-ruleForm {
      display: flex;
      flex-wrap: wrap;
  }
}
/**  template 中给指定表单设置百分比即可 **/
  <el-form-item label="规则名称" prop="name" style="width:50%">
       <el-input v-model="formData.name" />
   </el-form-item>

3、文字超出省略号显示且鼠标放上提示

**注:此代码设置了el-tooltip提示组件设置为不在最外层body ***

<template>
  <div class="text-over-tooltip-components">
    <el-tooltip
      ref="mypop"
      placement="top"
      :effect="effect"
      :visible-arrow="true"
      :disabled="isDisabledTooltip"
      :append-to-body="false"
      :content="content"
    >
      <div class="ellipsis" :class="className" @mouseover="onMouseOver(refName)">
        <span :ref="refName">{{ content }}</span>
      </div>
    </el-tooltip>

    <!-- tooltip出现dom位置 -->
    <div ref="here" />
  </div>
</template>

<script>
export default {
  name: 'TextOverTooltip',
  props: {
    // 显示的文字内容
    content: { type: String, default: '' },
    // 设置父元素的样式:比如宽度字体等,需可以自己在组件内部配置样式比如字体大小20:fs20
    className: { type: String, default: '' },
    // 子元素标识(如在同一页面中调用多次组件,此参数不可重复)
    refName: { type: String, default: '' },
    // 默认提供的主题 dark/light
    effect: {
      type: String,
      default: () => {
        return 'dark'
      }
    },
    // Tooltip 的出现位置top/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-end
    placement: {
      type: String,
      default: () => {
        return 'top'
      }
    }
  },
  data() {
    return {
      isDisabledTooltip: true // 是否需要禁止提示
    }
  },
  mounted() {
    this.$refs.here.appendChild(
      this.$refs.mypop.popperVM.$el
    )
  },
  methods: {
    // 移入事件: 判断内容的宽度contentWidth是否大于父级的宽度
    onMouseOver(str) {
      const parentWidth = this.$refs[str].parentNode.offsetWidth
      const contentWidth = this.$refs[str].offsetWidth
      // 判断是否禁用tooltip功能
      this.isDisabledTooltip = contentWidth <= parentWidth
    }
  }
}
</script>

<style lang="scss" scoped>
.text-over-tooltip-components{
  /* 文字超出宽度显示省略号 单行 */
  .ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap
  }
  /* 自定义样式 */
  .fs20 {
    font-size: 20px;
    font-weight: 600;
    color: green;
    line-height: 22px;
  }
  .fs12 {
    font-size: 12px;
    font-weight: 400;
    color: orange;
    line-height: 14px;
  }
}
::v-deep{
.el-tooltip{
  margin-right: 10px;
}
}
</style>