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>
相关文章
|
4月前
|
JavaScript
Vue项目中使用wangEditor富文本输入框(推荐)
Vue项目中使用wangEditor富文本输入框(推荐)
|
21天前
Vue3信息提示(Modal)
这是一个基于 Vue2 的信息提示模态框组件,支持多种弹窗类型(如 info、success、error 等),并提供丰富的自定义属性,包括按钮文本、按钮类型、居中方式等。该组件可根据内容自动调整高度,并兼容不同按钮样式配置。预览效果展示了不同类型的模态框及其样式。代码中详细介绍了组件的实现方式和使用方法。
Vue3信息提示(Modal)
|
21天前
Vue2信息提示(Modal)
这是一个基于 Vue3 的信息提示模态框(Modal)组件,提供了丰富的自定义属性,包括标题、内容、宽度、按钮文本等。它支持两种模式:确认提示框(confirm)和信息提示框(info),并有六种不同的展示效果。模态框可以水平垂直居中或固定高度水平居中显示,支持加载中状态。该组件模仿了 ant-design-vue 的样式,适用于各种场景下的信息提示。
Vue2信息提示(Modal)
|
1天前
|
JavaScript 前端开发
vue动态添加style样式
vue动态添加style样式
|
21天前
Vue3文字提示(Tooltip)
这是一篇关于 Vue2 文字提示(Tooltip)组件的教程,支持多种自定义属性,如最大宽度、展示文本、提示文本、样式、背景色、箭头显示等,并提供了在线预览示例。组件通过监听插槽和 `requestAnimationFrame` 实现了延迟显示与隐藏效果。文章详细介绍了组件实现代码及在页面中的使用方法。
Vue3文字提示(Tooltip)
|
4月前
|
JavaScript 前端开发
iconfont 图标在vue里的使用
iconfont 图标在vue里的使用
288 0
|
2月前
|
JavaScript
vue 头像修改-裁剪图片 vue-cropper
vue 头像修改-裁剪图片 vue-cropper
93 0
|
11月前
|
JavaScript
vue + element-ui + vue-clipboard2 实现文字复制粘贴功能与提示
1、在所在项目下安装插件 ```js npm install vue-clipboard2 --save ``` 2、在所在项目的index.js注入vue-clipboard2 ```js import VueClipboard from 'vue-clipboard2' Vue.use(VueClipboard) ``` 3、使用 ```html <div> <el-button size="mini" type="primary" icon="el-icon-copy-document" round class="copy-btn" v-clipboard:copy="要
164 2
|
4月前
|
移动开发 JavaScript 小程序
uView Tooltip 长按提示
uView Tooltip 长按提示
44 1
|
4月前
|
JavaScript 测试技术 iOS开发
vue element plus Icon 图标
vue element plus Icon 图标
113 0