查看
  • 编辑修改
  • 首页
  • UNITY
  • NODEJS
  • PYTHON
  • AI
  • GIT
  • PHP
  • GO
  • CEF3
  • JAVA
  • HTML
  • CSS
devbox
笔触狂放9
这个屌丝很懒,什么也没留下!
关注作者
热门标签
  • jquery
  • HTML
  • CSS
  • PHP
  • ASP
  • PYTHON
  • GO
  • AI
  • C
  • C++
  • C#
  • PHOTOSHOP
  • UNITY
  • iOS
  • android
  • vue
  • xml
  • 爬虫
  • SEO
  • LINUX
  • WINDOWS
  • JAVA
  • MFC
  • CEF3
  • CAD
  • NODEJS
  • GIT
  • Pyppeteer
  • article
热门文章
  • 1Vue 登录权限配置_vue3登录游客登录和管理员登录
  • 2STM32_PWM呼吸灯_stm32pwm的呼吸灯设计总结
  • 3python网球比赛模拟_2019-05-12 Python之模拟体育竞赛
  • 4如何理解期权的保证金和权利金的区别?
  • 5python做应用程序错误_pythonw.exe应用程序错误
  • 6Python+Selenium详解(超全)_selenium,python
  • 7云服务器搭载zookeeper集群遇到的坑,java.net.BindExxeption和java.net.SoketTimeoutException_quorumlistenonallips=true
  • 8提取数据_python pdf文件提取表格数据
  • 9【联邦学习+区块链】TORR: A Lightweight Blockchain for Decentralized Federated Learning_federated learning blockchain call for paper
  • 10【编程入门题--二维数组的转置】
当前位置:   article > 正文

vue 文本内容超长截取显示... 悬浮提示全部_vue3文字超长截断

作者:笔触狂放9 | 2024-02-16 07:20:41

赞

踩

vue3文字超长截断

项目使用vue+element UI  根据文本内容长度 进行悬浮提示 (内容过长截取内容显示... 并在鼠标悬浮的时候显示全部),如果内容没有超长就不需要进行截取显示
 

代码内容:

  1. <template>
  2. <div :style="{'width':width,'height':height}" ref="nariTootip" class="nariTootip">
  3. <div class=" pointer " @mouseenter="handleCellMouseEnter($event)" @mouseleave="handleCellMouseLeave($event)">
  4. <div class="topicStyle totip"> {{ value }} </div>
  5. </div>
  6. <el-tooltip ref="tooltip" effect="dark" :content="tooltipContent" :placement="placement"> </el-tooltip>
  7. </div>
  8. </template>
  9. <script>
  10. import debounce from 'throttle-debounce/debounce'
  11. import { getStyle, hasClass } from '@/utils/dom.js'
  12. export default {
  13. name: "nariTootip",
  14. props: {
  15. value: {
  16. type: String,
  17. default: "",
  18. },
  19. width: {
  20. type: String,
  21. default: "100%",
  22. },
  23. height: {
  24. type: String,
  25. default: "100%",
  26. },
  27. placement: {
  28. type: String,
  29. default: "top",
  30. },
  31. },
  32. data() {
  33. return {
  34. tooltipContent: ''
  35. }
  36. },
  37. created() {
  38. this.activateTooltip = debounce(50, tooltip => tooltip.handleShowPopper())
  39. },
  40. methods: {
  41. // 鼠标移入
  42. handleCellMouseEnter(event) {
  43. const target = event.target
  44. // 判断是否text-overflow, 如果是就显示tooltip
  45. let child = target.querySelector('.topicStyle')
  46. // 如果区域宽度被限定,则通过高度判断
  47. let heightFlag = child.scrollHeight > child.offsetHeight
  48. // use range width instead of scrollWidth to determine whether the text is overflowing
  49. // to address a potential FireFox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1074543#c3
  50. const range = document.createRange()
  51. range.setStart(child, 0)
  52. range.setEnd(child, child.childNodes.length)
  53. const rangeWidth = range.getBoundingClientRect().width // 文本区域宽度
  54. const padding = (parseInt(getStyle(target, 'paddingLeft'), 10) || 0) +
  55. (parseInt(getStyle(target, 'paddingRight'), 10) || 0)
  56. if ((rangeWidth + padding > target.offsetWidth || child.scrollWidth > child.offsetWidth) || heightFlag && this.$refs.tooltip) {
  57. const tooltip = this.$refs.tooltip
  58. // TODO 会引起整个 Table 的重新渲染,需要优化
  59. this.tooltipContent = target.innerText || target.textContent
  60. tooltip.referenceElm = target
  61. tooltip.$refs.popper && (tooltip.$refs.popper.style.display = 'none')
  62. tooltip.doDestroy()
  63. tooltip.setExpectedState(true)
  64. this.activateTooltip(tooltip)
  65. }
  66. },
  67. // 鼠标移出
  68. handleCellMouseLeave(event) {
  69. const tooltip = this.$refs.tooltip
  70. if (tooltip) {
  71. tooltip.setExpectedState(false)
  72. tooltip.handleClosePopper()
  73. }
  74. },
  75. },
  76. };
  77. </script>
  78. <style lang="scss" scoped>
  79. .nariTootip {
  80. .totip {
  81. display: inline-block;
  82. white-space: nowrap;
  83. text-overflow: ellipsis;
  84. overflow: hidden;
  85. width: 100%;
  86. font-size: 12px;
  87. }
  88. }
  89. </style>

 

2  参考elementUI table 表格 自适应悬浮提示效果()

因引用如下

 

 

这个是dom.js 代码

  1. /* istanbul ignore next */
  2. import Vue from 'vue';
  3. const isServer = Vue.prototype.$isServer;
  4. const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
  5. const MOZ_HACK_REGEXP = /^moz([A-Z])/;
  6. const ieVersion = isServer ? 0 : Number(document.documentMode);
  7. /* istanbul ignore next */
  8. const trim = function(string) {
  9. return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
  10. };
  11. /* istanbul ignore next */
  12. const camelCase = function(name) {
  13. return name.replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
  14. return offset ? letter.toUpperCase() : letter;
  15. }).replace(MOZ_HACK_REGEXP, 'Moz$1');
  16. };
  17. /* istanbul ignore next */
  18. export const on = (function() {
  19. if (!isServer && document.addEventListener) {
  20. return function(element, event, handler) {
  21. if (element && event && handler) {
  22. element.addEventListener(event, handler, false);
  23. }
  24. };
  25. } else {
  26. return function(element, event, handler) {
  27. if (element && event && handler) {
  28. element.attachEvent('on' + event, handler);
  29. }
  30. };
  31. }
  32. })();
  33. /* istanbul ignore next */
  34. export const off = (function() {
  35. if (!isServer && document.removeEventListener) {
  36. return function(element, event, handler) {
  37. if (element && event) {
  38. element.removeEventListener(event, handler, false);
  39. }
  40. };
  41. } else {
  42. return function(element, event, handler) {
  43. if (element && event) {
  44. element.detachEvent('on' + event, handler);
  45. }
  46. };
  47. }
  48. })();
  49. /* istanbul ignore next */
  50. export const once = function(el, event, fn) {
  51. var listener = function() {
  52. if (fn) {
  53. fn.apply(this, arguments);
  54. }
  55. off(el, event, listener);
  56. };
  57. on(el, event, listener);
  58. };
  59. /* istanbul ignore next */
  60. export function hasClass(el, cls) {
  61. if (!el || !cls) return false;
  62. if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.');
  63. if (el.classList) {
  64. return el.classList.contains(cls);
  65. } else {
  66. return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
  67. }
  68. };
  69. /* istanbul ignore next */
  70. export function addClass(el, cls) {
  71. if (!el) return;
  72. var curClass = el.className;
  73. var classes = (cls || '').split(' ');
  74. for (var i = 0, j = classes.length; i < j; i++) {
  75. var clsName = classes[i];
  76. if (!clsName) continue;
  77. if (el.classList) {
  78. el.classList.add(clsName);
  79. } else if (!hasClass(el, clsName)) {
  80. curClass += ' ' + clsName;
  81. }
  82. }
  83. if (!el.classList) {
  84. el.className = curClass;
  85. }
  86. };
  87. /* istanbul ignore next */
  88. export function removeClass(el, cls) {
  89. if (!el || !cls) return;
  90. var classes = cls.split(' ');
  91. var curClass = ' ' + el.className + ' ';
  92. for (var i = 0, j = classes.length; i < j; i++) {
  93. var clsName = classes[i];
  94. if (!clsName) continue;
  95. if (el.classList) {
  96. el.classList.remove(clsName);
  97. } else if (hasClass(el, clsName)) {
  98. curClass = curClass.replace(' ' + clsName + ' ', ' ');
  99. }
  100. }
  101. if (!el.classList) {
  102. el.className = trim(curClass);
  103. }
  104. };
  105. /* istanbul ignore next */
  106. export const getStyle = ieVersion < 9 ? function(element, styleName) {
  107. if (isServer) return;
  108. if (!element || !styleName) return null;
  109. styleName = camelCase(styleName);
  110. if (styleName === 'float') {
  111. styleName = 'styleFloat';
  112. }
  113. try {
  114. switch (styleName) {
  115. case 'opacity':
  116. try {
  117. return element.filters.item('alpha').opacity / 100;
  118. } catch (e) {
  119. return 1.0;
  120. }
  121. default:
  122. return (element.style[styleName] || element.currentStyle ? element.currentStyle[styleName] : null);
  123. }
  124. } catch (e) {
  125. return element.style[styleName];
  126. }
  127. } : function(element, styleName) {
  128. if (isServer) return;
  129. if (!element || !styleName) return null;
  130. styleName = camelCase(styleName);
  131. if (styleName === 'float') {
  132. styleName = 'cssFloat';
  133. }
  134. try {
  135. var computed = document.defaultView.getComputedStyle(element, '');
  136. return element.style[styleName] || computed ? computed[styleName] : null;
  137. } catch (e) {
  138. return element.style[styleName];
  139. }
  140. };
  141. /* istanbul ignore next */
  142. export function setStyle(element, styleName, value) {
  143. if (!element || !styleName) return;
  144. if (typeof styleName === 'object') {
  145. for (var prop in styleName) {
  146. if (styleName.hasOwnProperty(prop)) {
  147. setStyle(element, prop, styleName[prop]);
  148. }
  149. }
  150. } else {
  151. styleName = camelCase(styleName);
  152. if (styleName === 'opacity' && ieVersion < 9) {
  153. element.style.filter = isNaN(value) ? '' : 'alpha(opacity=' + value * 100 + ')';
  154. } else {
  155. element.style[styleName] = value;
  156. }
  157. }
  158. };
  159. export const isScroll = (el, vertical) => {
  160. if (isServer) return;
  161. const determinedDirection = vertical !== null || vertical !== undefined;
  162. const overflow = determinedDirection
  163. ? vertical
  164. ? getStyle(el, 'overflow-y')
  165. : getStyle(el, 'overflow-x')
  166. : getStyle(el, 'overflow');
  167. return overflow.match(/(scroll|auto)/);
  168. };
  169. export const getScrollContainer = (el, vertical) => {
  170. if (isServer) return;
  171. let parent = el;
  172. while (parent) {
  173. if ([window, document, document.documentElement].includes(parent)) {
  174. return window;
  175. }
  176. if (isScroll(parent, vertical)) {
  177. return parent;
  178. }
  179. parent = parent.parentNode;
  180. }
  181. return parent;
  182. };
  183. export const isInContainer = (el, container) => {
  184. if (isServer || !el || !container) return false;
  185. const elRect = el.getBoundingClientRect();
  186. let containerRect;
  187. if ([window, document, document.documentElement, null, undefined].includes(container)) {
  188. containerRect = {
  189. top: 0,
  190. right: window.innerWidth,
  191. bottom: window.innerHeight,
  192. left: 0
  193. };
  194. } else {
  195. containerRect = container.getBoundingClientRect();
  196. }
  197. return elRect.top < containerRect.bottom &&
  198. elRect.bottom > containerRect.top &&
  199. elRect.right > containerRect.left &&
  200. elRect.left < containerRect.right;
  201. };

效果图是:

内容没有过长的时候

内容过长的时候

 

 

 

3 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  • article铱塔 (iita) 开源 IoT 物联网开发平台,基于 SpringBoot + TDEngine ...
    01铱塔(iita)物联网平台铱塔智联 (open-iita) 基于Java语言的开源物联网基础开发平台,提供了物联网及...

    赞

    踩

  • articlevue3【列表渲染】v-for 详细介绍(vue中的“循环”)_vue3 v-for...
    在本文之前已经有文章简单概要介绍过vue中的渲染,点击帮你快速复习vue3 v-for ...

    赞

    踩

  • articleVue3: 数据过多条数的隐藏与展示(展开折叠功能)_vue3 pc端内容折叠...
    Vue3: 数据过多条数的隐藏与展示(展开折叠功能)_vue3 pc端内容折叠vue3 pc端内容折叠 ...

    赞

    踩

  • articleVue3 内容展开收起动画过渡效果_vue3收缩展开动画插件...
    内容dom(item.expand 展开用内容高度,收起变为0)_vue3收缩展开动画插件vue3收缩展开动画插件 ...

    赞

    踩

  • articleVue3 - 实现任意内容展开 / 收起功能组件插件,点击查看更多内容控制段落文字展开折叠效果,支持...
    vue3展开收起折叠插件组件详细教程,vue3展开收起功能,vue3中实现点击收起和展开效果功能,vue3多行文本展开收...

    赞

    踩

  • article【Vue3】SplitPane 可拖拽分隔面板组件_vue3 splitpanes...
    可拖拽分隔面板_vue3 splitpanesvue3 splitpanes ...

    赞

    踩

  • articleVue3.0入门 + Vant3.0移动端实践(一)环境搭建及首页底部导航栏设计_vue3 开发移动...
    Vue3.0出来了,感觉Vue3.0比2.0好用多啦,且据说性能也有不少的提升,那么今后拥抱Vue3.0吧,会是个趋势。...

    赞

    踩

  • articleVue3.0项目——打造企业级音乐App(一)Tab栏、轮播图、歌单列表、滚动组件_vue3开发企业...
    Vue3.0项目——打造企业级音乐App(一)Tab栏实现、轮播图实现、歌单列表实现、滚动组件的封装_vue3开发企业级...

    赞

    踩

  • articlevue3 自定义tabbar 缓存当前页面的数据_vue tabs标签页缓存...
    1. 封装自定义tabbar组件 需要监听当前的路由路径,当路由发生变化的时候,需要存tabslist和 keepali...

    赞

    踩

  • articleVue3 第三十二篇:常用组件:Tab栏_vue3 tab...
    JSX:CSS_vue3 tabvue3 tab JSX: import style from ...

    赞

    踩

  • article【uniapp - vue3】搭建项目模板 第二章 代码风格检测与限制(eslint + prett...
    为什么要有代码风格管理,平常我们开发不是一个人,而是整个团队,每个人都有自己的编码风格,但这样不利于团队之间的配合, 统...

    赞

    踩

  • article【uniapp&微信小程序】vue3+ts+vite+pinia+eslint+prettier+h...
    本篇文章是从0搭建uniapp项目,如果是个人项目或者公司无代码规范等,可以参考本篇vue3+ts+vite+pinia...

    赞

    踩

  • articleuniapp+vue3+ts(规范化代码配置ESLint+Prettier+Husky)_uni-a...
    uniapp + vue3 + ts 项目的规范化配置(ESLint + Prettier + Husky)。可用于规范...

    赞

    踩

  • articlevue3+eslint7.32.0 自动修复规范代码 开发工具 webstorm_eslint: 7...
    安装指定的eslint//package.json 的devDependencies 加上 运行npm i "babel...

    赞

    踩

  • articlevue3.0如何关闭eslint校验的三种方法_vue3关闭eslint...
    vue3关闭eslint_vue3关闭eslintvue3关闭eslint 前言:ESLint&...

    赞

    踩

  • articlevue3 img图片怎么渲染...
    在 Vue3 中加载图片(img)src地址时,出现无法加载问题。网上很多都建议使用 require 加载相对路径,如下...

    赞

    踩

  • articlevite+vue3+ts+element-plus项目搭建--超详细_vue3 vite ts el...
    vite+vue3+ts+element-plus项目搭建–超详细vite 作用快速的冷启动:不需要等待打包操作;即时的...

    赞

    踩

  • article在 vue3 中用 event.stopPropagation() 方法阻止冒泡触发事件_vue3阻...
    方法可以阻止事件的默认行为,但是它不会影响事件的冒泡行为。在 Vue 3 中,同时存在事件时,例如@click 事件,那...

    赞

    踩

  • articleVue3 事件处理...
    我们可以使用 `v-on` 指令来监听 DOM 事件,从而执行 JavaScript 代码。`v-on` 指令可以缩写为...

    赞

    踩

  • articleVue3 事件处理 -- Vue3后台管理系统模板_vue3 tag中阻止events中默认事件...
    使用 Vue3、Vite、ElementPlus、Pinia最新开发技术栈, 拥有完整的Token登录鉴权、路由配置、界...

    赞

    踩

相关标签
  • 开源
  • 物联网
  • spring boot
  • tdengine
  • 后端
  • vue.js
  • javascript
  • 前端
  • typescript
  • 前端框架
  • 学习
  • css3
  • vue3
  • vue3实现内容展开收起功能
  • 指定内容超出后显示展开折叠
  • vue3高度不固定时查看更多
  • 控制多行文字展开和收起功能组件
  • vue3展开和折叠插件组件教程
  • div容器如何实现展开和收起效

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。

  

闽ICP备14008679号