【vue2】封装文字过长自动省略部分并且鼠标悬浮显示全部

news/2024/7/7 20:03:58

技术:Ant design vue1.7.8 UI框架、vue2.X

需求:实现文字过长自动省略部分,鼠标悬浮显示全部

效果图
图一:

在这里插入图片描述
图二:
在这里插入图片描述

1.封装组件代码:

src/components/Ellipsis/index.js 文件下代码

import Ellipsis from './Ellipsis'

export default Ellipsis

src/components/Ellipsis/Ellipsis.vue 文件下代码

<script>
import Tooltip from 'ant-design-vue/es/tooltip'
import { cutStrByFullLength, getStrFullLength } from '@/components/_util/util'
export default {
  name: 'Ellipsis',
  components: {
    Tooltip
  },
  props: {
    prefixCls: {
      type: String,
      default: 'ant-pro-ellipsis'
    },
    tooltip: {
      type: Boolean
    },
    length: {
      type: Number,
      required: true
    },
    lines: {
      type: Number,
      default: 1
    },
    fullWidthRecognition: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    getStrDom (str, fullLength) {
      return (
        <span>{ cutStrByFullLength(str, this.length) + (fullLength > this.length ? '...' : '') }</span>
      )
    },
    getTooltip (fullStr, fullLength) {
      return (
        <Tooltip>
          <template slot="title">{ fullStr }</template>
          { this.getStrDom(fullStr, fullLength) }
        </Tooltip>
      )
    }
  },
  render () {
    const { tooltip, length } = this.$props
    const str = this.$slots.default.map(vNode => vNode.text).join('')
    const fullLength = getStrFullLength(str)
    const strDom = tooltip && fullLength > length ? this.getTooltip(str, fullLength) : this.getStrDom(str, fullLength)
    return (
      strDom
    )
  }
}
</script>

src/components/_util/util.js 文件下代码


/**
 * 获取字符串长度,英文字符 长度1,中文字符长度2
 * @param {*} str
 */
export const getStrFullLength = (str = '') =>
  str.split('').reduce((pre, cur) => {
    const charCode = cur.charCodeAt(0)
    if (charCode >= 0 && charCode <= 128) {
      return pre + 1
    }
    return pre + 2
  }, 0)

/**
 * 截取字符串,根据 maxLength 截取后返回
 * @param {*} str
 * @param {*} maxLength
 */
export const cutStrByFullLength = (str = '', maxLength) => {
  let showLength = 0
  return str.split('').reduce((pre, cur) => {
    const charCode = cur.charCodeAt(0)
    if (charCode >= 0 && charCode <= 128) {
      showLength += 1
    } else {
      showLength += 2
    }
    if (showLength <= maxLength) {
      return pre + cur
    }
    return pre
  }, '')
}

2.使用方法

script代码块

import { Ellipsis } from '@/components'
export default {
  components: {
    Ellipsis,
  },
 }

template代码块

 <ellipsis :length="10" tooltip>{{ name }}</ellipsis>

http://lihuaxi.xjx100.cn/news/1280191.html

相关文章

dsl语句查询elasticsearch集群节点分布和资源使用情况

查询语句如下&#xff08;本文是直接在kibana里面执行的哦&#xff09; GET _cat/nodes?v执行结果 这样就可以很直观的看到&#xff0c;es部署在了哪些节点上&#xff0c;以及各节点资源分布使用

【PostgreSQL-16新特性之普通用户的保留连接个数(reserved_connections)】

PostgreSQL数据库为了保证在高并发&#xff0c;高连接数情况下某些用户能够正常连接到数据库里&#xff0c;设立了几个用户连接的保留个数。 本文介绍了PostgreSQL16版本前为超级用户保留的连接数&#xff08;superuser_reserved_connections&#xff09;以及PostgreSQL16版本…

python3.10-一些有意思的语法

python3.10发行已经有一段时间了&#xff0c;但是时至今日才开始用上python3.10版本&#xff0c;说实话有点惭愧。下面来记录一下&#xff0c;在Python3.10版本中几个亮眼的语法变更&#xff1a; 带括号的上下文管理器 在以前&#xff0c;我们如果需要打开多个文件&#xff0…

DALL-E2原理解读——大模型论文阅读笔记五

论文&#xff1a;https://cdn.openai.com/papers/dall-e-2.pdf 项目&#xff1a;https://openai.com/dall-e-2 一. 主要思想 利用CLIP提取的文本特征&#xff0c;级联式的生成图片。第一阶段通过prior将文本特征与图像特征进行对齐&#xff0c;第二阶段用扩散模型将视觉特征转…

基于matlab多运动目标跟踪监测算法实现(附源码)

一、前言 此示例演示如何对来自固定摄像机的视频中的移动对象执行自动检测和基于运动的跟踪。 二、介绍 移动物体检测和基于运动的跟踪是许多计算机视觉应用的重要组成部分&#xff0c;包括活动识别、交通监控和汽车安全。基于运动的对象跟踪问题可以分为两部分&#xff1a; 检…

Jupyter notebook运行环境创建

进入到jupyter notebook,没找到自己之前创建的环境xzc_pytorch 进入到Anaconda prompt,输入如下命令 (xzc_pytorch) C:\Users\xzc> conda install ipykernel安装完后&#xff0c;重启jupyter notebook发现还是没有 &#xff0c;此时选择手动创建&#xff0c;在base环境下执…

Segment Anything Model Geospatial (SAM-Geo) 创建交互式地图

SAM-Geo是一个用于地理空间数据的Python 包&#xff0c;可在 PyPI 和 conda-forge 上使用。本节教程是SAM-Geo官网的一个教程&#xff0c;根据输入提示范围创建mask遮罩。后面还有一种基于提示词创建的方式&#xff0c;如只输出房屋、道路、树木等&#xff0c;下一期我们专门写…

【Makefile】解析Makefile:驾驭自动编译的力量

Makefile简介 一个工程中的源文件不计其数&#xff0c;其按类型、功能、模块分别放在若干个目录中&#xff0c;makefile定义了一系列的规则来指定&#xff0c;哪些文件需要先编译&#xff0c;哪些文件需要后编译&#xff0c;哪些文件需要重新编译&#xff0c;甚至于进行更复杂的…