Vue2文字提示(Tooltip)

简介: 这篇文章介绍了如何在Vue 3框架中创建一个文字提示组件(Tooltip),允许自定义提示框的最大宽度、展示文本和提示文本,支持鼠标悬停显示和隐藏效果。

可自定义设置以下属性:

  • 提示框内容最大宽度(maxWidth),类型:number,默认 120px

  • 展示的文本(content),类型:string,默认暂无内容,支持string | slot

  • 提示的文本(title),类型:string,默认暂无提示,支持string | slot

效果如下图:

①创建提示框组件Tooltip.vue:

<template>
  <div class="m-tooltip" @mouseenter="onShow" @mouseleave="onHide">
    <div
      ref="titleRef"
      class="m-title"
      :class="{'show-tip': visible}"
      @mouseenter="onShow"
      @mouseleave="onHide"
      :style="`max-width: ${maxWidth}px; top: ${top}px; left: ${left}px;`">
      <div class="u-title">
        <slot name="title">{
  
  { title }}</slot>
      </div>
      <div class="m-arrow">
        <span class="u-arrow"></span>
      </div>
    </div>
    <div ref="contentRef" class="u-content">
      <slot>{
  
  { content }}</slot>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Tooltip',
  props: {
    maxWidth: { // 提示框内容最大宽度
      type: Number,
      default: 120
    },
    content: { // 展示的文本
      type: String,
      default: '暂无内容' // string | slot
    },
    title: { // 提示的文本
      type: String,
      default: '暂无提示' // string | slot
    }
  },
  data () {
    return {
      hideTimer: null,
      top: 0, // 提示框top定位
      left: 0, // 提示框left定位
      visible: false
    }
  },
  mounted () {
    this.getPosition()
  },
  methods: {
    getPosition () {
      const rect = this.$refs.contentRef.getBoundingClientRect()
      const targetTop = rect.top
      const targetLeft = rect.left
      const targetWidth = rect.width
      const titleWidth = this.$refs.titleRef.offsetWidth // 提示文本宽度
      const titleHeight = this.$refs.titleRef.offsetHeight // 提示文本高度
      this.top = targetTop - titleHeight
      this.left = targetLeft - (titleWidth - targetWidth) / 2
    },
    onShow () {
      this.getPosition()
      clearTimeout(this.hideTimer)
      this.visible = true
    },
    onHide () {
      this.hideTimer = setTimeout(() => {
        this.visible = false
      }, 100)
    }
  }
}
</script>
<style lang="less" scoped>
.m-tooltip {
  display: inline-block;
}
.m-title {
  position: fixed;
  z-index: 999;
  padding-bottom: 12px;
  pointer-events: none;
  opacity: 0;
  transform-origin: 50% 75%;
  transform: scale(0.8);
  transition: transform .25s, opacity .25s;
  .u-title {
    padding: 10px;
    margin: 0 auto;
    word-break: break-all;
    word-wrap: break-word;
    background: #FFFFFF;
    box-shadow: 0px 0px 7px 1px fade(@themeColor, 36%);
    border-radius: 5px;
    font-weight: 400;
    font-size: 14px;
    line-height: 20px;
    color: #333;
  }
  .m-arrow {
    position: absolute;
    z-index: 9;
    bottom: 12px;
    left: 50%;
    transform: translate(-50%, 100%);
    width: 15.55px;
    height: 10px;
    border-radius: 0 0 5px 5px;
    overflow: hidden;
    .u-arrow {
      position: absolute;
      left: 50%;
      top: 0px;
      transform: translate(-50%, -50%) rotate(45deg);
      margin: 0 auto;
      width: 11px;
      height: 11px;
      background: #FFF;
      border-radius: 0 0 3px 0;
      z-index: 8;
      box-shadow: 1px 1px 3px 1px fade(@themeColor, 10%);
    }
  }
}
.show-tip {
  pointer-events: auto;
  opacity: 1;
  transform: scale(1);
}
.u-content {
  cursor: pointer;
  font-size: 14px;
  line-height: 1.5;
  color: #000000;
}
</style>

②在要使用的页面引入:

<template>
  <div>
    <p class="u-info mt150 mb10">普通用法:</p>
    <Tooltip :maxWidth="240">
      <template #title>特斯拉(Tesla)是美国一家电动汽车及能源公司,总部位于帕洛阿托(Palo Alto),市值达2100亿美元,产销电动汽车、太阳能板、及储能设备</template>
      <div class="u-tag yellow">特斯拉</div>
    </Tooltip>
    <p class="u-info ml120 mb10">模拟接口调用:</p>
    <Tooltip :maxWidth="240">
      <!-- <template #title>《哥斯拉大战金刚》是由美国传奇影业公司出品,亚当·温佳德执导,亚历山大·斯卡斯加德、米莉·博比·布朗、丽贝卡·豪尔、凯莉·霍特尔、布莱恩·泰里·亨利、小栗旬联合主演的动作科幻片,于2021于3月26日在中国内地上映</template> -->
      <template #title>{
  
  { title || '--' }}</template>
      <!-- <div class="u-tag blue">哥斯拉</div> -->
      <div class="u-tag blue">{
  
  { content || '--' }}</div>
    </Tooltip>
  </div>
</template>
<script>
import Tooltip from './Tooltip'
export default {
  name: 'InfoTip',
  components: {
    Tooltip
  },
  data () {
    return {
      content: '',
      title: ''
    }
  },
  mounted () {
    setTimeout(() => { // 模拟接口调用
      this.title = '《哥斯拉大战金刚》是由美国传奇影业公司出品,亚当·温佳德执导,亚历山大·斯卡斯加德、米莉·博比·布朗、丽贝卡·豪尔、凯莉·霍特尔、布莱恩·泰里·亨利、小栗旬联合主演的动作科幻片,于2021于3月26日在中国内地上映'
      this.content = '哥斯拉'
    }, 1000)
  }
}

</script>
<style lang="less" scoped>
.u-tag {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 15px;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  cursor: pointer;
}
.u-info {
  display: inline-block;
  font-size: 16px;
}
.mt150 {
  margin-top: 150px;
}
.yellow {
  color: #FF9000;
  background: #FFF9F0;
}
.blue {
  color: #0079DD;
  background: #F0F7FD;
}
</style>
相关文章
|
前端开发 JavaScript 安全
【前端相关】elementui使用el-upload组件实现自定义上传
【前端相关】elementui使用el-upload组件实现自定义上传
3955 0
|
算法
经典控制算法——PID算法原理分析及优化
这篇文章介绍了PID控制算法,这是一种广泛应用的控制策略,具有简单、鲁棒性强的特点。PID通过比例、积分和微分三个部分调整控制量,以减少系统误差。文章提到了在大学智能汽车竞赛中的应用,并详细解释了PID的基本原理和数学表达式。接着,讨论了数字PID的实现,包括位置式、增量式和步进式,以及它们各自的优缺点。最后,文章介绍了PID的优化方法,如积分饱和处理和微分项优化,以及串级PID在电机控制中的应用。整个内容旨在帮助读者理解PID控制的原理和实际运用。
3320 1
|
移动开发 小程序 前端开发
|
JSON 算法 fastjson
com.alibaba.fastjson转换JSONObject数据后顺序与原JSON字符串顺序不一致原因分析
Json字符串转JSONObject对象保证属性及其内部JSONObject有序(本身顺序而非需要指定排序)
4398 1
|
JavaScript 前端开发
我为展开收起功能做了动画,被老板称赞!
【8月更文挑战第23天】我为展开收起功能做了动画,被老板称赞!
771 1
我为展开收起功能做了动画,被老板称赞!
|
资源调度 开发者
Vue2选择器(Select)
这是一个基于Vue3的选择器组件(VueAmazingSelector),已在npm上发布,方便快捷地通过`yarn add vue-amazing-selector`进行安装。此组件允许全局或局部注册,并提供了丰富的自定义属性,如选项数据、选择器文本字段名、值字段名、默认文字、禁用状态、清除功能等。同时,它还支持调整选择框的尺寸和下拉项数量,并附有详细的使用示例和代码片段,便于开发者快速集成和定制。
418 1
Vue2选择器(Select)
Vue3文字提示(Tooltip)
这是一篇关于 Vue2 文字提示(Tooltip)组件的教程,支持多种自定义属性,如最大宽度、展示文本、提示文本、样式、背景色、箭头显示等,并提供了在线预览示例。组件通过监听插槽和 `requestAnimationFrame` 实现了延迟显示与隐藏效果。文章详细介绍了组件实现代码及在页面中的使用方法。
985 0
Vue3文字提示(Tooltip)
【1】ElementUI 组件实际应用===》按钮的使用
这篇文章详细介绍了Element UI库中的按钮组件的使用方法,包括基础用法、禁用状态、文字按钮、图标按钮、按钮组、加载中状态、不同尺寸的按钮以及按钮的属性说明。文章通过实例代码展示了如何定义按钮样式、添加图标、设置按钮尺寸和类型,并解释了如何绑定方法到按钮以执行操作。
|
移动开发 资源调度 JavaScript
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)
这篇文章介绍了在Vue移动端网页中使用`pdfh5`和`vue-pdf`两个插件来实现PDF文件的预览,包括滚动查看、缩放、添加水印、分页加载、跳转指定页数等功能。
11794 1
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)