【element ui】 el-tooltip 样式修改

本文介绍了如何在Element UI中自定义el-tooltip组件的样式,包括默认悬浮显示样式和自定义背景色及箭头颜色的方法,并提醒在设置CSS时注意避免污染全局样式。

今天是2021年的第201天,更一篇吧。否则2021年的博客就真的寸草不生了。


前言

最近使用到el-tooltip组件和el-popper组件,但是自定义背景色时痛苦难受。毕竟与el-select还是有些不同。

因此,特来记录。


一、el-tooltip组件

该组件常用于展示鼠标 hover 时的提示信息。基本使用方式见 element ui官网

二、自定义样式

1.默认悬浮显示样式

  • 文字提示的默认样式:

  • 默认样式对应的代码:
<template>
  <div id="app">
    <!-- <img alt="Vue logo" src="./assets/logo.png" /> -->
    <div style="background-color: black; height: 200px">
      <el-tooltip
        class="item"
        effect="light"
        content="Bottom Left 提示文字"
        placement="bottom-start"
        :manual="manual"
      >
        <el-button type="success">成功按钮</el-button>
      </el-tooltip>
    </div>
  </div>
</template>

2.自定义样式

  • 背景色设置黄色 && 箭头设置绿色:
  •  代码示例
<template>
  <div id="app">
    <!-- <img alt="Vue logo" src="./assets/logo.png" /> -->
    <div style="background-color: black; height: 200px">
      <el-tooltip
        class="item"
        effect="light"
        content="Bottom Left 提示文字"
        placement="bottom"
        popper-class="tip-class"
      >
        <el-button type="success">成功按钮</el-button>
      </el-tooltip>
    </div>
    
  </div>
</template>
<script>


<style>

div.tip-class {
  padding: 1.5rem !important;
  max-width: 30% !important;
  background-color: #f5560c !important;/*设置矩形框的背景颜色*/
}
/* [x-placement^='A'] A指的是el-tooltip标签中的placement的值,此处是bottom。猜测用于动态设置border-A-color。*/
.el-tooltip__popper[x-placement^='bottom'].tip-class .popper__arrow::after {
  border-bottom-color: #05e205 !important;/*设置指向箭头的颜色*/
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

该处使用的url网络请求的数据。


说明

此处需要说明,在style中没有使用scoped属性,存在可能污染全局css的风险。因此,在设置css样式时,使用了.tip-class 进行标签过滤。

OK。

bye~

下次随缘~
 

### 实现 Element UIel-table 整行触发 el-tooltip 的方法 在 Element UI 中,`el-table` 是一个强大的表格组件,而 `el-tooltip` 则用于显示提示信息。为了实现整行触发 `el-tooltip` 的效果,可以通过自定义表格单元格的渲染逻辑来完成。以下是具体的实现方式: #### 1. 使用 `slot-scope` 自定义单元格内容 通过 `el-table-column` 的 `scoped-slot` 功能,可以为每个单元格设置自定义内容,并将 `el-tooltip` 包裹在单元格内容外部[^2]。 ```vue <template> <el-table :data="tableData" @cell-mouse-enter="handleCellEnter" @cell-mouse-leave="handleCellLeave"> <el-table-column v-for="(column, index) in tableLabel" :key="index" :label="column.label" :prop="column.prop"> <template slot-scope="scope"> <el-tooltip :content="tooltipContent(scope.row)" placement="top" :visible-arrow="false" :open-delay="300"> <span>{{ scope.row[column.prop] }}</span> </el-tooltip> </template> </el-table-column> </el-table> </template> ``` #### 2. 动态设置 Tooltip 内容 通过 `tooltipContent` 方法动态生成每一行的提示信息。可以根据需求拼接整行数据作为提示内容[^3]。 ```javascript export default { data() { return { tableData: [ { name: &#39;张三&#39;, age: 25, address: &#39;北京市&#39; }, { name: &#39;李四&#39;, age: 30, address: &#39;上海市&#39; } ], tableLabel: [ { label: &#39;姓名&#39;, prop: &#39;name&#39; }, { label: &#39;年龄&#39;, prop: &#39;age&#39; }, { label: &#39;地址&#39;, prop: &#39;address&#39; } ], currentTooltipRow: null }; }, methods: { tooltipContent(row) { return `姓名:${row.name},年龄:${row.age},地址:${row.address}`; }, handleCellEnter(row) { this.currentTooltipRow = row; }, handleCellLeave() { this.currentTooltipRow = null; } } }; ``` #### 3. 监听鼠标事件 为了实现整行触发的效果,需要监听 `@cell-mouse-enter` 和 `@cell-mouse-leave` 事件。当鼠标进入某一行时,动态更新当前行的数据,并将其传递给 `el-tooltip` 的 `content` 属性[^4]。 #### 4. 样式优化 为了确保用户体验良好,可以通过 CSS 调整 `el-tooltip` 的样式,例如设置延迟显示时间、隐藏箭头等[^5]。 ```css /* 隐藏 Tooltip 箭头 */ .el-tooltip__popper[x-placement^="top"] .popper__arrow, .el-tooltip__popper[x-placement^="top"] .popper__arrow::after { display: none; } ``` ### 注意事项 - 如果表格数据量较大,建议优化性能,避免频繁触发事件导致页面卡顿。 - 可以根据实际需求调整 `el-tooltip` 的 `placement` 属性(如 top、bottom、left、right)以适应不同的布局场景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值